forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadjoin.cpp
99 lines (80 loc) · 2.21 KB
/
adjoin.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
91
92
93
94
95
96
97
98
99
#include <bits/stdc++.h>
using namespace std;
// Returns {node, dist}
pair<int,int> getfurthest(vector<vector<int>>& adj, int start) {
map<int,int> dist;
queue<int> q;
q.push(start);
dist[start] = 0;
int furthestdist = 0;
int furthestidx = start;
while(!q.empty()) {
int curr = q.front();
q.pop();
for(auto next : adj[curr]) {
if(dist.count(next) == 0) {
dist[next] = dist[curr] + 1;
q.push(next);
if(dist[next] > furthestdist) {
furthestdist = dist[next];
furthestidx = next;
}
}
}
}
return {furthestidx, furthestdist};
}
int getdiam(vector<vector<int>>& adj, int start) {
// Get furthest away, use that as start, get furthest away
pair<int,int> s = getfurthest(adj, start);
pair<int,int> t = getfurthest(adj, s.first);
return t.second;
}
int find(vector<int>& d, int a) {
if(d[a] == -1) {
return a;
}
return d[a] = find(d, d[a]);
}
void join(vector<int>& d, int a, int b) {
a = find(d, a);
b = find(d, b);
if(a == b) return;
d[a] = b;
}
int main() {
int n, m;
cin >> n >> m;
vector<int> d(n, -1);
vector<vector<int>> adj(n);
for(int i = 0; i < m; i++) {
int n1, n2;
cin >> n1 >> n2;
join(d, n1, n2);
adj[n1].push_back(n2);
adj[n2].push_back(n1);
}
vector<int> diameters;
for(int i = 0; i < n; i++) {
if(d[i] == -1) {
diameters.push_back(getdiam(adj, i));
}
}
sort(diameters.begin(), diameters.end());
while(diameters.size() > 1) {
// keep joining until done
int wing1 = diameters.back() / 2;
int wing2 = diameters.back() / 2;
if(diameters.back() % 2 == 1) wing1++;
diameters.pop_back();
int wing3 = diameters.back() / 2;
int wing4 = diameters.back() / 2;
if(diameters.back() % 2 == 1) wing3++;
diameters.pop_back();
int op1 = wing1 + wing2;
int op2 = wing3 + wing4;
int op3 = wing1 + wing3 + 1;
diameters.push_back(max({op1, op2, op3}));
}
cout << diameters[0] << endl;
}