The first is to strongly connect the indentation point, statistics of the new graph of the points of the degree and the degree.
First question the number of points with a direct output of 0
The second question is, if the new diagram becomes a strong connected graph, then each point must have at least one out edge and one edge, the output degree and the number of points with a size of 0
Note that the input is already a very strong connected graph, Output 1 0
Code
/* undirected graph Strong Connectivity Garbow algorithm, the idea and the Tarjan algorithm is the same, the realization more direct, more efficient time complexity is also O (n+m) ideas: DFN record access sequence, ST for the Access stack, TEM for the auxiliary stack every time to find Ring, all points in the ring except the point at the top of the order are all out of the stack St tem, and all point above the current point (including the current point) is a strong connected component */#include <iostream> #include <cstring>using namespace Std;const int INF = 109;struct node {int u, V, NE;} E[inf*inf];int Head[inf], Cnt;int Dfn[inf], Num[inf], Sta[inf], Tops, Tem[inf], topt, scc;int n;void addedge (int u, int v ) {e[++cnt].u = u, e[cnt].v = v; E[cnt].ne = Head[u];head[u] = cnt;} void Dfs (int k, int t) {Sta[++tops] = tem[++topt] = k;dfn[k] = ++t;for (int i = head[k]; I! = 0; i = e[i].ne) {int v = e[ I].v;if (!dfn[v]) Dfs (V, t), else if (num[v] = = 0) while (Dfn[sta[tops]] > Dfn[v]) tops--;} if (sta[tops] = = k) {Tops--, scc++;d onum[tem[topt]] = Scc;while (tem[topt--]! = k);}} void Garbow (int n) {memset (DFN, 0, sizeof DFN); memset (num, 0, sizeof num); Tops = topt = SCC = 0;for (int i = 1; I <= n; i++) if (!num[i]) DFS (i, 0);} void make () {Garbow (n); int Degi[inf], dego[inf];memset (Degi, 0, sizeof Degi), memset (dego, 0, sizeof Dego), if (SCC = = 1) {cout << 1 << endl << 0 << Endl; return;} for (int i = 1; I <= cnt; i++) {int u = e[i].u, v = e[i].v;if (num[u]! = Num[v]) {Degi[num[v]] = 1;dego[num[u]] = 1;}} int ans1=0, ans2=0;for (int i = 1; I <= SCC; i++) {if (degi[i] = = 0) ans1++;if (dego[i] = = 0) ans2++;} cout << ans1 <<endl<< max (ans1, ans2);} int main () {cin >> n;for (int i = 1; I <= n; i++) {int X;while (cin >> x && x) Addedge (i, x);} Make (); return 0;}
POJ 1236.Network of schools Problem Solving report