-
Notifications
You must be signed in to change notification settings - Fork 156
/
2101 - New Roads Queries.cpp
89 lines (79 loc) · 2.02 KB
/
2101 - New Roads Queries.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
/*
Problem Name: New Roads Queries
Problem Link: https://cses.fi/problemset/task/2101
Author: Sachin Srivastava (mrsac7)
*/
#include<bits/stdc++.h>
using namespace std;
// #define int long long
#define endl '\n'
#define F first
#define S second
const int mxN = 2e5+5;
int par[mxN], len[mxN];
void init() {
for (int i = 0; i < mxN; i++)
par[i] = i, len[i] = 1;
}
int find(int x) {
if (par[x] == x)
return x;
return par[x] = find(par[x]);
}
void join(int x, int y) {
x = find(x);
y = find(y);
if (x != y) {
if (len[x] < len[y])
swap(x, y);
par[y] = x;
len[x] += len[y];
}
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
#ifdef LOCAL
freopen("input.txt", "r" , stdin);
freopen("output.txt", "w", stdout);
#endif
int n, m, t; cin >> n >> m >> t;
vector<pair<int, int>> edges(m);
for (auto &[i, j]: edges)
cin >> i >> j;
vector<pair<int, int>> v(t);
for (auto &[i, j]: v)
cin >> i >> j;
vector<pair<int, int>> range(t);
vector<int> ans(t, -1);
for (int i = 0; i < t; i++) {
range[i] = {0, m};
}
while (1) {
init();
vector<pair<int, int>> mid;
for (int i = 0; i < t; i++) if (range[i].F <= range[i].S) {
int m = (range[i].F + range[i].S) / 2;
mid.push_back({m, i});
}
if (mid.size() == 0)
break;
sort(mid.begin(), mid.end());
int j = 0;
for (int i = 0; i <= m; i++) {
if (i) join(edges[i-1].F, edges[i-1].S);
while (j < mid.size() && mid[j].F == i) {
int k = mid[j].S;
if (find(v[k].F) == find(v[k].S)) {
ans[k] = i;
range[k] = {range[k].F, i-1};
}
else {
range[k] = {i+1, range[k].S};
}
j++;
}
}
}
for (int i = 0; i < t; i++)
cout << ans[i] << endl;
}