-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path1082.cpp
More file actions
63 lines (55 loc) · 1.15 KB
/
1082.cpp
File metadata and controls
63 lines (55 loc) · 1.15 KB
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
57
58
59
60
61
62
63
#include<bits/stdc++.h>
using namespace std;
vector<char>comp;
vector<vector<int> > graph;
int vis[27];
void dfs(int u)
{
comp.push_back(u + 'a');
vis[u]=1;
for(int i=0;i<graph[u].size();i++)
{
int q=graph[u][i];
if(vis[q]==0)
{
dfs(q);
}
}
}
char a,b;
int main()
{
int n;cin >> n;
int k = 1;
while(n--)
{
int x,y;cin >> x >> y;
graph.assign(27,vector<int>());
memset(vis,0,sizeof vis);
for(int i=0;i<y;i++)
{
cin >> a >> b;
graph[a-'a'].push_back(b-'a');
graph[b-'a'].push_back(a-'a');
}
int ans=0;
printf("Case #%d:\n",k++);
for(int i=0;i<x;i++)
{
if(vis[i]==0)
{
ans++;
dfs(i);
sort(comp.begin(),comp.end());
for(int j=0;j<comp.size();j++)
{
cout << comp[j] << ",";
}
printf("\n");
comp.clear();
}
}
printf("%d connected components\n\n",ans);
}
return 0;
}