-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwc_2018_c4_s1.cpp
79 lines (71 loc) · 1.34 KB
/
wc_2018_c4_s1.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
#include <algorithm>
#include <numeric>
#include <iterator>
#include <iostream>
#include <cstring>
#include <bitset>
#include <deque>
#include <vector>
#include <string>
#include <cmath>
#include <map>
#include <set>
#include <unordered_set>
#include <queue>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int,int> vii;
typedef vector<long long,long long> vll;
typedef unordered_set<int> uset;
int p[100001],r[100001];
int findU(int m) {
if (p[m] != m){
p[m] = findU(p[m]);
}
return p[m];
}
void link(int x, int y){
if(r[x] > r[y]) {
p[y] = x;
}
else{
p[x] = y;
if(r[x] == r[y]){
r[x]++;
}
}
}
void un(int x, int y) {
link(findU(x),findU(y));
}
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
int N,M,K;
string plt;
cin >> N >> M >> K >> plt;
for(int i = 0; i < 100001; i++) {
p[i] = i;
r[i] = 0;
}
for(int i = 0; i < M; i++) {
int A,B;
cin >> A >> B;
A--;
B--;
if(plt[A] == plt[B]) {
// cout << A << " ~ " << B << "\n";
un(A,B);
}
}
int cnt = 0;
for(int i = 0; i < K; i++) {
int x,y;
cin >> x >> y;
x--;
y--;
if(findU(x) == findU(y)) cnt++;
}
cout << cnt;
}