Submission #674356

#TimeUsernameProblemLanguageResultExecution timeMemory
674356Ahmed57Sightseeing (NOI14_sightseeing)C++14
15 / 25
3528 ms196332 KiB
//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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...