title: http://acm.hdu.edu.cn/showproblem.php?pid=1269
Connected components, intermittent look for a few days, today finally a strong Unicom component (SCC) template problem. Beginner SCC, understanding is not very good, is the use of DFS in the backtracking process to operate, the details of the master is not good enough.
The topic test instructions is to judge whether the number of SCC in the graph is unique, the template problem;
#include <stack>
#include <cstdio>
#include <cstring>
#include <iostream>
usingnamespaceStd
ConstintN =10001;
structEdge
{
int from, to, next;
} edge[100100];
intN, M, CNT, CNT_SCC, Vis_num, Low[n], tim[n], head[n], instack[n];
voidADD (intAintb
{
Edge E = {A, B, head[a]};
EDGE[CNT] = E;
Head[a] = cnt++;
}
Stack <int>S;
voidTarjan (intX
{
intJ
TIM[X] = low[x] = ++vis_num;
INSTACK[X] =1;
S.push (x);
for(inti = head[x]; I! =-1; i = edge[i].next)
{
intu = edge[i].to;
if(Tim[u] = =-1)
{
Tarjan (U);
if(Low[x] > Low[u])
LOW[X] = Low[u];
}
Elseif(Instack[u] && low[x] > Tim[u])
LOW[X] = Tim[u];
}
if(Low[x] = = Tim[x])
{
cnt_scc++;
Do{
j = S.top ();
S.pop ();
INSTACK[J] =0;
} while(J! = x);
}
}
voidSolve ()
{
Vis_num = CNT_SCC =0;
for(inti =1; I <= N; i++)
if(Tim[i] = =-1)
Tarjan (i);
if(CNT_SCC = =1)
Puts"Yes");
Else
Puts"No");
}
voidGetmap ()
{
intA, B;
memset (Instack,0,sizeof(Instack));
memset (Low,0,sizeof(low));
Memset (Tim,-1,sizeof(Tim));
CNT =0;
Memset (Head,-1,sizeof(head));
while(m--)
{
scanf"%d%d", &a, &b);
Add (A, b);
}
}
intMain ()
{
while(~SCANF ("%d%d", &n, &m), n+m)
{
Getmap ();
Solve ();
}
return0;} Mygod
1 Great God Analysis (Scc_tarjan): 2 http://blog.sina.com.cn/s/blog_7a1746820100vtk9.html3 http:// Www.cnblogs.com/saltless/archive/2010/11/08/1871430.html
Paste A and check the set of practices, and find a bit useful. Two and check set, a reverse processing has a forward edge.
#include <cstdio>
#include <cstring>
#include <iostream>
usingnamespaceStd
intfather[3][10001];
intFindintAintI
{
if(Father[i][a] = = a)
returnA
Else
returnFind (Father[i][a], i);
}
intN, M;
voidMercyintAintb
{
intP, Q;
if(A! = N)//Important;
{
P = Find (A,0);
Q = Find (b,0);
if(P! = Q)
father[0][a] = b;//Detail;
}
if(b! = N)
{
P = Find (A,1);
Q = Find (b,1);
if(P! = Q)
father[1][B] = A;
}
}
intMain ()
{
while(~SCANF ("%d%d", &n, &m), n+m)
{
for(inti =1; I <= N; i++)
father[0][i] = father[1][i] = i;
for(inti =1; I <= m; i++)
{
intA, B;
scanf"%d%d", &a, &b);
Mercy (A, b);
}
BOOLFlag =true;
for(inti =1; I <= N; i++)
{
if(Find (I,0)! = N | | Find (I,1)! = N)
{
Flag =false;
Break;
}
}
if(flag)
printf"yes\n");
Else
printf"no\n");
}
return0;
}
Hangzhou Electric 1269--Maze Castle (strong connected components)