2019年南京工业大学828数据结构第三题
程序员文章站
2022-05-15 14:02:22
...
#include <iostream>
using namespace std;
int map[105][105];
int vis[105];
int n, m;
void Dfs(int pos)
{
vis[pos] = 1;
for (int i = 1; i <= n; i++)
{
if (map[pos][i] == 1 && vis[i] == 0)
{
Dfs(i);
}
}
}
int main()
{
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
vis[i] = 0;
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (i == j)
{
map[i][j] = 1;
}
else
{
map[i][j] = 0;
}
}
}
for (int i = 0; i < m; i++)
{
int s, d;
cin >> s >> d;
map[s][d] = 1;
map[d][s] = 1;
}
int cnt = 0;
for (int i = 1; i <= n; i++)
{
if (vis[i] == 0)
{
Dfs(i);
cnt++;
}
}
cout << cnt << endl;
return 0;
}
上一篇: HDU3072(Intelligence System)
下一篇: Tarjan LCA