QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#734404 | #9570. Binary Tree | MiniLong | RE | 0ms | 0kb | C++14 | 5.4kb | 2024-11-11 09:53:55 | 2024-11-11 09:53:55 |
answer
#include <bits/stdc++.h>
#define _rep(i, x, y) for(int i = x; i <= y; ++i)
#define _req(i, x, y) for(int i = x; i >= y; --i)
#define _rev(i, u) for(int i = head[u]; i; i = e[i].nxt)
#define pb push_back
#define fi first
#define se second
#define mst(f, i) memset(f, i, sizeof f)
using namespace std;
#ifdef ONLINE_JUDGE
#define debug(...) 0
#else
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#endif
typedef long long ll;
typedef pair<int, int> PII;
namespace fastio{
#ifdef ONLINE_JUDGE
char ibuf[1 << 20],*p1 = ibuf, *p2 = ibuf;
#define get() p1 == p2 && (p2 = (p1 = ibuf) + fread(ibuf, 1, 1 << 20, stdin), p1 == p2) ? EOF : *p1++
#else
#define get() getchar()
#endif
template<typename T> inline void read(T &t){
T x = 0, f = 1;
char c = getchar();
while(!isdigit(c)){
if(c == '-') f = -f;
c = getchar();
}
while(isdigit(c)) x = x * 10 + c - '0', c = getchar();
t = x * f;
}
template<typename T, typename ... Args> inline void read(T &t, Args&... args){
read(t);
read(args...);
}
template<typename T> void write(T t){
if(t < 0) putchar('-'), t = -t;
if(t >= 10) write(t / 10);
putchar(t % 10 + '0');
}
template<typename T, typename ... Args> void write(T t, Args... args){
write(t), putchar(' '), write(args...);
}
template<typename T> void writeln(T t){
write(t);
puts("");
}
template<typename T> void writes(T t){
write(t), putchar(' ');
}
#undef get
};
using namespace fastio;
#define multitest() int T; read(T); _rep(tCase, 1, T)
namespace Calculation{
const ll mod = 998244353;
ll ksm(ll p, ll h){ll base = p % mod, res = 1; while(h){if(h & 1ll) res = res * base % mod; base = base * base % mod, h >>= 1ll;} return res;}
void dec(ll &x, ll y){x = ((x - y) % mod + mod) % mod;}
void add(ll &x, ll y){x = (x + y) % mod;}
void mul(ll &x, ll y){x = x * y % mod;}
ll sub(ll x, ll y){return ((x - y) % mod + mod) % mod;}
ll pls(ll x, ll y){return ((x + y) % mod + mod) % mod;}
ll mult(ll x, ll y){return x * y % mod;}
}
using namespace Calculation;
const int N = 1e5 + 5;
int n, T;
vector<int> G[N];
int tot, rt, siz[N], maxn[N];
bool flag, vis[N];
void findrt(int u, int fa){
siz[u] = 1, maxn[u] = 0;
for(auto &v : G[u]){
if(v == fa || vis[v]) continue;
findrt(v, u), siz[u] += siz[v];
maxn[u] = max(maxn[u], siz[v]);
}
maxn[u] = max(maxn[u], tot - siz[u]);
if(maxn[u] < maxn[rt]) rt = u, flag = maxn[u] == tot / 2;
}
int ask(int x, int y){cout << "? " << x << ' ' << y << endl; int t; cin >> t; return t;}
void del(int u, int fa){
vis[u] = 1;
for(auto &v : G[u]) if(v != fa) del(v, u);
}
int main(){
cin >> T;
while(T--){
cin >> n;
vector<PII> e;
vector<int> p;
_rep(i, 1, n){
p.pb(i);
int x, y; cin >> x >> y;
if(x) G[i].pb(x), G[x].pb(i), e.pb({i, x});
if(y) G[i].pb(y), G[y].pb(i), e.pb({i, y});
}
int res = 0;
auto work = [&]() -> void{
vector<PII> cur;
for(auto &i : p) G[i].clear();
for(auto &[u, v] : e) if(!vis[u] && !vis[v]) G[u].pb(v), G[v].pb(u), cur.pb({u, v});
swap(e, cur);
vector<int> np;
for(auto &i : p) if(!vis[i]) np.pb(i);
swap(p, np);
};
tot = n;
while(tot > 1){
tot = p.size(), maxn[rt = 0] = 2e9, flag = 0, findrt(p[0], 0);
if(tot == 1) break;
// debug("rt:%d flag:%d tot:%d\n", rt, flag, tot);
if(flag && tot % 2 == 0){
int u = rt, v = 0;
for(auto &i : G[u]) if(siz[i] == tot / 2) v = i;
for(auto &i : G[u]) if(siz[i] > siz[u] && siz[u] == tot / 2) v = i;
int t = ask(u, v);
if(!t) swap(u, v);
del(u, v); work();
continue;
}
if(G[rt].size() == 1){
int u = rt, v = G[rt][0];
int t = ask(u, v);
p.clear(); if(t) p.pb(v); else p.pb(u);
break;
}
if(G[rt].size() == 2){
int u = G[rt][0], v = G[rt][1];
int t = ask(u, v);
if(t == 1){
p.clear(), p.pb(rt);
break;
}
if(t == 2) swap(u, v);
del(rt, u); work();
continue;
}
PII nxt[] = {{G[rt][0], 0}, {G[rt][1], 0}, {G[rt][2], 0}};
_rep(i, 0, 2){
int v = nxt[i].fi;
if(siz[v] > siz[rt]) nxt[i].se = tot - siz[rt];
else nxt[i].se = siz[v];
}
sort(nxt, nxt + 3, [](PII x, PII y){return x.se > y.se;});
int u = nxt[0].fi, v = nxt[1].fi;
int t = ask(u, v);
if(t == 0) del(rt, u);
if(t == 1) del(u, rt), del(v, rt);
if(t == 2) del(rt, v);
work();
}
res = p[0];
cout << res << endl;
_rep(i, 0, n) vis[i] = siz[i] = maxn[i] = 0, G[i].clear();
tot = rt = n = 0;
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
input:
2 5 0 0 1 5 2 4 0 0 0 0 1 0
output:
? 3 1 ? 5 2 5