-
Notifications
You must be signed in to change notification settings - Fork 0
/
1082.cpp
55 lines (46 loc) · 1.06 KB
/
1082.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
#include <bits/stdc++.h>
using namespace std;
void dfs(set<int> g[], vector<bool> &visited, int u, set<int> &out)
{
visited[u] = true;
out.insert(u);
for (auto v : g[u])
{
if (!visited[v])
dfs(g, visited, v, out);
}
}
int main()
{
int casos;
scanf("%d", &casos);
for (int c = 1; c <= casos; c++)
{
printf("Case #%d:\n", c);
int n, e;
scanf("%d%d", &n, &e);
vector<bool> visited(n, false);
set<int> g[n];
for (int i = 0; i < e; i++)
{
char u, v;
cin >> u >> v;
g[u - 'a'].insert(v - 'a');
g[v - 'a'].insert(u - 'a');
}
int ccc = 0;
for (int u = 0; u < n; u++)
{
set<int> out;
if (!visited[u])
{
ccc++;
dfs(g, visited, u, out);
for (auto ee : out)
printf("%c,", ee + 'a');
printf("\n");
}
}
printf("%d connected components\n\n", ccc);
}
}