QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#805064 | #9570. Binary Tree | Kogenta2010 | TL | 0ms | 7308kb | C++14 | 3.7kb | 2024-12-08 12:40:39 | 2024-12-08 12:40:40 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
int n;
bool mark[maxn],p[maxn];
struct edge{
int to;
int block_size;
edge(int to_=0,int sz_=0):to(to_),block_size(sz_){}
bool const operator <(const edge &b){
if(mark[to]){
if(mark[b.to])return 0;
else return 1;
}
else if(mark[b.to])return 0;
else return block_size<b.block_size;
}
};
vector<edge>v[maxn];
int edge_cnt[maxn];
void build_edge(int x,int y){
if(x<=0||x>n||y==0||y>n||x==y)return ;
if(v[x].size()<=edge_cnt[x]){
v[x].push_back((edge){y,0});
edge_cnt[x]++;
}
else{
v[x][edge_cnt[x]].to=y;
v[x][edge_cnt[x]++].block_size=0;
}
if(v[y].size()<=edge_cnt[y]){
v[y].push_back((edge){x,0});
edge_cnt[y]++;
}
else{
v[y][edge_cnt[y]].to=x;
v[y][edge_cnt[y]++].block_size=0;
}
return ;
}
edge fa[maxn];
stack<int>RemainNode;
queue<int>SelectNode;
void dfs(int x,int prev,int EdgeID){
if(mark[x]||x>n)return ;
//cout<<"Now Goes to "<<x<<" (Prev:"<<prev<<","<<EdgeID<<")"<<endl;
//system("pause");
fa[x].to=prev;
RemainNode.push(x);
if(x!=prev){
v[prev][EdgeID].block_size=1;
}
for(int i=0;i<edge_cnt[x];i++){
if(v[x][i].to==prev){
fa[x].block_size=i;
continue;
}
dfs(v[x][i].to,x,i);
if(x!=prev)v[prev][EdgeID].block_size+=v[x][i].block_size;
}
}
struct cmp_for_block{
bool operator()(const int &a,const int &b){
return v[a][edge_cnt[a]-1].block_size>v[b][edge_cnt[b]-1].block_size;
}
};
priority_queue<int,vector<int>,cmp_for_block>blocks;
int main(){
int t;
cin>>t;
while(t--){
//cout<<"Remain "<<t<<" Group(s)"<<endl;
while(!blocks.empty()){
blocks.pop();
}
while(!RemainNode.empty()){
RemainNode.pop();
}
while(!SelectNode.empty()){
SelectNode.pop();
}
int x,y;
cin>>n;
for(int i=1;i<=n;i++){
edge_cnt[i]=0;
mark[i]=0;
p[i]=0;
}
for(int i=1;i<=n;i++){
cin>>x>>y;
build_edge(x,i);
build_edge(y,i);
if(x!=0)p[x]=1;
if(y!=0)p[y]=1;
}
int rt=0;
for(int i=1;i<=n;i++){
if(!p[i]){
rt=i;
break;
}
}
while(1){
if(mark[rt]){
bool chg=0;
for(int i=0;i<edge_cnt[rt];i++){
if(mark[v[rt][i].to]==0){
rt=v[rt][i].to;
chg=1;
break;
}
}
if(!chg)break;
}
dfs(rt,rt,-1);
int sz=RemainNode.size();
if(sz<=1){
break;
}
int now;
while(!RemainNode.empty()){
now=RemainNode.top();
RemainNode.pop();
if(!RemainNode.empty())v[now][fa[now].block_size].block_size=sz-1;
for(int i=0;i<edge_cnt[now];i++){
if(i==fa[now].block_size)continue;
if(!RemainNode.empty())v[now][fa[now].block_size].block_size-=v[now][i].block_size;
}
sort(v[now].begin(),v[now].begin()+edge_cnt[now]);
blocks.push(now);
}
now=blocks.top();
//cout<<"Found Center: "<<now<<endl;
while(!blocks.empty()){
blocks.pop();
}
for(int i=edge_cnt[now]-1;i>=0;i--){
if(mark[v[now][i].to])break;
SelectNode.push(v[now][i].to);
if(SelectNode.size()==2)break;
}
int ask1=SelectNode.front(),ask2,ans;
SelectNode.pop();
if(SelectNode.empty()){
ask2=now;
}
else{
ask2=SelectNode.front();
SelectNode.pop();
}
cout<<"? "<<ask1<<' '<<ask2<<endl;
cout.flush();
cin>>ans;
if(ans==1){
mark[ask1]=mark[ask2]=1;
rt=now;
}
else{
if(ans==0){
mark[now]=1;
rt=ask1;
}
else if(ans==2){
if(ask2==now){
mark[ask1]=1;
rt=now;
}
else{
mark[now]=1;
rt=ask2;
}
}
}
}
cout<<"!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