-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUVA-459.cpp
More file actions
109 lines (86 loc) · 1.54 KB
/
UVA-459.cpp
File metadata and controls
109 lines (86 loc) · 1.54 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include<bits/stdc++.h>
#define MAXN 300
#define L 1000
using namespace std;
typedef struct{
int matrix[MAXN][MAXN];
int numEdge;
int mark[MAXN];
}G;
int n;
queue<int> q;
G create_graph(){
G graph;
memset(graph.matrix, 0, sizeof(graph.matrix));
graph.numEdge = 0;
memset(graph.mark, 0, sizeof(graph.mark));
return graph;
}
int first(G g, int v){
for(int i = 0; i < n; i++){
if(g.matrix[v][i] != 0) return i;
}
return n;
}
int next(G g, int v, int w){
for(int i = w+1; i < n; i++){
if(g.matrix[v][i] != 0) return i;
}
return n;
}
int deq(){
int i = q.front();
q.pop();
return i;
}
void BFS(G *g, int start){
q = queue<int>();
q.push(start);
g->mark[start] = 1;
while(q.size() > 0){
int v = deq();
int w = first(*g, v);
while(w < n){
if(g->mark[w] == 0){
g->mark[w] = 1;
q.push(w);
}
w = next(*g, v, w);
}
}
}
int graphTraverse(G g){
int resp = 0;
for (int v = 0; v < n; v++){
g.mark[v] = 0;
}
for(int v = 0; v < n; v++){
if(g.mark[v] == 0){
resp++;
BFS(&g, v);
}
}
return resp;
}
int main(){
ios::sync_with_stdio(0); cin.tie(nullptr);
int t;
char c;
string arestas;
cin >> t; cin.ignore();
getline(cin, arestas);
for(int i = 0; i < t; i++){
G graph = create_graph();
cin >> c; cin.ignore();
n = c - 'A' + 1;
while(getline(cin, arestas) && arestas != ""){
int u = arestas[0] - 'A';
int v = arestas[1] - 'A';
graph.matrix[u][v] = 1;
graph.matrix[v][u] = 1;
}
cout << graphTraverse(graph) << endl;
if(i != t - 1) cout << endl;
}
return 0 ;
}