-
Notifications
You must be signed in to change notification settings - Fork 0
/
2ndBestMST.cpp
88 lines (82 loc) · 2.22 KB
/
2ndBestMST.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
#include<iostream>
#include<vector>
#include<cstring>
using namespace std;
const int nax = (int)1e3 + 5;
int par[nax][23], mx[nax][23], level[nax];
int LG;
vector<pair<int, int> > g[nax];
void dfs(int s, int p, int w) {
par[s][0] = p, mx[s][0] = w;
for (int lg=1; lg<LG; lg++) {
if (par[s][lg-1] != 0) {
par[s][lg] = par[par[s][lg-1]][lg-1];
mx[s][lg] = max(mx[s][lg], max(mx[par[s][lg-1]][lg-1], mx[s][lg-1]));
// cout << (1<<lg) << "th parent of " << s << " is " << par[s][lg] << "\n";
// cout << "max from " << s << " to " << par[s][lg] << " is " << mx[s][lg] << "\n";
}
}
for (auto& x: g[s]) {
int v = x.first, w_ = x.second;
if (p != v) {
level[v] = level[s] + 1;
dfs(v, s, w_);
}
}
}
int lca(int u, int v) {
if (level[u] < level[v]) swap(u, v);
// cout << level[u] << " " << level[v] << "\n";
int diff = level[u] - level[v];
for (int i=LG; i>=0; i--) {
if (diff & (1<<i)) {
u = par[u][i];
}
}
if(u == v) return u;
for (int i=LG; i>=0; i--) {
if (par[u][i] != par[v][i]) {
u = par[u][i], v = par[v][i];
}
}
return par[u][0];
}
// u -> from, v -> to
int max_query(int u, int v) {
if (level[u] < level[v]) swap(u, v);
int diff = level[u] - level[v];
int ans = 0;
for (int i=LG; i>=0; i--) {
if (diff & (1<<i)) {
ans = max(ans, mx[u][i]);
u = par[u][i];
}
}
if(u == v) return ans;
for (int i=LG; i>=0; i--) {
if (par[u][i] != par[v][i]) {
ans = max(ans, max(mx[u][i], mx[v][i]));
u = par[u][i], v = par[v][i];
}
}
return max(ans, mx[u][0]);
}
int main() {
int n, m, u, v, w;
cin >> n >> m;
for (int i=0; i<m; i++) {
cin >> u >> v >> w;
g[u].push_back(make_pair(v, w));
g[v].push_back(make_pair(u, w));
}
while ((1 << LG) < n) LG += 1;
dfs(1, 0, 0);
cin >> u >> v;
while (u != -1) {
int _lca = lca(u, v);
int ans = max(max_query(u, _lca), max_query(v, _lca));
cout << _lca << " max: " << ans << "\n";
cin >> u >> v;
}
return 0;
}