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;
#define int long long
struct UnionFind {
vector<int> p, ranks;
UnionFind(int n) : p(n), ranks(n, 0) {
for(int i = 0; i < n; ++i) {
p[i] = i;
}
}
int find_set(int a) {
return p[a] == a ? a : p[a] = find_set(p[a]);
}
bool is_same_set(int a, int b) {
return find_set(a) == find_set(b);
}
void unite(int a, int b) {
int x = find_set(a), y = find_set(b);
if(x != y) {
if(ranks[x] > ranks[y]) {
p[y] = x;
} else {
p[x] = y;
if(ranks[x] == ranks[y]) {
++ranks[y];
}
}
}
}
};
typedef pair<int, pair<int, int>> iii;
struct edge {
int v, w;
};
vector<vector<edge>> adj;
vector<int> dist;
void dfs(int u, int p = -1, int d = 1e9) {
dist[u] = d;
for(edge e : adj[u]) {
if(e.v != p) {
dfs(e.v, u, min(d, e.w));
}
}
}
signed main() {
cin.sync_with_stdio(false);
cin.tie(0);
int v, e, q;
cin >> v >> e >> q;
adj.resize(v);
dist.resize(v);
vector<iii> edges(e);
for(int i = 0; i < e; ++i) {
int a, b, c;
cin >> a >> b >> c;
--a; --b;
edges[i] = {c, {a, b}};
}
sort(edges.rbegin(), edges.rend());
UnionFind uf(v);
for(iii p : edges) {
int a = p.second.first, b = p.second.second;
int c = p.first;
if(!uf.is_same_set(a, b)) {
uf.unite(a, b);
adj[a].push_back(edge({b, c}));
adj[b].push_back(edge({a, c}));
}
}
dfs(0);
for(int i = 0; i < q; ++i) {
int x;
cin >> x;
--x;
cout << dist[x] << "\n";
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |