mirror of
https://git.sb/baoshuo/OI-codes.git
synced 2024-11-27 20:36:28 +00:00
P1967 [NOIP2013 提高组] 货车运输
R53202344
This commit is contained in:
parent
57ade6ea99
commit
d4f8aaab2e
67
Luogu/problem/P1967/P1967.cpp
Normal file
67
Luogu/problem/P1967/P1967.cpp
Normal file
@ -0,0 +1,67 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int n, m, q, x, y, c, px, py, ans[50005], fa[10005];
|
||||
set<int> f[10005];
|
||||
set<int>::iterator it;
|
||||
|
||||
struct edge {
|
||||
int u, v, cost;
|
||||
|
||||
bool operator<(edge b) {
|
||||
return cost > b.cost;
|
||||
}
|
||||
} es[50005];
|
||||
|
||||
int find(int x) {
|
||||
return x == fa[x] ? x : fa[x] = find(fa[x]);
|
||||
}
|
||||
|
||||
int main() {
|
||||
memset(ans, 0xff, sizeof(ans));
|
||||
for (int i = 1; i < 10005; i++) {
|
||||
fa[i] = i;
|
||||
}
|
||||
cin >> n >> m;
|
||||
for (int i = 0; i < m; i++) {
|
||||
cin >> es[i].u >> es[i].v >> es[i].cost;
|
||||
}
|
||||
sort(es, es + m);
|
||||
cin >> q;
|
||||
for (int i = 0; i < q; i++) {
|
||||
cin >> x >> y;
|
||||
f[x].insert(i);
|
||||
f[y].insert(i);
|
||||
}
|
||||
for (int i = 0; i < m; i++) {
|
||||
x = es[i].u;
|
||||
y = es[i].v;
|
||||
c = es[i].cost;
|
||||
px = find(x);
|
||||
py = find(y);
|
||||
if (px == py) {
|
||||
continue;
|
||||
} else {
|
||||
if (f[px].size() > f[py].size()) {
|
||||
swap(px, py);
|
||||
}
|
||||
vector<int> tmp;
|
||||
for (it = f[px].begin(); it != f[px].end(); it++) {
|
||||
if (f[py].count(*it)) {
|
||||
ans[*it] = c;
|
||||
tmp.push_back(*it);
|
||||
}
|
||||
f[py].insert(*it);
|
||||
}
|
||||
for (int i = 0; i < tmp.size(); i++) {
|
||||
f[py].erase(tmp[i]);
|
||||
}
|
||||
fa[px] = py;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < q; i++) {
|
||||
cout << ans[i] << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user