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
判断图是否是完全连通的。
以任何一个点为根正负边各自建一个图,然后遍历记录即可。
#include<stdio.h>
int f[10001][300],z[10001][300];
int ff[10001],zz[10001];
int ss1(int x)
{
int i;
for (i=1;i<=f[x][0];i++)
if(ff[f[x][i]])
{
ff[f[x][i]]=0;
ss1(f[x][i]);
}
return 0;
}
int ss2(int x)
{
int i;
for (i=1;i<=z[x][0];i++)
if(zz[z[x][i]])
{
zz[z[x][i]]=0;
ss2(z[x][i]);
}
return 0;
}
int main(){
int i,n,m,a,b,w;
while (1)
{
scanf("%d%d",&n,&m);
if (n==0&&m==0) break;
for (i=1;i<=n;i++)
{
f[i][0]=0;z[i][0]=0;
ff[i]=1; zz[i]=1;
}
for (i=1;i<=m;i++)
{
scanf("%d%d",&a,&b);
f[a][++f[a][0]]=b;
z[b][++z[b][0]]=a;
}
ff[1]=0; zz[1]=0;
ss1(1); ss2(1);
for (w=0,i=1;i<=n;i++) if (ff[i]||zz[i]) w=1;
if (w==0) printf("Yes\n"); else printf("No\n");
}
return 0;
}
用vector更加简便。
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> f[2][10005];
int c[10005];
void dfs(int x,int u)
{
c[x]=1;
for (int i=0;i<f[u][x].size();i++)
if (!c[f[u][x][i]]) dfs(f[u][x][i],u);
}
int main()
{
int n,m;
while (scanf("%d%d",&n,&m),n+m){
for (int i=1;i<=n;i++) f[0][i].clear(),f[1][i].clear();
for (int i=0;i<m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
f[0][x].push_back(y);
f[1][y].push_back(x);
}
int ff=1;
memset(c,0,sizeof(c));
dfs(1,0);
for (int i=1;i<=n;i++) if (c[i]!=1) {ff=0; break;}
memset(c,0,sizeof(c));
dfs(1,1);
for (int i=1;i<=n;i++) if (c[i]!=1) {ff=0; break;}
if (ff) printf("Yes\n"); else printf("No\n");
}
return 0;
}
强连通的tarjan算法。
#include<stack>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<vector>
using namespace std;
const int maxn = 10005;
int ff, t, tot, n, m, x, y, dfn[maxn], low[maxn], ins[maxn], fa[maxn];
vector<int> tree[maxn];
stack<int> p;
void dfs(int x)
{
int j;
low[x] = dfn[x] = ++tot;
ins[x] = 1; p.push(x);
for (int i = 0; i < tree[x].size(); i++)
{
j = tree[x][i];
if (!dfn[j])
{
dfs(j);
low[x] = min(low[x], low[j]);
}
else if (ins[j]) low[x] = min(dfn[j], low[x]);
}
if (dfn[x] == low[x])
{
t++;
do{
j = p.top();
p.pop();
ins[j] = 0;
fa[j] = t;
} while (j != x);
}
}
int main()
{
while (~scanf("%d%d", &n, &m), n + m)
{
for (int i = 1; i <= n; i++) tree[i].clear();
while (m--)
{
scanf("%d%d", &x, &y);
tree[x].push_back(y);
}
memset(dfn, 0, sizeof(dfn));
memset(fa, 0, sizeof(fa));
memset(ins, 0, sizeof(ins));
tot = t = 0; dfs(1);
for (int i = ff = 1; i <= n; i++) if (fa[i] != fa[1]) { ff = 0; break; }
if (ff) printf("Yes\n"); else printf("No\n");
}
return 0;
}
还可以这样写
#include<stack>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<vector>
using namespace std;
const int maxn = 10005;
int t, tot, n, m, x, y, dfn[maxn], low[maxn], ins[maxn];
vector<int> tree[maxn];
stack<int> p;
void dfs(int x)
{
low[x] = dfn[x] = ++tot;
ins[x] = 1; p.push(x);
for (int i = 0; i < tree[x].size(); i++)
{
int y = tree[x][i], j;
if (!ins[y])
{
dfs(y);
low[x] = min(low[x], low[y]);
if (dfn[x] < low[y])
{
t++;
do{
j = p.top();
p.pop();
} while (j != y);
}
}
else low[x] = min(dfn[y], low[x]);
}
}
int main()
{
while (~scanf("%d%d", &n, &m), n + m)
{
while (!p.empty()) p.pop();
for (int i = 1; i <= n; i++) tree[i].clear();
while (m--)
{
scanf("%d%d", &x, &y);
tree[x].push_back(y);
}
memset(ins, 0, sizeof(ins));
tot = t = 0; dfs(1);
for (int i = 1; i <= n; i++) if (!ins[i]) t++;
if (!t) printf("Yes\n"); else printf("No\n");
}
return 0;
}