Submission #156990

# Submission time Handle Problem Language Result Execution time Memory
156990 2019-10-08T19:20:34 Z brcode Sightseeing (NOI14_sightseeing) C++14
25 / 25
3088 ms 200216 KB
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int MAXN = 5e5+5;
int par[MAXN];
int ans[MAXN];
int sz[MAXN];
vector<pair<int,int>> v1[MAXN];
vector<pair<int,pair<int,int>>> edges;
int find(int a){
    if(par[a] == a){
        return a;
    }
    return par[a] = find(par[a]);
}
void unite(int a,int b){
    a = find(a);
    b = find(b);
    if(a==b){
        return;
    }
    if(sz[a]<sz[b]){
        swap(a,b);
    }
    par[b] = a;
    sz[a] +=sz[b];
}
void dfs(int curr,int par,int weight){
    ans[curr] = weight;
    for(auto x:v1[curr]){
        if(x.first!=par){
            dfs(x.first,curr,min(weight,x.second));
        }
    }
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n,m,q;
    cin>>n>>m>>q;
    for(int i=1;i<=n;i++){
        par[i] = i;
        sz[i] = 1;
    }
    for(int i=1;i<=m;i++){
        int x,y,w;
        cin>>x>>y>>w;
        edges.push_back(make_pair(w,make_pair(x,y)));
    }
    sort(edges.begin(),edges.end());
    reverse(edges.begin(),edges.end());
    for(auto x:edges){
        int w = x.first;
        int a = x.second.first;
        int b = x.second.second;
        if(find(a)!=find(b)){
            unite(a,b);
           
            v1[a].push_back(make_pair(b,w));
            v1[b].push_back(make_pair(a,w));
        }
    }
    dfs(1,1,1e9);
    for(int i=1;i<=q;i++){
        int x;
        cin>>x;
        cout<<ans[x]<<"\n";
    }
}
# Verdict Execution time Memory Grader output
1 Correct 12 ms 12152 KB Output is correct
2 Correct 12 ms 12024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 12152 KB Output is correct
2 Correct 14 ms 12096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 14448 KB Output is correct
2 Correct 42 ms 14320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1975 ms 83476 KB Output is correct
2 Correct 3088 ms 200216 KB Output is correct