-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graph1.cpp
80 lines (68 loc) · 1.5 KB
/
Graph1.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Rodrigo Farias de Macêdo
//string::empty
#include <string>
#include <iostream>
#include <cstdio>
#include <stdlib.h>
#include <sstream>
#include <cctype>
#include <algorithm>
#include <vector>
#include <utility>
#include <iterator>
#include <math.h>
#include <set>
using namespace std;
int mat[26][26];
void dfs(int u){
if(mat[u][u] == 2);
else{
mat[u][u] = 2;
for (int i = 0; i < 26; i++){
if(mat[u][i] == 1){
dfs(i);
}
}
}
}
int main(){
int cases;
cin >>cases;
while(cases--){
cout << endl;
char v;
cin >> v;
int n = v-64, a, b;
for (int i = 0; i< n; i++){
for (int j = 0; j< n; j++){
mat[i][j] = 0;
}
}
while(true){
std::string line;
std::getline(std::cin, line);
if(line.empty())break;
a = line[0]-64; b = line[1] - 64;
mat[a-1][b-1] = 1;
mat[b-1][a-1] = 1;
}
// for (int i = 0; i< n; i++){
// for (int j = 0; j< n; j++){
// cout << mat[i][j]<< " ";
// }
// cout << endl;
// }
// cout << n << " n\n";
for(int i = 0; i < n; i++){
mat[i][i] = 0;
}
int c = 0;
for (int i = 0; i < n; i++){
if(mat[i][i] == 0){
c++;
dfs(i);
}
}
cout<<"\n" << c << endl;
}
}