-
Notifications
You must be signed in to change notification settings - Fork 0
/
itineraries_v2.cpp
executable file
·101 lines (84 loc) · 2.26 KB
/
itineraries_v2.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
100
#include <iostream>
#include <vector>
#include <algorithm>
#include "MST.h"
using namespace std;
const int N = 2e5+5;
const int K = 25;
int T;
vector<pair<int,int>> adjList[N];
int up[K+2][N]; // sparse table of ancestors
int noise[K+2][N]; // sparse table of noises
int tin[N], tout[N]; // time_in, time_out
// preprocessing
void dfs(int v, int par){
tin[v] = T++;
for (auto [u,w] : adjList[v]) {
if (u == par) continue;
up[0][u] = v;
noise[0][u]= w;
dfs(u, v);
}
tout[v] = T;
}
// fill sparse table accordingly :
// * up[j][i] = 2^j-th ancestor of i
// * noise[j][i] = max noise between i and up[j][i]
void search_ancestor_and_maxnoise(int n){
up[0][0] = 0; // parent's root (0) is itself
for(int j = 1; j < K; j++){
for(int i = 0; i < n; i++){
up[j][i] = up[j - 1][up[j - 1][i]];
noise[j][i] = max(noise[j - 1][i], noise[j - 1][up[j - 1][i]]);
}
}
}
//true if "a" is an ancestor of "b"
bool upper (int a, int b) {
return tin[a] <= tin[b] && tout[a] >= tout[b];
}
//calculate path max noise between u and v
int query(int v, int u) {
int res = 0;
if (v == u) return 0; //
for (int l = K; l >= 0; l--){
if (!upper(up[l][v], u)){
res = max(res, noise[l][v]);
v = up[l][v];
}
}
for (int l = K; l >= 0; l--){
if (!upper(up[l][u], v)){
res = max(res, noise[l][u]);
u = up[l][u];
}
}
if(upper(u,v)) return max(res, noise[0][v]);
if(upper(v,u)) return max(res, noise[0][u]);
return max({res, noise[0][u], noise[0][v]});
}
int main(){
int n, m, l;
cin >> n >> m;
//enter our graph with all edges and noises on the all edges
vector<Edge> edgeList;
for(int i=0; i<m; i++){
int u, v, c;
cin >> u >> v >> c;
if(m == n-1){
adjList[u-1].push_back({v-1,c});
adjList[v-1].push_back({u-1,c});
}
else edgeList.push_back(Edge(u-1, v-1, c));
}
//make a minimum spanning tree
if(m != n-1) build_MST(adjList, edgeList, n);
dfs(0, 0);
search_ancestor_and_maxnoise(n);
cin >> l;
while(l--){
int u, v;
cin >> u >> v;
cout << query(u-1, v-1) << endl;
}
}