Submission #1364629

#TimeUsernameProblemLanguageResultExecution timeMemory
1364629yyc000123Social Engineering (EGOI22_socialengineering)C++20
100 / 100
425 ms50904 KiB
#include<bits/stdc++.h>
using namespace std ;
#define F first
#define S second
const int N = 2e5+5 ;
const int M = 4e5+5 ;
int n , m , spe[N] , vis[N] , sub[N] , deep[N] , anc[N][(int)log2(N)+5] , match[N] ;
vector<int> nei[N] , roots ;

int GetMove(){
    int ret ; cin >> ret ; return ret ;
}
void MakeMove(int v){
    cout << v << endl ;
}

void dfs(int node){
    sub[node]+=spe[node] ; vis[node] = 1 ;
    for(int i:nei[node]){
        if(vis[i]) continue ;
        anc[i][0] = node ; deep[i] = deep[node]+1 ;
        dfs(i) ; sub[node]+=sub[i] ;
    }
}

int dfs1(int node){
    vis[node] = 1 ;
    vector<int> v ;
    if(spe[node]) v.push_back(node) ;
    for(int i:nei[node]){
        if(vis[i]) continue ;
        int temp = dfs1(i) ;
        if(temp!=-1) v.push_back(temp) ;
    }
    while(v.size()>=2){
        int a = v.back() ; v.pop_back() ;
        int b = v.back() ; v.pop_back() ;
        match[a] = b ; match[b] = a ;
    }
    if(v.empty()) return -1 ;
    else return v[0] ;
}

int flca(int a , int b){
    if(deep[a]<deep[b]) swap(a,b) ;
    int diff = deep[a]-deep[b] ;
    for(int i=0 ; i<=log2(n)+1 ; i++){
        if((diff>>i)&1) a = anc[a][i] ;
    }
    if(a==b) return a ;
    for(int i=log2(n)+1 ; i>=0 ; i--){
        if(anc[a][i]!=anc[b][i]) a = anc[a][i] , b = anc[b][i] ;
    }
    return anc[a][0] ;
}

int main(){
    cin >> n >> m ;
    for(int i=0 ; i<m ; i++){
        int u , v ; cin >> u >> v ;
        if(u>v) swap(u,v) ;
        if(u==1) spe[v] = 1 ;
        else nei[u].push_back(v) , nei[v].push_back(u) ;
    }
    for(int i=2 ; i<=n ; i++){
        if(vis[i]) continue ;
        roots.push_back(i) ; dfs(i) ;
    }
    for(int j=1 ; j<=log2(n)+1 ; j++){
        for(int i=1 ; i<=n ; i++) anc[i][j] = anc[anc[i][j-1]][j-1] ;
    }
    memset(vis,0,sizeof(vis)) ;
    for(int i:roots){
        if(sub[i]&1){ cout << "NO\n" ; return 0 ; }
        dfs1(i) ;
    }
    cout << "YES\n" ;
    while(true){
        int k = GetMove() ;
        if(!k) break ;
        int lca = flca(k,match[k]) , l = match[k] ;
        vector<int> path , path1 ;
        while(k!=lca) path.push_back(anc[k][0]) , k = anc[k][0] ;
        while(l!=lca) path1.push_back(l) , l = anc[l][0] ;
        reverse(path1.begin(),path1.end()) ;
        for(int i:path1) path.push_back(i) ;
        path.push_back(1) ;
        for(int i:path) MakeMove(i) ;
    }
    return 0 ;
}
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...