题解:CF2022D2 Asesino (Hard Version)

· · 题解

原来我的证明是对的。被同学反复质疑了一个早上。感觉早上的课都听得不是很认真。

思路

我们把对 x 询问 y 当做一条边,就相当于是建图。

玩一玩表格可以发现,如果 x 到 y 的双向边不相同,那么内鬼一定在 x 和 y 中。否则这两个点一定都不是内鬼。

然后赛时成功想出了 1 和 i 连双向边这种智慧做法,简单版本都过不去。

现在,我们需要证明,我们不能用 n-1 次找出答案。

由于只有 n-1 条边,所以一定有一个点入度为 0,一定还有个点出度为 0。

假设不是一个点,那么对于这个子图,我们对所有的边的权值都设成 1,那么就有两种情况。

所以是判断不了的。

假设是一个点,那么相当于用 n-1 条边,确定了内鬼在哪条边中。最差的情况是还需要通过两次确定内鬼到底是哪个。

证明部分可能不是特别严谨,如果有问题私信问我。

Code

#include <bits/stdc++.h>
using namespace std;
namespace yhy{
    const int MAXN=1e5+5;
    int n;
    inline void Output(int x,int y) {
        int t=x-1;
        if(t==0) t=n;
        int a,b;
        cout<<"? "<<x<<" "<<t<<endl;
        cin>>a;
        cout<<"? "<<t<<" "<<x<<endl;
        cin>>b;
        if(a==b) cout<<"! "<<y<<endl;
        else cout<<"! "<<x<<endl;
    }
    int main() {
//      freopen(".in","r",stdin);
//      freopen(".out","w",stdout);
        std::ios::sync_with_stdio(false);
        cin.tie(0);cout.tie(0);
        int T;cin>>T;
        while(T--) {
            cin>>n;
            if(n==3) {
                int x1,x2;
                int y1,y2;
                cout<<"? 1 2"<<endl;cin>>x1;
                cout<<"? 2 1"<<endl;cin>>x2;
                cout<<"? 1 3"<<endl;cin>>y1;
                cout<<"? 3 1"<<endl;cin>>y2;
                if(x1==x2 && y1!=y2) cout<<"! 3"<<endl;
                if(x1!=x2 && y1==y2) cout<<"! 2"<<endl;
                if(x1!=x2 && y1!=y2) cout<<"! 1"<<endl;
            }
            else if(n&1) {
                int a,b,c;
                cout<<"? 1 2"<<endl;cin>>a;
                cout<<"? 2 3"<<endl;cin>>b;
                cout<<"? 3 1"<<endl;cin>>c;
                if(a+b+c==0 || a+b+c==2) {
                    int x,y;
                    cout<<"? 2 1"<<endl;cin>>x;
                    cout<<"? 1 3"<<endl;cin>>y; 
                    if(a==x && c!=y) cout<<"! 3"<<endl;
                    if(a!=x && c==y) cout<<"! 2"<<endl;
                    if(a!=x && c!=y) cout<<"! 1"<<endl;
                }
                else {
                    int flag=0;
                    for(int i=4;i<=n-2;i+=2) {
                        int x,y;
                        cout<<"? "<<i<<" "<<i+1<<endl;
                        cin>>x;
                        cout<<"? "<<i+1<<" "<<i<<endl;
                        cin>>y;
                        if(x!=y) {
                            Output(i,i+1);
                            flag=1;
                            break;
                        }
                    }
                    if(!flag) {
                        Output(n-1,n);
                    }
                }
            }
            else {
                int flag=0;
                for(int i=1;i<=n-2;i+=2) {
                    int x,y;
                    cout<<"? "<<i<<" "<<i+1<<endl;
                    cin>>x;
                    cout<<"? "<<i+1<<" "<<i<<endl;
                    cin>>y;
                    if(x!=y) {
                        Output(i,i+1);
                        flag=1;
                        break;
                    }
                }
                if(!flag) {
                    Output(n-1,n);
                }
            }
        }
        return 0;
    }
}
signed main() {
    yhy::main();
    return 0;
}