标签:
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 39739 Accepted Submission(s): 21091
#include<stdio.h>
#include<string.h>
#define INF 9999999
int map[10000][10000],vis[1005],c[1005];
int n,m;
void Dfs(int x)
{
int i;
vis[x]=1;
for(i=1;i<=n;i++)
if(!vis[i]&&map[x][i]==1)
Dfs(i);
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF&&n!=0)
{
int i,j,k=0,a,b;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
map[i][j]=INF;
memset(vis,0,sizeof(vis));
memset(c,0,sizeof(c));
for(i=1; i<=m; i++)
{
scanf("%d%d",&a,&b);
map[a][b]=map[b][a]=1;
}
for(i=1;i<=n;i++)
{
if(vis[i]==0)
{
k++;
Dfs(i);
}
}
printf("%d\n",k-1);
}
return 0;
}
标签:
原文地址:http://www.cnblogs.com/-lgh/p/4961894.html