Test instructions: Gives a graph that represents the relationship that cows and cows like, and likes the relationship to be transitive, to find the total number of cows that can be liked by all cows (except their own cows, or it is narcissistic).
Idea: The difficulty of this is that this is a ring diagram, we can use the Tarjan algorithm to find strong connected components, the strong connected components compressed into a point, a new figure, this figure must be no ring, if there is a link with strong connectivity components of the contradiction. Compressed into a non-circular graph after the point in this figure is not a direction, we can traverse each node to connect to the point, if the ID value of two points is located in the strong connected component region is not the same, we will add this node out of the degree of 1. Finally, to find out that the degree is equal to 0 points, if there is no or more than 1, then the answer to this figure is 0, because if there are two points they are out of 0, then it means that there is no relationship between the two points, so the map can not appear a cow is all the cattle like.
Note that there was an error in the ID record of my connected component at the beginning because my first point was in the main function, causing some points to not be recorded. Later affected by the inertial thinking of the graph, and the Judge PA! The condition of the = v Plus, this is the direction of the graph, is definitely not to add this judgment! The specific code is as follows:
#include <cstdio>#include<stack>#include<cstring>#include<iostream>using namespacestd;#defineMAXN 10010structedge{intTO,NXT;} EDGE[MAXN*5];intHead[maxn],id[maxn],dfn[maxn],low[maxn],tot,sum;stack<int>s;intALL[MAXN];voidTarjan (intUintFA) {s.push (U); Dfn[u]= Low[u] = + +tot; for(inti = Head[u]; I! =-1; i =edge[i].nxt) { intv =edge[i].to; if(!Dfn[v]) {Tarjan (v,u); Low[u]=min (low[v],low[u]); } Else /*if (v! = FA)*/Low[u] =min (low[u],dfn[v]); } if(Low[u] = =Dfn[u]) {Sum++; while(!S.empty ()) { intnum =S.top (); S.pop (); Id[num]=sum; All[sum]++; if(U = = num) Break; } } return ;}intMain () {intN,m,x,y; while(~SCANF ("%d%d",&n,&m) {memset (head,-1,sizeof(head)); for(inti =0; I < m; i++) {scanf ("%d%d",&x,&y); Edge[i].to=y; EDGE[I].NXT=Head[x]; HEAD[X]=i; } memset (DFN,0,sizeof(DFN)); memset (Low,0,sizeof(low)); memset (ID,0,sizeof(ID)); Tot=0, sum =0; while(!s.empty ()) S.pop (); memset (All,0,sizeof(All)); for(inti =1; I <= N; i++) { if(!Dfn[i]) {Tarjan (i,-1); } } //cout<< "sum =" <<sum<<endl; intVIS[MAXN]; memset (Vis,0,sizeof(VIS)); intDU[MAXN]; Memset (Du,0,sizeof(du)); /*for (int i = 1;i <= sum;i++) {cout<< ' all = ' <<all[i]<<endl; }*/ for(intU =1; U <= N; u++) { for(intj = Head[u]; J! =-1; j =edge[j].nxt) { intv =edge[j].to; if(Id[u]! =Id[v]) {Du[id[u]]++; } } } intSub =0, last =0; for(inti =1; I <= sum; i++) { if(Du[i] = =0) {Sub++; Last=i; } } if(Sub! =1) puts ("0"); Elseprintf"%d\n", All[last]); } return 0;}
POJ 2186 Popular cows Tarjan scaling algorithm