#include<iostream>
using namespace std;
const int maxn=1000+5;
bool map[maxn][maxn],used[maxn];
int n,m;
void dfs(int u)
{
for(int v=1;v<=n;v++){
if(map[u][v]&&!used[v]){
used[v]=true;
dfs(v);
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(map,false,sizeof(map));
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
int u,v;
scanf("%d%d",&u,&v);
map[u][v]=map[v][u]=true;
}
int ans=0;
memset(used,false,sizeof(used));
for(int i=1;i<=n;i++){
if(!used[i]) {ans++;dfs(i);}
}
printf("%d\n",ans);
}
return 0;
}
HDU 1213 How many table(dfs)
最新推荐文章于 2022-11-07 14:02:50 发布