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;
}

原文链接: https://www.cnblogs.com/cader/p/12409302.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    Kuroni and the Celebration

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/333436

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年3月1日 下午9:10
下一篇 2023年3月1日 下午9:10

相关推荐