-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy path1082 - Connected Components.cpp
57 lines (47 loc) · 1.04 KB
/
1082 - Connected Components.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <stdio.h>
#include <memory.h>
#include <vector>
#include <algorithm>
using namespace std;
bool vis[26];
vector<vector<int> > g, comps;
vector<int> tmp;
void DFS(int node) {
tmp.push_back(node);
vis[node] = true;
for(int i = 0; i < (int)g[node].size(); ++i)
if(!vis[g[node][i]])
DFS(g[node][i]);
}
int main() {
int t;
scanf("%d", &t);
int n, m;
char a, b;
for(int cnt = 1; t-- != 0; ++cnt) {
scanf("%d %d", &n, &m);
g.clear();
g.resize(n);
for(int i = 0; i < m; ++i) {
scanf(" %c %c", &a, &b);
g[a - 'a'].push_back(b - 'a');
g[b - 'a'].push_back(a - 'a');
}
memset(vis, false, sizeof vis);
comps.clear();
for(int i = 0; i < n; ++i)
if(!vis[i]) {
tmp.clear();
DFS(i);
comps.push_back(tmp);
}
printf("Case #%d:\n", cnt);
for(int i = 0; i < (int)comps.size(); ++i, puts("")) {
sort(comps[i].begin(), comps[i].end());
for(int j = 0; j < (int)comps[i].size(); ++j)
printf("%c,", char(comps[i][j] + 'a'));
}
printf("%d connected components\n\n", (int)comps.size());
}
return 0;
}