QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#788520#9570. Binary TreeJinTianHaoWA 1ms5824kbC++172.4kb2024-11-27 17:17:092024-11-27 17:17:10

Judging History

你现在查看的是最新测评结果

  • [2024-11-27 17:17:10]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:5824kb
  • [2024-11-27 17:17:09]
  • 提交

answer

#include <bits/stdc++.h>
#define inf 1000000007
#define mod 1000000007
// #define int long long
// #pragma GCC optimize("Ofast","inline","-ffast-math")
// #pragma GCC target("avx,sse2,sse3,sse4,mmx")
using namespace std;
template <typename T> void read(T &x){
	x=0;char ch=getchar();int fh=1;
	while (ch<'0'||ch>'9'){if (ch=='-')fh=-1;ch=getchar();}
	while (ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
	x*=fh;
}
template <typename T> void write(T x) {
	if (x<0) x=-x,putchar('-');
	if (x>9) write(x/10);
	putchar(x%10+'0');
}
template <typename T> void writeln(T x) {
	write(x);
	puts("");
}
int n,cnt;
int h[100005],e[200005],ne[200005],idx;
bool cut[200005];
vector<int> vis;
void add(int a,int b)
{
	e[idx]=b,cut[idx]=0,ne[idx]=h[a],h[a]=idx++;
}
int rt,mn,all;
int siz[100005];
void dfs(int x,int fa)
{
	vis.push_back(x);
	siz[x]=1;
	for(int i=h[x];~i;i=ne[i])
	{
		int j=e[i];
		if(i==fa||cut[i]) continue;
		dfs(j,i^1);
		siz[x]+=siz[j];
		if(mn>max(siz[j],all-siz[j]))
			mn=max(siz[j],all-siz[j]),rt=i;
	}
}
int ask(int u,int v)
{
	++cnt;
	if(u==3&&v==5)
	{
		writeln(n);
		for(int i=0;i<idx;i+=2)
			write(e[i]),putchar(' '),write(e[i^1]),putchar(',');
		exit(0);
	}
	if(cnt>(int)log2(n)) assert(0);
	printf("? %d %d\n",u,v);
	fflush(stdout);
	int ret;
	read(ret);
	return ret;
}
int solve()
{
	if(all==1) return e[rt];
	if(all==2)
	{
		if(ask(e[rt],e[rt^1])) rt^=1;
		return e[rt];
	}
	if(all==3)
	{
		sort(vis.begin(),vis.end(),[](int a,int b){return siz[a]<siz[b];});
		if(siz[vis[1]]==2)
		{
			int r=ask(vis[0],vis[2]);
			if(r==0) return vis[0];
			if(r==1) return vis[1];
			if(r==2) return vis[2];
		}
		else
		{
			int r=ask(vis[0],vis[1]);
			if(r==0) return vis[0];
			if(r==1) return vis[2];
			if(r==2) return vis[1];
		}
	}
	if(ask(e[rt],e[rt^1]))
		all=all-siz[e[rt]],mn=all,rt^=1;
	else
		all=siz[e[rt]],mn=all;
	cut[rt]=cut[rt^1]=1;vis.clear();
	dfs(e[rt],-1);
	return solve();
}
signed main()
{
	// freopen(".in","r",stdin);
	// freopen(".out","w",stdout);
	int tc;read(tc);
	while(tc--)
	{
		cnt=0;
		read(n);idx=0;
		for(int i=1;i<=n;++i)
			h[i]=-1;
		for(int i=1;i<=n;++i)
		{
			int x,y;
			read(x);read(y);
			if(x) add(x,i),add(i,x);
			if(y) add(i,y),add(y,i);
		}
		rt=0;mn=all=n;vis.clear();
		dfs(e[rt],-1);
		printf("! %d\n",solve());
		fflush(stdout);
	}
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 5824kb

input:

2
5
0 0
1 5
2 4
0 0
0 0
2
1
2
0 2
0 0
2

output:

? 3 2
? 5 1
! 2
? 1 2
! 2

result:

ok OK (2 test cases)

Test #2:

score: -100
Wrong Answer
time: 0ms
memory: 3996kb

input:

5555
8
2 0
8 6
0 0
3 0
0 0
7 0
0 0
5 4
2
0
2
8
0 0
1 4
2 0
0 0
7 8
0 0
3 0
6 0
0
2
0
8
5 8
0 0
1 7
0 0
0 0
4 2
0 0
6 0
0
2
0
5
4 5
3 1
0 0
0 0
0 0
2
1
8
0 0
0 0
5 6
0 0
1 4
2 0
3 8
0 0
2
2
2
5
3 0
5 1
0 0
0 0
4 0
2
1
5
5 0
0 0
0 0
3 0
2 4
2
2
3
3 0
1 0
0 0
2
2
2 0
0 0
0
3
2 3
0 0
0 0
0
10
2 8
9 7
0 ...

output:

? 8 2
? 6 2
? 7 6
! 6
? 7 3
? 8 5
? 7 5
! 7
? 8 1
? 2 6
? 8 4
! 8
? 2 1
? 5 4
! 1
? 5 3
? 7 3
? 2 3
! 3
? 5 2
? 3 2
! 1
? 4 5
? 2 1
! 1
? 2 3
! 3
? 2 1
! 2
? 3 2
! 3
? 7 2
? 9 2
? 8 2
! 8
? 2 1
! 1
? 5 7
? 2 9
? 1 7
! 7
? 10 5
? 7 3
? 5 1
! 1
? 8 9
? 3 8
? 5 6
! 5
? 1 2
! 1
? 4 5
7
3 2,4 5,1 4,5 3,6...

result:

wrong answer Token parameter [name=op] equals to "7", doesn't correspond to pattern "[?!]{1,1}" (test case 17)