Submission #674357

# Submission time Handle Problem Language Result Execution time Memory
674357 2022-12-23T18:31:44 Z Ahmed57 Sightseeing (NOI14_sightseeing) C++14
25 / 25
2032 ms 213152 KB
//LCA
#include <bits/stdc++.h>

using namespace std;

const int N=500001;

vector<pair<long long,long long>> adj[N];

int n,m,q;

//DSU
int pr[500001];
int gs[500001];

int findleader(int x){
    if(pr[x]==x){
        return x;
    }
    return pr[x] = findleader(pr[x]);
}
bool samegroup(int x,int y){
    int led1 = findleader(x);
    int led2 = findleader(y);
    return led1==led2;
}
void mergegroup(int x,int y){
    int led1 = findleader(x);
    int led2 = findleader(y);
    if(led1==led2)return;
    if(gs[led1]>gs[led2]){
        pr[led2]=led1;
        gs[led1]+=gs[led2];
    }else{
        pr[led1]=led2;
        gs[led2]+=gs[led1];
    }
}
long long val[500001];
void dfs(long long i,int pr,long long cnt){
    val[i] = cnt;
    for(auto j:adj[i]){
        if(j.first==pr)continue;
        dfs(j.first,i,min(cnt,j.second));
    }
}
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    for(int i = 0;i<N;i++)pr[i] = i , gs[i] = 1;
    cin>>n>>m>>q;
    vector<pair<long long,pair<long long,long long>>>v;
    for(int i=0;i<m;i++)
    {
        long long x,y,z;
        cin>>x>>y>>z;
        v.push_back({z,{x,y}});
    }
    sort(v.begin(),v.end());
    for(int i = v.size()-1;i>=0;i--){
        if(!samegroup(v[i].second.first,v[i].second.second)){
            mergegroup(v[i].second.first,v[i].second.second);
            adj[v[i].second.first].push_back({v[i].second.second,v[i].first});
            adj[v[i].second.second].push_back({v[i].second.first,v[i].first});
        }
    }
    dfs(1,0,1e18);
    while(q--){
        int x;
        cin>>x;
        cout<<val[x]<<"\n";
    }
}
# Verdict Execution time Memory Grader output
1 Correct 8 ms 15956 KB Output is correct
2 Correct 8 ms 15956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 16156 KB Output is correct
2 Correct 9 ms 15956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 19252 KB Output is correct
2 Correct 29 ms 18920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1339 ms 114636 KB Output is correct
2 Correct 2032 ms 213152 KB Output is correct