跳到正文

题解

题解:CF2022D2 Asesino (Hard Version)

Annihilation_y约 5 分钟
原文
文章信息与版本

文章信息

发布
2025年4月17日
收录
2025年12月12日
更新
2026年11月10日
篇幅
约 5 分钟
获赞
4
题目
CF2022D2

版本

  1. v1当前

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

思路

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

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

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

  • 对于偶数来说,可以通过至多 n−2n-2 次的询问,每次 ii 和 i+1i+1 连双向边,来确定内鬼到底在哪两个点中。再把两个点中的其中一个点与已经确定的不是内鬼的点连双向边,就可以求出内鬼是哪个,总共的次数是 nn 次。

  • 可是对于奇数来说,我们用至多 n−1n-1 次询问确定了内鬼在哪里,总次数就是 n+1n+1,显然不够优秀。首先 n=3n=3 是肯定优化不了了。对于 n=5n=5,我们采用询问同学打表可以发现是可以优化的。

  • 具体做法如下:我们先把前三个点连成一个环,显然我们是可以判断环中有没有内鬼的。如果有,再用两次把内鬼找出来,如果没有,内鬼就在剩下两个当中,用两次找出来。容易发现,n≥5n \ge 5 的奇数都可以这么干。这样,我们对于所有 n>3n > 3 的数都可以用 nn 次解决了。

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

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

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

  • 入度为 00 的点是内鬼,其他的是骗子。

  • 出度为 00 的点是内鬼,其他的是好人。

所以是判断不了的。

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

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

Code

C++
#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;
}
原文
存档评论3 条抓取于