标签:des blog io os sp for 数据 div log
2 3 1 1 2 3 2 3 2 1 2
2 1
#include <iostream> #include <string> #include <stdio.h> #include <string.h> #define INF 99999999 using namespace std; int map[50][50]; int vis[50]; int n; void dfs(int dd) { int j; for(j=1; j<=n; j++) { if(!vis[j] && map[dd][j]==1 ) { vis[j]=1; dfs(j); } } } int main() { int t; cin>>t; int m; int u, v; int cnt; int i; while(t--) { cin>>n>>m; memset(map, 0, sizeof(map)); memset(vis, 0, sizeof(vis)); cnt=0; while(m--) { cin>>u>>v; map[u][v]=1; map[v][u]=1; } for(i=1; i<=n; i++) { if(!vis[i] ) { dfs(i); cnt++; } } cout<<cnt<<endl; } return 0; }
标签:des blog io os sp for 数据 div log
原文地址:http://www.cnblogs.com/yspworld/p/4093627.html