Title Link: http://poj.org/problem?id=1236
Test instructions
This is entitled to the map.
There are two issues that need to be addressed:
1 The number of points that need to be given in order to spread all points.
2 plus how many edges, making the entire graph become strong connectivity.
Use Tarjan to indent points, get an SCC diagram,
The number of degrees in this figure is 0, and the number of degrees is 0.
Assume that there are n degrees in the 0,m with a degree of 0
So the first answer is N, and the second answer is Max (n,m)
Code:
#include <stdio.h>#include <iostream>#include <math.h>#include <stdlib.h>#include <ctype.h>#include <algorithm>#include <vector>#include <string.h>#include <string>#include <queue>#include <stack>#include <set>#include <map>#include <sstream>#include <time.h>using namespace STD;Const intMAXN =20010;Const intMAXM =50010;structedge{intTo, next;} EDGE[MAXM];intHEAD[MAXM], tot;intLOW[MAXN], DFN[MAXN], STACK[MAXN], BELONG[MAXN];the value of//belong is 1 ~ SCCintIndex, top;intScc//Strong connectivity numberBOOLINSTACK[MAXN];intNUM[MAXN];//The number of points that each strong connect containsvoidAddedge (intUintV) {edge[tot].to = v; Edge[tot].next = Head[u]; Head[u] = tot++;}voidTarjan (intu) {intV Low[u] = dfn[u] = ++index; stack[top++] = u; Instack[u] =true; for(inti = Head[u]; I! =-1; i = edge[i].next) {v = edge[i].to;if(! Dfn[v]) {Tarjan (v);if(Low[u] > Low[v]) Low[u] = Low[v]; }Else if(Instack[v] && low[u] > Dfn[v]) 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); }}intIN[MAXN], OUT[MAXN];voidSolveintN) {memset(DFN,0,sizeof(DFN));memset(Instack,false,sizeof(Instack));memset(Num,0,sizeof(num)); Index = SCC = top =0; for(inti =1; I <= N; i++) {if(! Dfn[i]) Tarjan (i); }if(SCC = =1) {printf("1\n0\n");return; } for(inti =1; I <= SCC; i++) In[i] = out[i] =0; for(intU =1; U <= N; u++) { for(inti = Head[u]; I! =-1; i = edge[i].next) {intv = edge[i].to;if(Belong[u]! = Belong[v]) {in[belong[v]]++; out[belong[u]]++; } } }intANS1 =0, Ans2 =0; for(inti =1; I <= SCC; i++) {if(In[i] = =0) ans1++;if(Out[i] = =0) ans2++; }//printf ("%d\n", SCC); printf("%d\n%d\n", Ans1,max (ANS1,ANS2));}voidInit () {tot =0;memset(head,-1,sizeof(head));}intMain () {intNintU, v; while(~scanf("%d", &n)) {init (); for(inti =1; I <= N; i++) { while(~scanf("%d", &u) && u) {Addedge (i,u); }} solve (n); }return 0;}
POJ 1236 Network of schools "Tarjan"