Kuroni and the Celebration

D - Kuroni and the Celebration

每次遍历一条链的两个端点,途中经过的点除了根节点,其他肯定不可能是。

// Created by CAD on 2020/3/4.
#include <bits/stdc++.h>
using namespace std;

vector<int> g[1005];
bool vis[1005];
int dfs(int x){
    int ans=x;
    for(auto i:g[x]){
        if(!vis[i]){
            vis[i]=1;
            ans=dfs(i);
            break;
        }
    }
    return ans;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;cin>>n;
    for(int i=1;i<n;++i){
        int u,v;cin>>u>>v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    int r=n;
    vis[n]=1;
    while(1){
        int x=dfs(r);
        if(r==x) break;
        int y=dfs(r);
        cout << "? " << y << " " << x << endl;
        cin>>r;
    }   
    cout<<"! "<<r<<endl;
    return 0;
}
posted @ 2020-03-04 14:25  caoanda  阅读(165)  评论(0编辑  收藏  举报