# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
977099 | dubabuba | Bridges (APIO19_bridges) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int mxn = 2e5 + 10;
int par[mxn], ans[mxn], n, m;
vector<pair<int, pii>> edges;
int parent(int u) {
if(par[u] < 0) return u;
return par[u] = parent(par[u]);
}
bool unite(int u, int v) {
u = parent(u);
v = parent(v);
if(u == v) return 0;
if(par[u] > par[v]) swap(u, v);
par[u] += par[v];
par[v] = u;
return 0;
}
int main() {
cin >> n >> m;
for(int i = 0; i < m; i++) {
int u, v, w;
cin >> u >> v >> w;
edges.push_back(MP(-w, MP(u, v)));
}
cin >> q;
for(int i = 1; i <= q; i++) {
int s, w;
cin >> s >> w;
edges.push_back(MP(-w, MP(-i, s)));
}
sort(edges.begin(), edges.end());
memset(par, -1, sizeof par);
for(auto p : edges) {
if(p.ss.ff > 0) {
int w = p.ff;
int u = p.ss.ff;
int v = p.ss.ss;
unite(u, v);
}
else {
int w = p.ff;
int i = -p.ss.ff;
int s = p.ss.ss;
ans[i] = par[parent(s)];
}
}
for(int i = 1; i <= q; i++)
cout << ans[i] << endl;
return 0;
}