Submission #674356

# Submission time Handle Problem Language Result Execution time Memory
674356 2022-12-23T18:29:32 Z Ahmed57 Sightseeing (NOI14_sightseeing) C++14
15 / 25
3500 ms 196332 KB
//LCA
#include <bits/stdc++.h>

using namespace std;

const int N=5e5+1;

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(int 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()
{
    for(int i = 0;i<N;i++)pr[i] = i , gs[i] = 1;
    cin>>n>>m>>q;
    vector<pair<int,pair<int,int>>>v;
    for(int i=0;i<m;i++)
    {
        int 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 10 ms 15956 KB Output is correct
2 Correct 9 ms 15956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 66 ms 18620 KB Output is correct
2 Correct 56 ms 18384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3261 ms 74292 KB Output is correct
2 Execution timed out 3528 ms 196332 KB Time limit exceeded
3 Halted 0 ms 0 KB -