답안 #559306

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
559306 2022-05-09T15:13:38 Z leaked Pastiri (COI20_pastiri) C++17
0 / 100
665 ms 139456 KB
#include <bits/stdc++.h>

#define f first
#define s second
#define m_p make_pair
#define vec vector
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define sz(x) (int)(x).size()
#define pw(x) (1LL<<(x))

using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
template<class T> bool umax(T &a,const T &b){return (a<b?a=b,1:0);}
template<class T> bool umin(T &a,const T &b){return (a>b?a=b,1:0);}
const int N=5e5+1;
vec<int> gr[N];
bool used[N];
vec<int> g[N];
int d[N],h[N];
vec<int> have[N],cand[N];
int up[N];
int di[2*N];
void dfs(int v,int p,pii mx){
//    cout<<d[v]+h[v]<<endl;
    if(di[d[v]+h[v]]==0){
        di[d[v]+h[v]]=v;
    }
    up[v]=di[h[v]];
    for(auto &z : g[v]){
        if(z==p) continue;
        h[z]=h[v]+1;
        dfs(z,v,mx);
    }
    if(di[d[v]+h[v]]==v)
        di[d[v]+h[v]]=0;
}
signed main(){
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    fill(d,d+N,1e9);
    int n,k;
    cin>>n>>k;
    for(int i=1;i<n;i++){
        int v,u;
        cin>>v>>u;
        g[v].pb(u);g[u].pb(v);
    }
    queue<int>q;
    vec<int> kek;
    for(int i=0;i<k;i++){
        int v;
        cin>>v;
        d[v]=0;
        q.push(v);
        kek.pb(v);
    }
//    vec<int> vc;
    while(!q.empty()){
        int v=q.front();q.pop();
        for(auto &z : g[v]){
            if(d[z]==1e9){
                d[z]=d[v]+1;
                q.push(z);
            }
        }
    }
    dfs(1,1,{-1,-1});
    for(auto &z : kek){
//        cout<<up[z]<<endl;
        cand[d[up[z]]].pb(up[z]);
    }
    for(int i=1;i<=n;i++){
        for(auto &z : g[i]){
            if(d[i]==d[z]+1)
                gr[i].pb(z);
        }
        have[d[i]].pb(i);
    }
    vec<int> ans;
//    vecE<
    for(int i=n-1;i>=0;i--){
        for(auto &z : cand[i]){
            if(!used[z]){
                used[z]=1;
                ans.pb(z);
            }
        }
        for(auto &z : have[i]){
            if(used[z]){
                for(auto &u : gr[z])
                    used[u]=1;
            }
//            for(auto &u : gr[z])
//                used[u]=1;
        }
    }
    cout<<sz(ans)<<endl;
    for(auto &z : ans)
        cout<<z<<' ';
    return 0;
}
/*
16 5
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 216 ms 139456 KB Output is correct
2 Incorrect 298 ms 128908 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 28 ms 49612 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 30 ms 49396 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 665 ms 88004 KB Output isn't correct
2 Halted 0 ms 0 KB -