-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathmain.cpp
70 lines (59 loc) · 1.47 KB
/
main.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
// Authored by : tony9402
// Co-authored by : -
// Link : http://boj.kr/c8fbfa4f718847b8a169037608c2e8be
#include<bits/stdc++.h>
using namespace std;
int uf[1005], N, K;
vector<vector<pair<int, int>>> graph;
vector<tuple<int, int, int>> V;
int find(int x) {
if(uf[x] < 0)return x;
return uf[x] = find(uf[x]);
}
bool merge(int a, int b) {
a = find(a);
b = find(b);
if(a == b)return false;
uf[b] = a;
return true;
}
pair<int, int> BFS(int s) {
vector<int> visited(N);
queue<pair<int, int>> Q;
Q.emplace(s, 0);
visited[s] = true;
pair<int, int> ret = make_pair(0, 0); // [dist, node]
while(!Q.empty()){
auto [cur, dist] = Q.front(); Q.pop();
ret = max(ret, make_pair(dist, cur));
for(auto &[nxt, cost]: graph[cur]) {
if(visited[nxt]) continue;
visited[nxt] = true;
Q.emplace(nxt, dist + cost);
}
}
return ret;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
for(int i=0;i<1000;i++) uf[i] = -1;
cin >> N >> K;
graph.resize(N);
for(int i=0;i<K;i++) {
int a, b, c; cin >> a >> b >> c;
V.emplace_back(c, a, b);
}
sort(V.begin(), V.end());
int answer = 0;
for(auto &[c, a, b]: V){
if(merge(a, b)) {
answer += c;
graph[a].emplace_back(b, c);
graph[b].emplace_back(a, c);
}
}
cout << answer << '\n';
cout << BFS(BFS(0).second).first;
return 0;
}