Title Link: http://poj.org/problem?id=1236
Test instructions: Give the network link between the N schools and some schools, the network between schools is one-way side, let you find the answer to two questions, 1. At least how many copies of software are needed so that all schools can receive it. 2. If you want to use a piece of software to make all schools receive the need to add several sides
The first is to find a strong connected component and then shrink the point, so-called indentation is a connected graph into a point. And then the Unicom map to form a diagram.
Then the question of the problem 1 is only required to connect to the point of 0 of the points and the line, problem 2 is to find the connectivity component and the degree of 0 and the most
Large value. (In order to form a fully connected component of the graph Unicom, at least to add max (in the degree of 0, out of 0) to ensure that unicom).
#include <iostream> #include <cstring> #include <vector> #include <cstdio>using namespace std; const int M = 10000 + 10;const int N = + 10;struct TnT {int V, next;} Edge[m];int Head[n], E;int low[n], dfn[n], stack[n], Belong[n];int Index, Top;int scc;bool instack[n];int num[N];void Init () {memset (head,-1, sizeof (head)); e = 0;} void Add (int u, int v) {edge[e].v = V, edge[e].next = Head[u], head[u] = e++;} void Tarjan (int u) {int V; Low[u] = dfn[u] = ++index; stack[top++] = u; Instack[u] = true; for (int i = head[u]; i =-1; i = edge[i].next) {int v = EDGE[I].V; if (! Dfn[v]) {Tarjan (v); Low[u] = min (Low[u], low[v]); } else if (Instack[v]) low[u] = min (Low[u], dfn[v]); } if (low[u] = = Dfn[u]) {scc++; do {v = stack[--top]; INSTACK[V] = false; BELONG[V] = SCC; num[scc]++; } while (V! = u); }}inT In[n], Out[n];int main () {int N; while (scanf ("%d", &n)! = EOF) {init (); for (int i = 1; I <= n; i++) {int x; while (scanf ("%d", &x)) {if (x = = 0) break; Add (i, x); }} memset (DFN, 0, sizeof (DFN)); Memset (Instack, False, sizeof (Instack)); memset (num, 0, sizeof (num)); memset (in, 0, sizeof (in)); memset (out, 0, sizeof (out)); for (int i = 1; I <= n; i++) if (! Dfn[i]) Tarjan (i); for (int i = 1, i <= N; i++) {for (int j = head[i]; J! =-1; j = edge[j].next) {int v = e DGE[J].V; if (belong[i]! = Belong[v]) {in[belong[v]]++; out[belong[i]]++; }}} int ans1 = 0, ans2 = 0; for (int i = 1; I <= SCC; i++) {if (in[i] = = 0) ans1++; if (out[i] = = 0) ans2++; } if (SCC = = 1) printf ("1\n0\n"); else printf ("%d\n%d\n", Ans1, Max (ans1, ans2)); } return 0;}
POJ 1236 Network of schools (Tarjan indent)