标签:des style blog class code c
3 3 1 2 2 3 3 1 3 3 1 2 2 3 3 2 0 0
Yes No
#include<stdio.h>
#include<iostream>
#include<malloc.h>
using namespace std;
typedef struct nnn
{
int u;
struct nnn *next;
}*List,list;
List linkList[10005];
int stack[10005],sn;
int dfn[10005],low[10005],vist[10005],deep,flog,n;
void addNode(List &p,int uu)
{
List q=(List)malloc(sizeof(list));
q->u=uu;
q->next=p->next;
p->next=q;
}
void DFS(int v)
{
List p=linkList[v]->next;
deep++; vist[v]=1;
stack[++sn]=v;
dfn[v]=low[v]=deep;
while(p!=NULL)
{
int u=p->u;
if(vist[u]==0)
{
DFS(u);
low[v]=(low[v]<low[u])?low[v]:low[u];
if(flog) return ;
}
else low[v]=(low[v]<dfn[u])?low[v]:dfn[u];
p=p->next;
}
if(low[v]==dfn[v])//表示在栈中的当前点到栈顶的点组成一个强连通
{
if(v!=1)flog=1;//如果v!=1表明可能有多个强连通
else if(sn!=n)flog=1;//v=1了但是这个强连通中点的个数为sn个
}
}
int main()
{
int m,v,u;
while(scanf("%d%d",&n,&m)>0&&n+m!=0)
{
for(int i=1; i<=n; i++)
{
linkList[i]=(List)malloc(sizeof(list));
vist[i]=0; linkList[i]->next=NULL;
}
while(m--)
{
scanf("%d%d",&v,&u);
addNode(linkList[v],u);
}
flog=0; deep=0; sn=0;
DFS(1);
if(flog)printf("No\n");
else printf("Yes\n");
}
}
hdu1269迷宫城堡 (强连通Tarjan+邻接表),布布扣,bubuko.com
标签:des style blog class code c
原文地址:http://blog.csdn.net/u010372095/article/details/25743747