Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- vector<int> pai, sub, edge;
- int find(int v) {return (v == pai[v] ? v : find(pai[v]));}
- void une(int x, int y, int t)
- {
- x = find(x); y = find(y);
- if (x == y) return;
- if (sub[x] < sub[y]) swap(x, y);
- pai[y] = x; sub[x] += sub[y]; edge[y] = t;
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- int N, M, Q; cin >> N >> M >> Q;
- pai.resize(N+1); iota(pai.begin(), pai.end(), 0);
- sub.resize(N+1); fill(sub.begin(), sub.end(), 1);
- edge.resize(N+1); fill(edge.begin(), edge.end(), 0);
- for (int i = 1; i <= M; i++)
- {
- int x, y; cin >> x >> y;
- une(x, y, i);
- }
- while (Q--)
- {
- int x, y; cin >> x >> y;
- if (find(x) != find(y)) {cout << -1 << '\n'; continue;}
- int resp = 0;
- while (x != y)
- {
- if (sub[x] < sub[y]) {resp = max(resp, edge[x]); x = pai[x];}
- else {resp = max(resp, edge[y]); y = pai[y];}
- }
- cout << resp << '\n';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment