POJ 1236 Network of schools
Topic links
Test instructions: Test instructions is essentially, given a graph, ask two questions
1, from which several vertices start, can walk all a little
2, at least a few sides, making the diagram strong connectivity
Ideas:
#include <cstdio> #include <cstring> #include <vector> #include <stack>using namespace std; const int N = 105;int n;vector<int> g[n];int pre[n], sccno[n], dfn[n], Dfs_clock, sccn;stack<int> S;void dfs_s CC (int u) {pre[u] = dfn[u] = ++dfs_clock; S.push (U); for (int i = 0; i < g[u].size (); i++) {int v = g[u][i];if (!pre[v]) {DFS_SCC (v);d fn[u] = min (Dfn[u], dfn[v]); } else if (!sccno[v]) dfn[u] = min (Dfn[u], pre[v]);} if (dfn[u] = = Pre[u]) {sccn++;while (1) {int x = S.top (); S.pop (); sccno[x] = sccn;if (x = = u) break;}}} void Find_scc () {SCCN = Dfs_clock = 0;memset (pre, 0, sizeof (PRE)), memset (sccno, 0, sizeof (SCCNO)); for (int i = 1; I <= N i++) if (!pre[i]) DFS_SCC (i);} int In[n], out[n];int main () {while (~SCANF ("%d", &n)) {int v;for (int i = 1; I <= N; i++) g[i].clear (); int cnt = N ; for (int u = 1; u <= n; u++) {while (~SCANF ("%d", &v) && v) g[u].push_back (v);} FIND_SCC (); memset (in, 0, sizeof (in)), memset (out, 0, sizeof), and for (int u =1; U <= N; u++) {for (int j = 0; J < G[u].size (); j + +) {int v = g[u][j];if (sccno[u]! = Sccno[v]) {in[sccno[v]]++;out[sccno[u]]++; }}}int ins = 0, outs = 0;for (int i = 1; I <= SCCN; i++) {if (!in[i]) ins++;if (!out[i]) outs++;} int ans1 = ins, ans2 = max (ins, outs), if (SCCN = = 1) ans2 = 0;printf ("%d\n%d\n", ans1, Ans2);} return 0;}
POJ 1236 Network of schools (strong connected components)