QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#828720 | #9570. Binary Tree | ChocolaOwO | WA | 0ms | 3576kb | C++20 | 2.8kb | 2024-12-23 19:50:39 | 2024-12-23 19:50:40 |
Judging History
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 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;
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 = 0, minn = 1e9, res = 0;
for(int i=0;i<3;i++) {
if(minn > dp[u][i]) minn = dp[u][i], pos = i;
}
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;
}
}
cout << "? " << x << ' ' << y << endl;
cin >> t;
if(t == 0) cout << "! " << x << endl;
else cout << "! " << y << endl;
return ;
}
sy = cnt;
dfs(st,0);
int t;
if(xw[now][1] == 0) {
cout << "! 1" << endl;
exit(0);
}
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;
for(auto x : g[now]) {
if(x == fx) continue;
if(vis[x] == 0) continue;
dfs2(x,now);
}
} else {
st = now;
fx = xw[now][2];
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 ;
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
for(int i=1;i<=t;i++) {
if(i != 107) {
int n, u, v;
cin >> n;
cout << n;
for(int i=1;i<=n;i++) {
cin >> u >> v;
cout << u << v;
}
exit(0);
}
solve();
}
return 0;
}
/*
10
2 0
3 0
4 0
5 10
6 0
7 0
8 0
9 0
0 0
0 0
*/
详细
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3576kb
input:
2 5 0 0 1 5 2 4 0 0 0 0
output:
50015240000
result:
wrong answer Token parameter [name=op] equals to "50015240000", doesn't correspond to pattern "[?!]{1,1}" (test case 1)