-
Notifications
You must be signed in to change notification settings - Fork 0
/
14621.cpp
90 lines (78 loc) · 2.07 KB
/
14621.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
80
81
82
83
84
85
86
87
88
89
90
#include<bits/stdc++.h>
using namespace std;
int N, M, P[10'010], ans_c = 0;;
bool gender[1010];
vector<tuple<int, int, int>> e;
int Find(int v) {return v == P[v] ? v : P[v] = Find(P[v]);}
bool Union(int u, int v){ return Find(u) != Find(v) && (P[P[u]]=P[v], true); }
long long Kruskal(){
long long ret = 0;
for(int i = 1; i <= N; i++) P[i] = i;
sort(e.begin(), e.end());
for(auto [w, u, v] : e) if(Union(u, v)) { ret += w;ans_c++; }
return ret;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin>>N>>M;
//남초, 여초 구별
for(int i = 1; i <= N; i++) {
char c;cin>>c;
if(c == 'W') gender[i] = false;
else gender[i] = true;
}
//남초끼리 또는 여초끼리 연결된 간선은 볼 필요 없음
for(int i = 1; i <= M; i++) {
int a, b, w;cin>>a>>b>>w;
if(gender[a] != gender[b]) {
e.push_back(make_tuple(w, a, b));
}
}
int ans = Kruskal();
cout<<(ans_c < N - 1 ? -1 : ans);
return 0;
}
/*
#include<bits/stdc++.h>
using namespace std;
using PII = pair<int, int>;
vector<PII> g[10101];
int N, M, C[1010], ans_c = 0;
bool gender[1010];
int Prim(){
int ret = 0;
priority_queue<PII, vector<PII>, greater<>> pq;
C[1] = 1;
for(auto [i, w]: g[1]) pq.emplace(w, i);
while(!pq.empty()) {
auto [c, v] = pq.top(); pq.pop();
if(C[v]) continue;
C[v] = 1;ret+=c;ans_c++;
for(auto [i, w] : g[v]) pq.emplace(w, i);
}
return ret;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin>>N>>M;
//남초, 여초 구별
for(int i = 1; i <= N; i++) {
char c;cin>>c;
if(c == 'W') gender[i] = false;
else gender[i] = true;
}
//남초끼리 또는 여초끼리 연결된 간선은 볼 필요 없음
for(int i = 1; i <= M; i++) {
int a, b, w;cin>>a>>b>>w;
if(gender[a] != gender[b]) {
g[a].push_back({b, w});
g[b].push_back({a, w});
}
}
int ans = Prim();
cout<<(ans_c < N - 1 ? -1 : ans);
return 0;
}
*/