#include <bits/stdc++.h>
using namespace std;
const int mxM = 5e6 + 6;
const int mxN = 5e5 + 5;
struct Edge{
int u, v, w;
bool operator<(Edge &other){
return w > other.w;
}
};
struct DSU{
vector<int>par;
vector<vector<int>>s;
DSU(int n){
par.assign(n + 1, -1);
s.resize(n + 1);
for(int i = 1;i <= n;++i){
s[i].push_back(i);
}
}
int FindRep(int u){
if(par[u] < 0)return u;
return par[u] = FindRep(par[u]);
}
void Union(int u, int v){
u = FindRep(u);
v = FindRep(v);
if(u == v)return;
if(par[u] > par[v])swap(u, v);
for(int &i : s[v])s[u].push_back(i);
par[u] += par[v];
par[v] = u;
}
};
Edge edges[mxM];
int ans[mxN];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m, q;
cin >> n >> m >> q;
for(int i = 0;i < m;++i){
int u, v, w;
cin >> u >> v >> w;
edges[i] = {u, v, w};
}
DSU dsu(n);
sort(edges, edges + m);
for(int i = 0;i < m;++i){
int u = edges[i].u, v = edges[i].v, w = edges[i].w;
u = dsu.FindRep(u);
v = dsu.FindRep(v);
int node = dsu.FindRep(1);
if((u == node && v == node) || (u != node && v != node)){
dsu.Union(u, v);
}
else{
if(u == node){
for(int &i : dsu.s[v])ans[i] = w;
}
else{
for(int &i : dsu.s[u])ans[i] = w;
}
dsu.Union(u, v);
}
}
while(q--){
int node;
cin >> node;
cout << ans[node] << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
7260 KB |
Output is correct |
2 |
Correct |
15 ms |
7004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
897 ms |
66100 KB |
Output is correct |
2 |
Correct |
1375 ms |
97624 KB |
Output is correct |