QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#730264 | #9570. Binary Tree | ucup-team3691# | WA | 1ms | 5628kb | C++20 | 4.0kb | 2024-11-09 19:29:31 | 2024-11-09 19:29:32 |
Judging History
answer
#include <iostream>
#include <vector>
#include <map>
using namespace std;
vector<int>adj[200005];
int c1=0,c2=0,sol=0,n;
int fre[100005];
int fiust[100005];
int fiudr[100005];
int par[100005];
int sub[100005];
int inf=1e6;
void dfs(int curr,int parent)
{
sub[curr]=1;
par[curr]=parent;
fiust[curr]=0;
fiudr[curr]=0;
for(auto k:adj[curr])
{
if(k!=parent && fre[k]==0)
{
dfs(k,curr);
sub[curr]+=sub[k];
if(fiust[curr]==0)
{
fiust[curr]=k;
}
else
{
fiudr[curr]=k;
}
}
}
}
int dist[100005][2];
void getdist(int curr,int par,int d)
{
for(auto k:adj[curr])
{
if(k!=par)
{
dist[k][d]=dist[curr][d]+1;
getdist(k,curr,d);
}
}
}
void taie(int a,int b)
{
int rez;
dist[a][0]=0;
dist[b][1]=0;
getdist(a,0,0);
getdist(b,0,1);
cout<<"? "<<a<<" "<<b<<endl;
cin>>rez;
if(rez==0)
{
for(int i=1;i<=n;i++)
{
if(dist[i][0]>=dist[i][1])
{
fre[i]=1;
}
}
}
else if (rez==1)
{
for(int i=1;i<=n;i++)
{
if(dist[i][0]!=dist[i][1])
{
fre[i]=1;
}
}
}
else
{
for(int i=1;i<=n;i++)
{
if(dist[i][0]<=dist[i][1])
{
fre[i]=1;
}
}
}
}
int findbest()
{
int i,root=0,tot,a,b,maxim;
for(i=1;i<=n;i++)
{
int vec=0;
for(auto k:adj[i])
{
if(fre[k]==0)
{
vec++;
}
}
if(fre[i]==0 && vec==1)
{
root=i;
}
}
par[root]=0;
dfs(root,0);
if(sub[root]==1)
{
cout<<"! "<<root<<endl;
return 1;
}
tot=sub[root];
int nod1=0,nod2=0,best=tot+1;
for(i=1;i<=n;i++)
{
if(fre[i]==0 && fre[fiust[i]]==0 && fre[fiudr[i]]==0)
{
a=fiust[i];
b=fiudr[i];
maxim=max(tot-sub[a]-sub[b],max(sub[a],sub[b]));
if(maxim<best)
{
nod1=a;
nod2=b;
best=maxim;
}
}
if(fre[i]==0 && fre[par[i]]==0)
{
a=i;
b=par[i];
maxim=max(sub[i],tot-sub[i]);
if(maxim<best)
{
nod1=a;
nod2=b;
best=maxim;
}
}
if(fre[i]==0 && fre[par[par[i]]]==0)
{
a=i;
b=par[par[i]];
maxim=max(max(sub[i],tot-sub[par[i]]),sub[par[i]]-sub[i]);
if(maxim<best)
{
nod1=a;
nod2=b;
best=maxim;
}
}
}
taie(nod1,nod2);
return 0;
}
int main()
{
int i,j,k,l,t,op,m,a,b,xx;
//ios_base::sync_with_stdio(false);
//cin.tie(NULL);
cin>>t;
for(op=1; op<=t; op++)
{
cin>>n;
for(i=1; i<=n; i++)
{
fre[i]=0;
}
fre[0]=1;
for(i=1; i<=n; i++)
{
cin>>a>>b;
if(a!=0)
{
adj[i].push_back(a);
adj[a].push_back(i);
fiust[i]=a;
par[a]=i;
}
if(b!=0)
{
adj[i].push_back(b);
adj[b].push_back(i);
fiudr[i]=b;
par[b]=i;
}
}
int plm=0;
while(findbest()==0)
{
plm++;
}
for(i=1;i<=n;i++)
{
adj[i].clear();
fiudr[i]=0;
fiust[i]=0;
par[i]=0;
fre[i]=0;
}
}
return 0;
}
详细
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 5628kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 2 0
output:
? 1 3 ? 3 4 ! 0
result:
wrong answer Integer parameter [name=ans] equals to 0, violates the range [1, 5] (test case 1)