#include<bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
struct Edge{
int a, b;
int c;
Edge(){};
bool operator<(const Edge& b)const{
return c<b.c;
}
Edge(int _a, int _b, int _c){
a = _a;
b= _b;
c= _c;
}
};
const int MAX_V = 5*1e5;
const int MAX_E = 5*1e6;
int v, e, q;
vector<pii> adj[MAX_V];
Edge edges[MAX_E];
int dsu[MAX_V], sz[MAX_V], adj_sz[MAX_V];
int cost[MAX_V];
int get_C(int id){
if(dsu[id]==-1){
return id;
}
else{
int anc= get_C(dsu[id]);
dsu[id] = anc;
return anc;
}
}
void merge(int a, int b){
a = get_C(a);
b = get_C(b);
if(sz[a]<sz[b]){
swap(a, b);
}
dsu[b] = a;
sz[a] += sz[b];
}
void dfs(int u, int c, int anc){
cost[u] = c;
for(auto v: adj[u]){
if(v.first!=anc){
dfs(v.first, min(c, v.second), u);
}
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin>>v>>e>>q;
for(int i = 0; i<e; i++){
int a, b;
int c;
cin>>a>>b;
a--;b--;
cin>>c;
edges[i] = Edge(a, b, c);
}
sort(edges, edges+e);
fill(dsu, dsu+v, -1);
fill(sz, sz+v, 1);
for(int i = e-1; i>=0; i--){
int a= edges[i].a;
int b= edges[i].b;
if(get_C(a)!=get_C(b)){
adj[a].push_back(pii(b, edges[i].c));
adj[b].push_back(pii(a, edges[i].c));
adj_sz[a]++;
adj_sz[b]++;
merge(a, b);
}
}
dfs(0, 1e9, -1);
for(int i = 0; i<q; i++){
int u;
cin>>u;
cout<<cost[u-1]<<endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
5 ms |
11988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
12116 KB |
Output is correct |
2 |
Correct |
5 ms |
12116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
14484 KB |
Output is correct |
2 |
Correct |
26 ms |
14348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1219 ms |
126344 KB |
Output is correct |
2 |
Correct |
1978 ms |
202928 KB |
Output is correct |