#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";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
556 KB |
Output is correct |
2 |
Correct |
3 ms |
672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
5216 KB |
Output is correct |
2 |
Correct |
31 ms |
5216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1994 ms |
109812 KB |
Output is correct |
2 |
Correct |
2886 ms |
275048 KB |
Output is correct |