PS: Strong connectivity, pinch point. Take care not to forget that the diagram is strongly connected, WA has been 4 times. Save the race and warm up.
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include < Vector> #include <stack>using namespace std;const int maxn = 110;vector<int> g[maxn];vector<int> G1 [maxn];vector<int> rg1[maxn];int n;int LOW[MAXN], PRE[MAXN], Sccno[maxn];int Dfs_clock, Scc_cnt;stack<int > s;void dfs (int u) {low[u] = pre[u] = ++dfs_clock; S.push (U); for (int i = 0; i < (int) g[u].size (); i++) {int v = g[u][i]; if (!pre[v]) {DFS (v); Low[u] = min (Low[u], low[v]); } else if (!sccno[v]) {Low[u] = min (Low[u], low[v]); }} if (Pre[u]==low[u]) {scc_cnt++; for (;;) {int x = S.top (); S.pop (); SCCNO[X] = scc_cnt; if (x==u) break; }}}void FIND_SCC () {while (! S.empty ()) S.pop (); memset (sccno, 0, sizeof (SCCNO)); memset (pre, 0, sizeof (pre)); memset (Low, 0, sizeof (low)); scc_cnt = 0; Dfs_clock = 0; for (int i = 1; I <= n; i++) {if (!pre[i]) DFS (i); }}void Build_dag () {int S, t; for (int i = 1; I <= scc_cnt; i++) {g1[i].clear (); Rg1[i].clear (); } for (int i = 1; I <= N, i++) {for (int j = 0; J < (int) g[i].size (); j + +) {int v = g[i][j]; s = sccno[i]; t = Sccno[v]; if (s!=t) {g1[s].push_back (t); Rg1[t].push_back (s); }}}}void work () {int ans1 = 0, ans2 = 0; for (int i = 1; I <= scc_cnt; i++) {if (! Rg1[i].size ()) {ans1++; }} for (int i = 1; I <= scc_cnt; i++) {if (! G1[i].size ()) {ans2++; }} if (scc_cnt==1) {//WA. printf ("1\n0\n"); Return } printf ("%d\n", ans1); printf ("%d\n", Max (ans1, Ans2));} int main () {int x; while (scanf ("%d", &n)!=eof) {for (int i = 1; I <= n; i++) g[i].clear (); for (int i = 1; I <= N; i++) {for (;;) {scanf ("%d", &x); if (x!=0) g[i].push_back (x); else break; }} FIND_SCC (); Build_dag (); Work (); } return 0;}