The main topic: There are n cattle in a piece, M single rope, there is m link relationship, ask how many groups inside any two cows can reach each other
The thinking of solving problems: the map of strongly connected component mode
The code is as follows:
#include <stdio.h>#include<vector>#include<map>#include<Set>#include<algorithm>using namespaceStd;typedefLong Longll;Const intN =10003; Vector<int>G[n], DQ;intLow[n], dfn[n], tot;BOOLMk[n];intN, m, ans;voidinit () {ans= Tot =0; Dq.clear (); for(intI=1; i<=n; ++i) {g[i].clear (); Low[i]= Dfn[i] =-1; Mk[i]=false; }}voidTarjan (intUintf) {Dfn[u]= Low[u] = + +tot; Dq.push_back (U); Mk[u]=true; for(inti =0; I<g[u].size (); ++i) {intv =G[u][i]; if(Dfn[v] = =-1) {Tarjan (V, u); Low[u]=min (Low[u], low[v]); } Else if(Mk[v]) Low[u]=min (Low[u], dfn[v]); } if(Dfn[u] = =Low[u]) { ints; intK =0; Do{s=Dq.back (); K++; Dq.pop_back (); Mk[s]=false; } while(U! =s); if(k >1) ans++; }}voidsolve () { for(intI=1; i<=n; ++i) {if(Dfn[i] = =-1) Tarjan (i,-1); } printf ("%d\n", ans);}intMain () { while(~SCANF ("%d%d", &n, &m)) {init (); for(intI=1; i<=m; ++i) {intu, v; scanf ("%d%d", &u, &v); G[u].push_back (v); } solve (); } return 0;}
View Code
POJ 3180-the Cow Prom (graph theory-strong link Tarjan algorithm)