#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;
const int MAX_C = 1e5;
int v, e, q;
vector<pii> adj[MAX_V];
vector<int> postal[MAX_C];
Edge in[MAX_E],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;
in[i] = Edge(a, b, c);
postal[c].push_back(i);
}
int cur_e= 0;
for(int i= 0; i<MAX_C; i++){
for(auto e: postal[i]){
edges[cur_e] = in[e];
cur_e++;
}
}
fill(dsu, dsu+e, -1);
fill(sz, sz+e, 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 |
8 ms |
14452 KB |
Output is correct |
2 |
Correct |
8 ms |
14420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14688 KB |
Output is correct |
2 |
Correct |
9 ms |
14508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
19312 KB |
Output is correct |
2 |
Correct |
34 ms |
18596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1233 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |