QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#828514#9570. Binary TreeChocolaOwORE 0ms16136kbC++142.9kb2024-12-23 17:38:092024-12-23 17:38:10

Judging History

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

  • [2024-12-23 17:38:10]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:16136kb
  • [2024-12-23 17:38:09]
  • 提交

answer

#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int,int> PII;

const int N = 2e5+5;

int n, cnt, vis[N], siz[N], sy;
int xw[N][3], now;
int dp[N][3], path[N][3];
vector<int> g[N];

void TLE() {
	ll c = 100000000000ll;
	int res = 0;
	while(c --) {
		res = res + 1 + res;
		res %= 12341231;
	}
	cout << res << '\n';
}

void dfs(int u, int fa) {
	dp[u][0] = dp[u][1] = dp[u][2] = 0;
	path[u][0] = path[u][1] = path[u][2] = 0;
	xw[u][0] = xw[u][1] = xw[u][2] = 0;
	siz[u] = 1;
	int t = 0;
	for(auto x : g[u]) {
		if(x == fa) continue;
		if(!vis[x]) continue;
		dfs(x,u);
		siz[u] += siz[x];
		dp[u][t] = siz[x];
		path[u][t] = x;
		t ++;
	}
	if(fa) {
		dp[u][t] = cnt - siz[u];
		path[u][t] = fa;
	}
	int pos = -1, minn = 1e9, res = 0;
	for(int i=0;i<3;i++) {
		if(minn > dp[u][i]) minn = dp[u][i], pos = i;
	}
	if(pos == -1) TLE();
	dp[u][pos] ++;
	t = 0;
	for(int i=0;i<3;i++) {
		res = max(res, dp[u][i]);
		if(i == pos) {
			xw[u][2] = path[u][i];
			continue;
		}
		xw[u][t] = path[u][i];
		t ++;
	}
	if(res < sy) {
		now = u;
		sy = res;
	}
}

void dfs2(int u, int fa) {
	vis[u] = 0;
	cnt --;
	for(int x : g[u]) {
		if(x == fa) continue;
		if(vis[x] == 0) break; 
		dfs2(x,u);
	}
}



void solve() {
	cin >> n;
	for(int i=1;i<=n;i++) {
		g[i].clear();
	}
	for(int i=1;i<=n;i++) {
		vis[i] = 1;
		int x, y;
		cin >> x >> y;
		if(x) g[x].push_back(i), g[i].push_back(x);
		if(y) g[y].push_back(i), g[i].push_back(y);
	}
	cnt = n;
	int st = 1;
	while(cnt > 1) {
		if(cnt == 2) {
			int x = 0, y = 0, t;
			for(int i=1;i<=n;i++) {
				if(vis[i]) {
					if(x) y = i;
					else x = i;
				}
			}
			if(x == 0 || y == 0) TLE();
			cout << "? " << x << ' ' << y << endl;
			cin >> t;
			if(t == 0) cout << "! " << x << endl;
			else cout << "! " << y << endl;
			return ;
		}
		if(st == 0 || st > n) TLE();
		sy = cnt;
//		cerr << "#" << ' ' << st << ' ' << sy << '\n';
		dfs(st,0);
		int t;
		cout << "? " << xw[now][0] << ' ' << xw[now][1] << endl;
		cin >> t;
		int fx;
		if(t == 0) {
			vis[now] = 0;
			cnt --;
			fx = xw[now][0];
			st = fx;
			for(auto x : g[now]) {
				if(x == fx) continue;
				if(vis[x] == 0) continue;
				dfs2(x,now);
			}
		} else if(t == 2) {
			vis[now] = 0;
			cnt --;
			fx = xw[now][1];
			st = fx;
//			cerr << "* " << now << ' ' << fx << '\n';
			for(auto x : g[now]) {
				if(x == fx) continue;
				if(vis[x] == 0) continue;
				dfs2(x,now);
			}
		} else {
			st = now;
			fx = xw[now][2];
//			cerr << "* " << now << ' ' << fx << '\n';
			for(auto x : g[now]) {
				if(x == fx) continue;
				if(vis[x] == 0) continue;
				dfs2(x,now);
			}
		}
	}
	for(int i=1;i<=n;i++) {
		if(vis[i]) {
			cout << "! " << i << endl;
			return ;
		}
	}
//	TLE();
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	int t = 1;
	cin >> t;
	while(t --) solve();
	
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 16136kb

input:

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

output:

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

result:

ok OK (2 test cases)

Test #2:

score: -100
Runtime Error

input:

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

output:

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

result: