At first completely did not understand the meaning of the topic, but incredibly also ac two points?
Take a closer look at the meaning of the topic and find the topic is not difficult.
For the first question, we only need to ask for a reduction in the number of points with a degree of 0.
For the second question, our goal is to require that all points after the indentation be connected to each other (because the only way to get to each other is to choose a point)
We change the meaning: All points after the indentation only the degree of penetration and the degree of more than 0 to be interconnected
So the second question is that we only need to compare the points of 0 and the out of 0, and who gets the value output.
Pit point: You need to give a special sentence when there is only one unicom block, otherwise it will be wrong.
#include <cstdio>#include<algorithm>#include<cstring>#include<stack>usingStd::stack;Const intN =233;structnode{intU,v,next; Node () {} node (int_u,int_v,int_next) {u=_u; V=_v; Next=_next; }}edge[n*N];inthead[n*N],count;intDfn[n],low[n];//scc_cnt is the number of unicom blocks (the amount of points after the point of contraction)//Sccno is the ordinal after the indent .intScc_cnt,step,sccno[n];intN;voidAddedge (intUintv) {Count++; Edge[count]=node (u,v,head[u]); Head[u]=Count;}voidinit () {scanf ("%d",&N); for(intI=1; i<=n;i++){ intx; while(SCANF ("%d", &x) && x!=0) {Addedge (i,x); }}}stack<int>S;voidDfsintx) {Dfn[x]= Low[x] = + +step; S.push (x); for(inti = head[x];i;i=Edge[i].next) { intv =edge[i].v; if(!Dfn[v]) {DFS (v); LOW[X]=std::min (Low[x],low[v]); } Else if(!sccno[v]) low[x] =std::min (Low[x],dfn[v]); } if(Low[x] = =Dfn[x]) {scc_cnt++; while(1){ intU =S.top (); S.pop (); Sccno[u]=scc_cnt; if(X==u) Break; } }}intTo[n],out[n];intMain () {init (); for(intI=1; i<=n;i++){ if(!Dfn[i]) DFS (i); } for(intI=1; i<=count;i++){ if(sccno[edge[i].u]!=SCCNO[EDGE[I].V]) {OUT[SCCNO[EDGE[I].U]]++; TO[SCCNO[EDGE[I].V]]++; } } //To- in degrees out of intask1=0, ask2=0; for(intI=1; i<=scc_cnt;i++){ if( ! To[i]) ask1++; if( ! Out[i]) ask2++; } intEnd =Std::max (ASK1,ASK2); if(scc_cnt==1) {printf ("1\n0\n"); } Else{printf ("%d\n%d\n", Ask1,end); } return 0; }
[Tarjan] Luo gu P2746 [USACO5.3] Campus Network of schools