-
Notifications
You must be signed in to change notification settings - Fork 0
/
13418_학교탐방하기.cpp
59 lines (57 loc) · 1.2 KB
/
13418_학교탐방하기.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
#include <bits/stdc++.h>
#define endl "\n"
#define ll long long
#define INF 987654321
#define MAX 1001
#define int ll
using namespace std;
typedef pair<int, int> p;
struct coords{
int a, b, c;
};
int N, M;
vector<coords> graph;
int parent[MAX];
bool cmp1(coords a, coords b){
return a.c < b.c;
}
bool cmp2(coords a, coords b){
return a.c > b.c;
}
int find(int u){
if(u == parent[u]) return u;
return parent[u] = find(parent[u]);
}
void merge(int u, int v){
u = find(u); v = find(v);
if(u == v) return;
parent[u] = v;
}
int kruskal(){
for(int i = 0; i <= N; ++i)
parent[i] = i;
int ans = 0, cnt = 0;
for(auto a : graph){
if(find(a.a) != find(a.b)){
merge(a.a, a.b);
if(a.c == 0) ans++;
if(++cnt == N) break;
}
}
return ans * ans;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> N >> M;
for(int i = 0; i < M + 1; ++i){
int a, b, c; cin >> a >> b >> c;
graph.push_back({a, b, c});
}
sort(graph.begin(), graph.end(), cmp1);
int ans = kruskal();
sort(graph.begin(), graph.end(), cmp2);
ans -= kruskal();
cout << ans << endl;
return 0;
}