标签:des style blog color os io for div log
Description
Input
Output
Sample Input
2 3 1 1 2 3 2 3 2 1 2
Sample Output
2 1
1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <algorithm> 5 #include <math.h> 6 #include <queue> 7 8 using namespace std; 9 10 bool G[23][23]; 11 12 int LT(int n) { 13 int ans = 0; 14 bool vis[23]; 15 16 memset(vis, 0, sizeof(vis)); 17 for (int i = 1; i <=n; i++) { 18 queue<int > que; 19 if (vis[i]) 20 continue; 21 que.push(i); 22 while (que.empty() == false) { 23 int u = que.front(); 24 que.pop(); 25 for (int j = 1; j <=n; j++) { 26 if (vis[j] == 0 && G[u][j]) { 27 vis[j] = true; 28 que.push(j); 29 } 30 } 31 } 32 ans++; 33 } 34 return ans; 35 } 36 37 int main() { 38 int t; 39 int n, m; 40 41 scanf("%d", &t); 42 while (t-- && scanf("%d %d", &n, &m)) { 43 memset(G, 0, sizeof(G)); 44 for (int i = 1; i <=n; i++) 45 G[i][i] = true; 46 int u, v; 47 while (m--) { 48 scanf("%d %d", &u, &v); 49 G[u][v] = G[v][u] = true; 50 } 51 printf("%d\n", LT(n)); 52 } 53 return 0; 54 }
标签:des style blog color os io for div log
原文地址:http://www.cnblogs.com/Yan-C/p/3934225.html