题目
求一个图的连通分量
[外链图片转存失败(img-irsdLZKj-1562483374877)(http://10.156.17.250/JudgeOnline/image/1759.jpg)]
输入
n 顶点数(<=100)
边
输出
连通分量
输入样例
8
6 3
1 2
2 5
5 4
4 1
8 7
0 0
输出样例
4
解题思路
深搜
从i点开始访问,输出并且标记,由此重复
当i的邻接点搜完后继续下一个点搜
广搜
从i点开始访问,再不断的从下一个点继续访问,访问到全部访问完为止
程序如下
程序解析到时更新
请谅解
深 搜 邻 接 矩 阵 深搜 邻接矩阵 深搜邻接矩阵
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n,x,y,a[1001][1001],p[1001],c,ans;
int dfs(int w)
{
p[w]=1;
int fg=1;
for(int i=1;i<=n;i++)
{
if(a[w][i]&&!p[i])
{
fg+=dfs(i);
}
}
return fg;
}
struct s
{
int y,next;
}q[10001];
int main()
{
scanf("%d",&n);
scanf("%d%d",&x,&y);
while(x&&y)
{
a[x][y]=1;
a[y][x]=1;
scanf("%d%d",&x,&y);
}
for(int i=1;i<=n;i++)
{
ans=max(ans,dfs(i));
}
printf("%d",ans);
return 0;
}
深 搜 邻 接 表 深搜邻接表 深搜邻接表
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n,x,y,a[1001][1001],p[1001],ans,k,g[1001];
struct s
{
int t,next;
}q[10001];
int dfs(int w)
{
p[w]=1;
int h=1;
for(int i=g[w];i;i=q[i].next)
{
if(!p[q[i].t])
{
h+=dfs(q[i].t);
}
}
return h;
}
int main()
{
scanf("%d",&n);
scanf("%d%d",&x,&y);
while(x&&y)
{
q[++k].t=y;
q[k].next=g[x];
g[x]=k;
q[++k].t=x;
q[k].next=g[y];
g[y]=k;
scanf("%d%d",&x,&y);
}
for(int i=1;i<=n;i++)
{
if(!p[i])
{
ans=max(ans,dfs(i));
}
}
printf("%d",ans);
return 0;
}
广 搜 邻 接 矩 阵 广搜 邻接矩阵 广搜邻接矩阵
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n,x,y,a[1001][1001],p[1001],ans;
struct s
{
int y,next;
}q[10001];
int bfs(int i)
{
int head=0,tail=1,stat[101],h,gg=1;
memset(stat,0,sizeof(stat));
stat[1]=i;
p[i]=1;
do
{
head++;
for(int j=1;j<=n;j++)
{
if(a[stat[head]][j]&&!p[j])
{
tail++;
gg++;
p[j]=1;
stat[tail]=j;
}
}
}while(head<=tail);
return gg;
}
int main()
{
scanf("%d",&n) ;
scanf("%d%d",&x,&y);
while(x&&y)
{
a[x][y]=1;
a[y][x]=1;
scanf("%d%d",&x,&y);
}
for(int i=1;i<=n;i++)
{
if(!p[i])
{
ans=max(ans,bfs(i));
}
}
printf("%d",ans);
return 0;
}
广 搜 邻 接 表 广搜邻接表 广搜邻接表
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n,x,y,a[1001][1001],p[1001],ans,k,g[1001];
struct s
{
int t,next;
}q[10001];
int bfs(int i)
{
int head=0,tail=1,stat[101],gg;
memset(stat,0,sizeof(stat));
stat[1]=i;
p[i]=1;
do
{
++head;
for(int j=g[stat[head]];j;j=q[j].next)
{
if(!p[q[j].t])
{
p[q[j].t]=1;
stat[++tail]=q[j].t;
}
}
}while(head<=tail);
return tail;
}
int main()
{
scanf("%d",&n);
scanf("%d%d",&x,&y);
while(x&&y)
{
q[++k].t=y;
q[k].next=g[x];
g[x]=k;
q[++k].t=x;
q[k].next=g[y];
g[y]=k;
scanf("%d%d",&x,&y);
}
for(int i=1;i<=n;i++)
{
if(!p[i])
{
ans=max(ans,bfs(i));
}
}
printf("%d",ans);
return 0;
}
广 搜 邻 接 表 + S T L 广搜邻接表+STL 广搜邻接表+STL
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
int n,x,y,a[1001][1001],p[1001],ans,k,g[1001];
struct s
{
int t,next;
}q[10001];
int bfs(int i)
{
int head=0,tail=1,gg;
p[i]=1;
queue<int>s;
s.push(i);
while(s.size())
{
int w=s.front();
s.pop();
for(int j=g[w];j;j=q[j].next)
{
if(!p[q[j].t])
{
tail++;
p[q[j].t]=1;
s.push(q[j].t);
}
}
}
return tail;
}
int main()
{
scanf("%d",&n);
scanf("%d%d",&x,&y);
while(x&&y)
{
q[++k].t=y;
q[k].next=g[x];
g[x]=k;
q[++k].t=x;
q[k].next=g[y];
g[y]=k;
scanf("%d%d",&x,&y);
}
for(int i=1;i<=n;i++)
{
if(!p[i])
{
ans=max(ans,bfs(i));
}
}
printf("%d",ans);
return 0;
}

本文介绍了一种计算图中连通分量的方法,包括深度优先搜索(DFS)和广度优先搜索(BFS),并提供了使用邻接矩阵和邻接表实现的具体代码示例。
7480

被折叠的 条评论
为什么被折叠?



