# HDU1269 迷宫城堡

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2092    Accepted Submission(s): 886

Problem Description

Input

Output

Sample Input
3 3 1 2 2 3 3 1 3 3 1 2 2 3 3 2 0 0

Sample Output
Yes No

*********************************************************************

#include <stdio.h>
#include <string.h>
#include <vector>
#include <stack>
#define MIN(a,b) ((a)<(b)?(a):(b))
#define N 10005
using namespace std;

stack<int>sta;
vector<int>gra[N];
int dfn[N],low[N],now,vis[N],sum;
int n,m;

void ini(void)
{
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(vis,0,sizeof(vis));
now=sum=0;
for(int i=1;i<=n;i++)
gra[i].clear();
while(sta.empty()==0)
sta.pop();
}

void tarjan(int s,int p)
{
vis[s]=2;
dfn[s]=low[s]=++now;
sta.push(s);
for(int i=0;i<gra[s].size();i++)
{
int t=gra[s][i];
if(!dfn[t])
tarjan(t,s),low[s]=MIN(low[s],low[t]);
else
if(vis[t]==2)
low[s]=MIN(low[s],dfn[t]);
}
if(low[s]==dfn[s])
{
sum++;
while(!sta.empty())
{
int t=sta.top();
sta.pop();
vis[t]=1;
if(t==s)break;
}
}
}

void re(void)
{
for(int i=1;i<=m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
gra[a].push_back(b);
}
}

void run(void)
{
for(int i=1;i<=n;i++)
if(!dfn[i])
tarjan(i,0);
if(sum>1)
puts("No");
else
puts("Yes");
}

int main()
{
while(scanf("%d%d",&n,&m),n+m)
{
ini();
re();
run();
}
}


posted on 2011-09-19 21:08  Fatedayt  阅读(2242)  评论(0编辑  收藏