-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4792.cpp
More file actions
89 lines (67 loc) · 1.57 KB
/
4792.cpp
File metadata and controls
89 lines (67 loc) · 1.57 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
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
#define MAXN 1050
int n,m,k,f,t,parent[MAXN];
char c;
bool chk;
struct edge{
int src, dst, val;
char color;
};
vector<edge> v;
bool cmp_less(edge a, edge b){
return a.val<b.val;
}
bool cmp_greater(edge a, edge b){
return a.val>b.val;
}
int find(int x){
if(parent[x] == x) return x;
return parent[x] = find(parent[x]);
}
void merge(int x, int y){
chk = false;
x = find(x);
y = find(y);
if(x == y) return;
parent[x] = y;
chk = true;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin >> n >> m >> k;
while(n){
int min=0, max=0;
for(int i=0 ; i<m ; i++){
edge e;
cin >> e.color >> e.src >> e.dst;
v.push_back(e);
}
for(int i=0 ; i<v.size() ; i++){
if(v[i].color == 'B') v[i].val = 1;
else v[i].val = 0;
}
// min
for(int i=0 ; i<=n ; i++) parent[i]=i;
sort(v.begin(), v.end(), cmp_less);
for(int i=0 ; i<m ; i++){
merge(v[i].src, v[i].dst);
if(chk) min += v[i].val;
}
// max
for(int i=0 ; i<=n ; i++) parent[i]=i;
sort(v.begin(), v.end(), cmp_greater);
for(int i=0 ; i<m ; i++){
merge(v[i].src, v[i].dst);
if(chk) max += v[i].val;
}
if(min <= k && k <= max) cout << 1 <<'\n';
else cout << 0 << '\n';
v.clear();
cin >> n >> m >> k;
}
}