题解:CF2022D2 Asesino (Hard Version)
Annihilation_y · · 题解
原来我的证明是对的。被同学反复质疑了一个早上。感觉早上的课都听得不是很认真。
思路
我们把对
玩一玩表格可以发现,如果
然后赛时成功想出了
-
对于偶数来说,可以通过至多
n-2 次的询问,每次i 和i+1 连双向边,来确定内鬼到底在哪两个点中。再把两个点中的其中一个点与已经确定的不是内鬼的点连双向边,就可以求出内鬼是哪个,总共的次数是n 次。 -
可是对于奇数来说,我们用至多
n-1 次询问确定了内鬼在哪里,总次数就是n+1 ,显然不够优秀。首先n=3 是肯定优化不了了。对于n=5 ,我们采用询问同学打表可以发现是可以优化的。 -
具体做法如下:我们先把前三个点连成一个环,显然我们是可以判断环中有没有内鬼的。如果有,再用两次把内鬼找出来,如果没有,内鬼就在剩下两个当中,用两次找出来。容易发现,
n \ge 5 的奇数都可以这么干。这样,我们对于所有n > 3 的数都可以用n 次解决了。
现在,我们需要证明,我们不能用
由于只有
假设不是一个点,那么对于这个子图,我们对所有的边的权值都设成
-
入度为
0 的点是内鬼,其他的是骗子。 -
出度为
0 的点是内鬼,其他的是好人。
所以是判断不了的。
假设是一个点,那么相当于用
证明部分可能不是特别严谨,如果有问题私信问我。
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;
}