QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#730168 | #9570. Binary Tree | ucup-team3691# | ML | 2ms | 5752kb | C++20 | 3.9kb | 2024-11-09 19:00:23 | 2024-11-09 19:00:23 |
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++)
{
if(fre[i]==0)
{
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;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 5752kb
input:
2 5 0 0 1 5 2 4 0 0 0 0 2 0 2 0 2 0 0 2
output:
? 1 3 ? 3 4 ! 3 ? 1 2 ! 2
result:
ok OK (2 test cases)
Test #2:
score: -100
Memory Limit Exceeded
input:
5555 8 2 0 8 6 0 0 3 0 0 0 7 0 0 0 5 4 2 2 0 8 0 0 1 4 2 0 0 0 7 8 0 0 3 0 6 0 2 2 0 8 5 8 0 0 1 7 0 0 0 0 4 2 0 0 6 0 2 1 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 0 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 2 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:
? 1 8 ? 3 8 ? 5 8 ! 5 ? 2 7 ? 5 8 ? 6 8 ! 6 ? 1 8 ? 2 8 ? 4 6 ! 4 ? 4 2 ? 1 5 ! 1 ? 5 6 ? 1 4 ! 5 ? 1 5 ? 1 3 ! 3 ? 1 4 ? 3 4 ! 3 ? 2 3 ! 3 ? 1 2 ! 2 ? 2 3 ! 1 ? 1 7 ? 3 7 ? 4 6 ! 4 ? 1 2 ! 2 ? 5 9 ? 4 5 ? 3 8 ! 3 ? 3 10 ? 2 8 ? 8 10 ! 10 ? 3 9 ? 1 9 ? 2 7 ! 7 ? 1 2 ! 2 ? 3 4 ? 1 7 ! 4 ? 4 9 ? 2 8 ?...