-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3176.cpp
99 lines (80 loc) · 2.44 KB
/
3176.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>
#pragma GCC optimize ("O3")
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("avx,avx2,fma")
using namespace std;
typedef long long ll;
int xxxx[]={-1,0,1,0,-1,1,1,-1};
int yyyy[]={0,1,0,-1,-1,1,-1,1};
typedef pair<int,int> pii;
const int MAX = 20;
void solve(){
int N,M;cin>>N;
int depth[N+1]; memset(depth, 0, sizeof depth);
depth[1]=1;
vector<vector<pii>> edges(N+1);
for (int i = 0; i < N-1; i++) {
int u,v,w;cin >> u >> v>>w;
edges[u].push_back({v,w});
edges[v].push_back({u,w});
}
queue<int> q; q.push(1);
int parent[MAX][N+1];
pii results[MAX][N+1]; for (int i = 0; i < MAX; i++) for (int j = 1; j <= N; j++) results[i][j] = {1e6,0};
while (q.size()) {
auto x = q.front();q.pop();
for (auto [nx, w] : edges[x]) {
if (depth[nx]) continue;
depth[nx]=depth[x]+1;
q.push(nx);
parent[0][nx]=x;
results[0][nx]={w,w};
}
}
parent[0][1]=1;
for (int j = 1; j < MAX; j++) {
for (int i = 1; i <= N; i++) {
int next = parent[j-1][i];
parent[j][i] = parent[j-1][next];
auto [a, b] = results[j-1][next];
auto [c, d] = results[j-1][i];
results[j][i] = {min(a,c), max(b,d)};
}
}
cin >> M;
while (M--) {
int a,b; cin >> a >> b;
if (depth[a] > depth[b]) swap(a,b);
int jump = 1;
while (depth[parent[jump][b]] > depth[a]) jump++;
jump--;
int _min = 1e6;
int _max = 0;
while (depth[b] > depth[a]) {
while (depth[parent[jump][b]] < depth[a]) jump--;
auto [x1,x2] = results[jump][b];
_min = min(_min,x1);
_max = max(_max,x2);
b = parent[jump][b];
}
while (a != b) {
jump = 1;
while (parent[jump][b] != parent[jump][a]) jump++;
jump--;
auto [x1,x2] = results[jump][a];
auto [x3,x4] = results[jump][b];
_min = min({_min,x1,x3});
_max = max({_max,x2,x4});
a = parent[jump][a];
b = parent[jump][b];
}
cout << _min << ' ' << _max << '\n';
}
}
int main()
{
cin.tie(0)->sync_with_stdio(false);
solve();
return 0;
}