QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#828508 | #9570. Binary Tree | ChocolaOwO | RE | 2ms | 15412kb | C++14 | 2.9kb | 2024-12-23 17:23:51 | 2024-12-23 17:23:51 |
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;
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 = 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 TLE() {
ll c = 100000000000ll;
int res = 0;
while(c --) {
res = res + 1 + res;
}
cout << res << '\n';
}
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 if(t == 1) exit(1);
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;
// cerr << "* " << now << ' ' << fx << '\n';
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;
}
详细
Test #1:
score: 100
Accepted
time: 2ms
memory: 15412kb
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 ...