迷宮城堡
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17917 Accepted Submission(s): 7852
Problem Description
爲了訓練小希的方向感,Gardon建立了一座大城堡,裏面有N個房間(N<=10000)和M條通道(M<=100000),每個通道都是單向的,就是說若稱某通道連通了A房間和B房間,只說明可以通過這個通道由A房間到達B房間,但並不說明通過它可以由B房間到達A房間。Gardon需要請你寫個程序確認一下是否任意兩個房間都是相互連通的,即:對於任意的i和j,至少存在一條路徑可以從房間i到房間j,也存在一條路徑可以從房間j到房間i。
Input
輸入包含多組數據,輸入的第一行有兩個數:N和M,接下來的M行每行有兩個數a和b,表示了一條通道可以從A房間來到B房間。文件最後以兩個0結束。
Output
對於輸入的每組數據,如果任意兩個房間都是相互連接的,輸出”Yes”,否則輸出”No”。
Sample Input
3 3
1 2
2 3
3 1
3 3
1 2
2 3
3 2
0 0
Sample Output
Yes
No
Author
Gardon
Source
HDU 2006-4 Programming Contest
Recommend
lxj
用來試試自己寫的模板
Tarjan:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#define maxn 10010
using namespace std;
int vis[maxn];
int dfn[maxn];
int low[maxn];
int s[maxn];
int dfncnt=0;
int top=0;
int cnt=0;
int col[maxn];
int colcnt=0;
int head[maxn];
struct node
{
int to,next;
}e[100010];
void tarjan(int x)
{
dfn[x]=++cnt;
vis[x]=1;
low[x]=dfn[x];
s[top++]=x;
for(int i=head[x];i!=-1;i=e[i].next)
{
int to=e[i].to;
if(!dfn[to])
{
tarjan(to);
low[x]=min(low[x],low[to]);
}
else if(vis[to]) low[x]=min(low[x],dfn[to]);
}
if(low[x]==dfn[x])
{
col[x]=++colcnt;
vis[x]=0;
while(s[top-1]!=x)
{
vis[s[top-1]]=0;
col[s[top-1]]=colcnt;
top--;
}
top--;
}
}
void add(int a,int b)
{
e[cnt].to=b;
e[cnt].next=head[a];
head[a]=cnt++;
}
void init(int n)
{
memset(vis,0,sizeof(vis));
memset(s,0,sizeof(s));
memset(head,-1,sizeof(head));
memset(col,0,sizeof(col));
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
cnt=0;
top=0;
dfncnt=0;
colcnt=0;
}
int main()
{
int m,n;
int a,b;
while(scanf("%d%d",&n,&m)==2)
{
if(m==0&&n==0)break;
init(n);
for(int i=0;i<m;i++)
{
scanf("%d%d",&a,&b);
add(a,b);
}
bool flag=1;
for(int i=1;i<=n;i++)
{
if(!dfn[i])
tarjan(i);
}
if(colcnt==1)puts("Yes");
else puts("No");
}
return 0;
}
Kosaraju:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#define maxn 10010
using namespace std;
vector<int> g[maxn];
vector<int> ng[maxn];
vector<int> s;
int vis[maxn];
int scc[maxn];
int cnt;
void dfs1(int x)
{
if(vis[x])return;
vis[x]=1;
for(int i=0;i<g[x].size();i++)
{
dfs1(g[x][i]);
}
s.push_back(x);
}
void dfs2(int x)
{
if(scc[x])return;
scc[x]=cnt;
for(int i=0;i<ng[x].size();i++)
{
dfs2(ng[x][i]);
}
}
int kosaraju(int n)
{
memset(vis,0,sizeof(vis));
memset(scc,0,sizeof(scc));
for(int i=1;i<=n;i++)
{
dfs1(i);
}
cnt=0;
for(int i=n-1;i>=0;i--)
{
if(!scc[s[i]])
{
cnt++;
dfs2(s[i]);
if(cnt>1)return 0;
}
}
return 1;
}
void init(int n)
{
for(int i=0;i<=n;i++)
{
g[i].clear();
ng[i].clear();
}
s.clear();
}
int main()
{
int n,m;
int a;
while(scanf("%d%d",&n,&m)==2)
{
if(m==0&&n==0)break;
init(n);
int a,b;
for(int i=0;i<m;i++)
{
scanf("%d%d",&a,&b);
g[a].push_back(b);
ng[b].push_back(a);
}
if(kosaraju(n))puts("Yes");
else puts("No");
}
return 0;
}
還是tarjan比較快