답안 #674351

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
674351 2022-12-23T18:25:32 Z Ahmed57 관광 (NOI14_sightseeing) C++14
15 / 25
3500 ms 184336 KB
//LCA
#include <bits/stdc++.h>

using namespace std;

const int N=5e5+5;

vector<pair<int,int>> adj[N];
long long P[N][20],mi[N][20];
long long hi[N];
int n,m,q;

void dfs(int node,int pa,int ed){
    P[node][0]=pa;
    hi[node]=hi[pa]+1;
    mi[node][0] = ed;
    for(int k=1;k<=19;k++){
        P[node][k]=P[P[node][k-1]][k-1];
        mi[node][k] = min(mi[node][k-1],mi[P[node][k-1]][k-1]);
    }
    for(auto i:adj[node]){
        if(i.first==pa) continue;
        dfs(i.first,node,i.second);
    }
}

long long lca(int x,int y)
{
    long long mii = 1e18;
    if(hi[x]<hi[y]) swap(x,y);
    for(int k=19;k>=0;k--)
    {
        if(hi[x]-(1<<k) >= hi[y]){
            mii = min(mii,mi[x][k]);
            x=P[x][k];
        }
    }
    if(x==y) return mii;
    for(int k=19;k>=0;k--)
    {
        if(P[x][k] != P[y][k]){
            mii = min(mii,mi[x][k]);
            mii = min(mii,mi[y][k]);
            x=P[x][k],y=P[y][k];
        }
    }
    return min({mii,mi[x][0],mi[y][0]});
}
//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];
    }
}
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,0);
    while(q--){
        int x;
        cin>>x;
        cout<<lca(1,x)<<"\n";
    }
}

Compilation message

sightseeing.cpp: In function 'int main()':
sightseeing.cpp:78:33: warning: iteration 500001 invokes undefined behavior [-Waggressive-loop-optimizations]
   78 |     for(int i = 0;i<N;i++)pr[i] = i , gs[i] = 1;
      |                           ~~~~~~^~~
sightseeing.cpp:78:20: note: within this loop
   78 |     for(int i = 0;i<N;i++)pr[i] = i , gs[i] = 1;
      |                   ~^~
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 15956 KB Output is correct
2 Correct 8 ms 15956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 16212 KB Output is correct
2 Correct 9 ms 16104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 76 ms 29188 KB Output is correct
2 Correct 73 ms 28620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3537 ms 184336 KB Time limit exceeded
2 Halted 0 ms 0 KB -