Title Link: http://poj.org/problem?id=2186
The main topic: There are n cows and m pairs of relations, each pair of relations have two number (A, B) on behalf of a bull said B bull is "popular", and this relationship is transitive, if a bull think B Bull "popular", b Bull think C Bull "popular", then a bull also think C Bull "popular". Now you want to know how many cows have been welcomed by all the other cows besides himself?
Problem-solving ideas: If there are two or more bulls in addition to his own outside all other cattle welcome, then in this or bulls, any one of the cattle is also subject to either end or bull cattle in the welcome, that is, the two or bulls with a strong unicom component, and other strong unicom components can reach the strong unicom component. Then can use the Korasaju algorithm to the strong connectivity component decomposition, and then also can get the order of each strong Unicom component topological ordering, then the only can become the solution of only the topological order of the last of the Unicom component, and need to check whether other strong Unicom components can all reach this strong unicom component, if can all reached , then how many elements of the strong Unicom component is the solution of the problem, otherwise 0;
The steps of decomposition of the strong Unicom component:
1: For figure G, the depth of the first traversal of G, calculate the end of each node U time S[u], the starting point how to choose does not matter.
2: For the figure g of the transpose diagram RG, select the starting point of the traversal, according to the end time of the node from large to small, traversing the process, one pass through the node to do the classification mark, each to find a new starting point, the classification tag plus a
3: For the second step, each point of the same mark is a strong component
The code is as follows:
#include <stdio.h>#include<string.h>#include<vector>#include<algorithm>using namespacestd;Const intN =10003; Vector<int>G[n], rg[n], vs;BOOLUsed[n];intv;intCmp[n];voidAdd_edge (intAintb) {G[a].push_back (b); Rg[b].push_back (a);}voidDfsintN) {Used[n]=true; for(intI=0; I<g[n].size (); ++i) {intv =G[n][i]; if(Used[v] = =false) Dfs (v); } vs.push_back (n);}voidRdfsintNintk) {Used[n]=true, cmp[n] =K; for(intI=0; I<rg[n].size (); ++i) {intv=Rg[n][i]; if(Used[v] = =false) Rdfs (V, k); }}intSCC () {memset (used,false,sizeof(used)); Vs.clear (); for(intI=0; i<v; ++i)if(Used[i] = =false) Dfs (i); memset (Used,false,sizeof(used)); intk=0; for(intI=vs.size ()-1; i>=0; --i) {if(Used[vs[i]] = =false) Rdfs (Vs[i], K++); } returnK;}intMain () {intm; scanf ("%d%d", &v, &m); for(intI=1; i<=m; ++i) {intA, B; scanf ("%d%d", &a, &b); Add_edge (A-1, B-1); } intn =SCC (); intU =0, num =0; for(intI=0; i<v; ++i)if(Cmp[i] = = N1) {u= i, num + +; } memset (Used,false,sizeof(used)); Rdfs (U,0); for(intI=0; i<v; ++i)if(Used[i] = =false) {num=0; Break; } printf ("%d\n", num);}
View Code
Note: The newly-learned strong unicom component, there is not please point out, extremely grateful!
POJ 2186-popular Cows (graph theory-strong unicom component Korasaju algorithm)