-
Notifications
You must be signed in to change notification settings - Fork 2
/
00459.cpp
62 lines (59 loc) · 1.02 KB
/
00459.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
57
58
59
60
61
62
#include<iostream>
#include<vector>
#include<stack>
using namespace std;
typedef vector<int> vi;
int main()
{
int T;
cin >> T;
cin.ignore();
bool first = true;
while(T--)
{
if(first) first = false;
else cout << endl;
cin.ignore();
int size = cin.get()-'A'+1;
char x;
vector<vi> edges(size);
vector<bool> visited(size, false);
cin.ignore();
while(cin.peek() != '\n' && cin.good())
{
int a = cin.get() - 'A';
int b = cin.get() - 'A';
/*
char x=a+'A', y=b+'A';
cout << x << y << endl;*/
cin.ignore();
edges[a].push_back(b);
edges[b].push_back(a);
}
int sets=0;
for(int i = 0; i < size; i++)
{
if(!visited[i])
{
sets++;
stack<int> S;
S.push(i);
while(S.size())
{
int src = S.top();
S.pop();
for(int j = 0; j < edges[src].size(); j++)
{
int dest = edges[src][j];
if(!visited[dest])
{
visited[dest] = true;
S.push(dest);
}
}
}
}
}
cout << sets << endl;
}
}