QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#731430#9570. Binary Treeucup-team1134#RE 1ms3612kbC++233.2kb2024-11-10 03:55:262024-11-10 03:55:27

Judging History

你现在查看的是最新测评结果

  • [2024-11-10 03:55:27]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:3612kb
  • [2024-11-10 03:55:26]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pair<int,int>>
#define vll vector<pair<ll,ll>>
#define vvi vector<vector<int>>
#define vvl vector<vector<ll>>
#define vvii vector<vector<pair<int,int>>>
#define vvll vector<vector<pair<ll,ll>>>
#define vst vector<string>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define mkunique(x) sort(all(x));(x).erase(unique(all(x)),(x).end())
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=998244353,MAX=300005,INF=15<<26;

//直径を返す

vector<int> G[MAX];
int dis[MAX][3];
vector<int> diameter;
bool alive[MAX];

void pre(int u,int p,int k){
    for(int to:G[u]){
        if(to==p) continue;
        dis[to][k]=dis[u][k]+1;
        pre(to,u,k);
    }
}

void make(int u,int p,int goal,bool &flag){
    if(u==goal){
        diameter.push_back(u);
        flag=true;
        return;
    }
    for(int to:G[u]){
        if(to==p) continue;
        make(to,u,goal,flag);
        if(flag){
            diameter.push_back(u);
            return;
        }
    }
}

int main(){
    
    int Q;cin>>Q;
    while(Q--){
        int N;cin>>N;
        for(int i=0;i<N;i++){
            G[i].clear();
            alive[i]=true;
        }
        for(int i=0;i<N;i++){
            int a,b;cin>>a>>b;
            if(a){
                G[i].pb(a-1);
                G[a-1].pb(i);
            }
            if(b){
                G[i].pb(b-1);
                G[b-1].pb(i);
            }
        }
        
        while(1){
            vi V;
            for(int i=0;i<N;i++){
                if(alive[i]) V.pb(i);
            }
            if(si(V)==1){
                cout<<"! "<<V[0]+1<<endl;
                break;
            }
            
            for(int i=0;i<N;i++){
                dis[i][0]=dis[i][1]=dis[i][2]=INF;
            }
            dis[0][V[0]]=0;
            pre(0,-1,0);
            pair<int,int> ma={0,0},ma2={0,0};
            for(int i=0;i<N;i++) if(alive[i]) chmax(ma,{dis[i][0],i});
            dis[ma.second][1]=0;
            pre(ma.second,-1,1);
            for(int i=0;i<N;i++) if(alive[i]) chmax(ma2,{dis[i][1],i});
            dis[ma2.second][2]=0;
            pre(ma2.second,-1,2);
            bool flag=false;
            
            cout<<"? "<<ma.se+1<<" "<<ma2.se+1<<endl;
            int c;cin>>c;
            for(int i=0;i<N;i++){
                if(alive[i]){
                    if(dis[i][1]<dis[i][2]){
                        if(c!=0) alive[i]=false;
                    }
                    if(dis[i][1]==dis[i][2]){
                        if(c!=1) alive[i]=false;
                    }
                    if(dis[i][1]>dis[i][2]){
                        if(c!=2) alive[i]=false;
                    }
                }
            }
        }
    }
    
    
    
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3612kb

input:

2
5
0 0
1 5
2 4
0 0
0 0
2
1
2
0 2
0 0
2

output:

? 4 5
? 5 1
! 2
? 2 1
! 1

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
0
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
0
0
5
4 5
3 1
0 0
0 0
0 0
2
0
8
0 0
0 0
5 6
0 0
1 4
2 0
3 8
0 0
2
0
5
3 0
5 1
0 0
0 0
4 0
2
2
5
5 0
0 0
0 0
3 0
2 4
2
2
3
3 0
1 0
0 0
2
2
2 0
0 0
2
3
2 3
0 0
0 0
0
10
2 8
9 7
0 0
...

output:

? 3 7
? 7 1
? 7 6
! 6
? 6 4
? 4 3
? 2 1
! 2
? 4 7
? 7 5
? 7 3
! 7
? 3 5
? 5 4
! 5
? 8 4
? 4 1
! 4
? 4 3
? 3 1
! 1
? 3 2
? 2 1
! 1
? 3 2
! 2
? 2 1
! 1
? 3 2
! 3
? 4 10
? 4 5
? 4 3
! 6
? 2 1
! 2
? 8 6
? 6 1
? 9 1
! 1
? 6 9
? 9 5
? 5 1
! 1
? 6 7
? 6 4
? 6 5
! 6
? 2 1
! 2
? 2 7
? 7 1
! 4
? 3 7
? 7 1
? 9...

result: