【AOJ 2155】Infected Computer
問題
Infected Computer | Aizu Online Judge
方針
与えられたデータをtで昇順ソートする.
あとはdとsの値を見てフラグにチェックをいれていき, カウントする.
コード
#include <iostream> #include <algorithm> using namespace std; #define LOG_MAX 20000 #define N_MAX 20000 struct LOG { int s, t, d; bool operator < (const LOG& another) const { return t < another.t; } }; LOG l[LOG_MAX+1]; int n, m; signed main(){ while(cin >> n >> m, n||m){ bool infected[N_MAX+1]; for(int i = 0 ; i < m ; i++){ cin >> l[i].t >> l[i].s >> l[i].d; } sort(l, l+m); for(int i = 0 ; i <= n ; i++) infected[i] = false; infected[1] = true; for(int i = 0 ; i < m ; i++) infected[l[i].d] |= infected[l[i].s]; int ans = 0; for(int i = 1 ; i <= n ; i++) if(infected[i]) ans++; cout << ans << endl; } }