-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathNew Roads Queries.cpp
More file actions
180 lines (155 loc) · 3.75 KB
/
New Roads Queries.cpp
File metadata and controls
180 lines (155 loc) · 3.75 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "bits/stdc++.h"
using namespace std;
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
#define F first
#define S second
/*
Heavy light decomposition divides the tree into logn paths
Each path only moves downward ( or upward )
So, for querying on any property of the nodes (or paths) from a to b
we can query the properties of path from a to LCA(a,b) and from b to LCA(a,b) and merge them
We create seperate segment tree for each path.
Here actually an array of euler path is taken on nodes. So we assign the property of the node
to the euler id of the node and create segment tree with that. And as for a path the euler path id
is increasing so, if we can somehow breakdown the problem where we can use the property on some
segment tree we can solve the problem.
Here, some properties can be minimum, maximum value of nodes, sum of values of nodes, xor, etc etc
*/
const int N = 4e5+5;
// HLD
const int D = 25;
const int S = (1<<D);
int n, q, v[N];
vector<int> adj[N];
int sz[N], p[N], dep[N];
int st[S], id[N], tp[N];
void update(int idx, int val) {
st[idx+=n ] = val;
for (idx /= 2; idx; idx /= 2)
st[idx] = max(st[2 * idx], st[2 * idx + 1]);
}
int query(int lo, int hi) {
int ra = 0, rb = 0; //<< Don't forget to pre initialize the starting value
for (lo += n, hi += n + 1; lo < hi; lo /= 2, hi /= 2) {
if (lo & 1)
ra = max(ra, st[lo++]);
if (hi & 1)
rb = max(rb, st[--hi]);
}
return max(ra, rb);
}
int dfs_sz(int cur, int par) {
sz[cur] = 1;
p[cur] = par;
for(int chi : adj[cur]) {
if(chi == par) continue;
dep[chi] = dep[cur] + 1;
p[chi] = cur;
sz[cur] += dfs_sz(chi, cur);
}
return sz[cur];
}
int ct = 1;
void dfs_hld(int cur, int par, int top) {
id[cur] = ct++;
tp[cur] = top;
update(id[cur], v[cur]); //< We update the property against the euler id
int h_chi = -1, h_sz = -1;
for(int chi : adj[cur]) {
if(chi == par) continue;
if(sz[chi] > h_sz) {
h_sz = sz[chi];
h_chi = chi;
}
}
if(h_chi == -1) return;
dfs_hld(h_chi, cur, top);
for(int chi : adj[cur]) {
if(chi == par || chi == h_chi) continue;
dfs_hld(chi, cur, chi);
}
}
int path(int x, int y){
int ret = 0;
while(tp[x] != tp[y]){
if(dep[tp[x]] < dep[tp[y]])swap(x,y);
ret = max(ret, query(id[tp[x]],id[x]));
x = p[tp[x]];
}
if(dep[x] > dep[y])swap(x,y);
ret = max(ret, query(id[x],id[y]));
return ret;
}
// DSU
int parent[N];
int Rank[N];
void make_set(int u) {
parent[u] = u;
Rank[u] = 0;
}
int find_set(int u) {
if (u == parent[u])
return u;
return parent[u] = find_set(parent[u]);
}
void union_sets(int a, int b) {
a = find_set(a);
b = find_set(b);
if (a != b) {
if (Rank[a] < Rank[b])
swap(a, b);
parent[b] = a;
if (Rank[a] == Rank[b])
Rank[a]++;
}
}
int edgeCounters;
int main() {
int m,q;
cin>>n>>m>>q;
for(int i=0;i<=n;i++)
make_set(i);
for(int i=1;i<=m;i++){
int a,b;
cin>>a>>b;
if( find_set(a)!=find_set(b) ){
union_sets(a,b);
edgeCounters++;
int w = n+edgeCounters;
adj[a].push_back(w);
adj[w].push_back(a);
adj[b].push_back(w);
adj[w].push_back(b);
v[ w ] = i;
}
}
int w = edgeCounters+n+1;
make_set(w);
for(int i=1;i<=n;i++){
if( find_set(i)!=find_set(w) ){
union_sets(i,w);
adj[i].push_back(w);
adj[w].push_back(i);
v[ w ] = w;
}
}
n = w;
dfs_sz(1, 1); //< 1 indexed system ; The LCA here is also 1 indexed
dfs_hld(1, 1, 1);
while(q--) {
int a, b;
cin>>a>>b;
int res = path(a,b);
if( res == w ) cout<<-1<<endl;
else cout<<res<<endl;
}
}
/*
5 4 0100
1 2
2 3
1 3
2 5
*/