int dp[MAXN]; // dp[i][j] -> the amount of links that the end-node = i
voidTopsort(){ std::queue<int> q; for (int i = 1; i <= n; ++i) { if (!id[i]) { q.push(i); top.push_back(i); } } while (!q.empty()) { int u = q.front(); q.pop(); int amt = head[u].size(); for (int i = 0; i < amt; ++i) { int v = head[u][i]; --id[v]; if (!id[v]) { top.push_back(v); q.push(v); } } } }
intmain(){ IMPROVE_IO(); cin >> n >> m; for (int i = 1; i <= m; ++i) { int A = 0, B = 0; cin >> A >> B; head[A].push_back(B); reallink[B].push_back(A); ++id[B]; } Topsort(); for (int i = 1; i <= n; ++i) { // enumerate Topsorted-Nodes int nnode = top[i - 1]; if (reallink[nnode].size() == 0) dp[nnode] = 1; // no out-edges connected for (int j = 0; j < reallink[nnode].size(); ++j) { int nenode = reallink[nnode][j]; dp[nnode] += dp[nenode]; ADD(dp[nnode]); } if (!head[nnode].size()) ans += dp[nnode]; ADD(ans); } cout << ans << endl; return0; }