题目链接:http://hihocoder.com/problemset/problem/1175
代码实现如下:
1 #include <queue> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 6 const int maxn = 1e5 + 7; 7 const int mod = 142857; 8 int n, m, k, u, v, x; 9 int InDeg[maxn], virus[maxn]; 10 vector<int> G[maxn]; 11 12 void topsort() { 13 queue<int> q; 14 for(int i = 1; i <= n; i++) { 15 if(InDeg[i] == 0) { 16 q.push(i); 17 } 18 } 19 int x; 20 while(!q.empty()) { 21 x = q.front(), q.pop(); 22 int s = G[x].size(); 23 for(int i = 0; i < s; i++) { 24 if(--InDeg[G[x][i]] == 0) { 25 q.push(G[x][i]); 26 } 27 virus[G[x][i]] = (virus[G[x][i]] + virus[x]) % mod; 28 } 29 } 30 } 31 32 int main() { 33 while(~scanf("%d%d%d", &n, &m, &k)) { 34 for(int i = 0; i <= n; i++) { 35 G[i].clear(); 36 } 37 memset(InDeg, 0, sizeof(InDeg)); 38 memset(virus, 0, sizeof(virus)); 39 for(int i = 0; i < k; i++) { 40 scanf("%d", &x); 41 virus[x] = 1; 42 } 43 for(int i = 0 ; i < m; i++) { 44 scanf("%d%d", &u, &v); 45 G[u].push_back(v); 46 InDeg[v]++; 47 } 48 topsort(); 49 int ans = 0; 50 for(int i = 1; i <= n; i++) { 51 ans = (ans + virus[i]) % mod; 52 } 53 printf("%d ", ans); 54 } 55 return 0; 56 }