#include <iostream>
#include <vector>
#include <queue>
#include <memory.h>
using namespace std;
const int MAXN = 1005;
bool visited[MAXN];
vector<int> g[MAXN];
void bfs(int u)
{
visited[u] = true;
queue<int> q;
q.push(u);
while(!q.empty())
{
int v = q.front();
q.pop();
for(int i = 0;i < g[v].size();i++)
{
if(!visited[g[v][i]])
{
visited[g[v][i]] = true;
q.push(g[v][i]);
}
}
}
}
int main()
{
int n,m;
while(cin >> n >> m && n != 0)
{
for(int i = 0;i < MAXN;i++)
g[i].clear();
for(int i = 0;i < m;i++)
{
int x,y;
cin >> x >> y;
x--;
y--;
g[x].push_back(y);
g[y].push_back(x);
}
memset(visited,false,sizeof(visited));
int result = 0;
for(int i = 0;i < n;i++)
{
if(!visited[i])
{
result++;
bfs(i);
}
}
cout << result << endl;
}
return 0;
}
sicily connected components in undiected graph
最新推荐文章于 2015-09-24 11:20:25 发布