QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#776137#9570. Binary TreeO_startWA 0ms11348kbC++203.3kb2024-11-23 17:40:592024-11-23 17:41:00

Judging History

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

  • [2024-11-23 17:41:00]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:11348kb
  • [2024-11-23 17:40:59]
  • 提交

answer

#include<iostream>
#include<vector>
#include<unordered_set>
using namespace std;
int t, n, x, y;
int cur_n;

struct Node{
    vector<int> to;
    int sz, fa, del;
}node[200005];

void init(){
    for(int i = 0;i <= n;i++){
        node[i].to.clear();
        node[i].del = 0;
    }
    cur_n = n;
}

int dfs(int cur, int fa){
    node[cur].fa = fa;
    int sz = 1;
    for(int to_p : node[cur].to){
        if(to_p == fa) continue;
        if(node[to_p].del) continue;
        sz += dfs(to_p, cur);
    }
    node[cur].sz = sz;
    return sz;
}

void dfs_tag(int cur, int fa, unordered_set<int>& st){
    st.insert(cur);
    for(int to_p : node[cur].to){
        if(to_p == fa || node[to_p].del) continue;
        dfs_tag(to_p, cur, st);
    }
}

int get_p(){
    for(int i = 1;i <= n;i++){
        if(node[i].del) continue;
        bool flag = true;
        for(int to_p : node[i].to){
            if(node[to_p].del) continue;
            int sz = node[to_p].sz;
            //cout<<to_p<<" "<<sz<<'\n';
            if(sz > node[i].sz) sz = sz - node[i].sz;
            //cout<<to_p<<" "<<sz<<" "<<cur_n<<'\n';
            if(sz > cur_n/2) flag = false;
        }

        if(flag) return i;
    }
}

int qr(int u, int v){
    cout << "? " << u << ' ' << v << '\n';
    fflush(stdout);
    int ret;
    cin >> ret;
    return ret;
}

int get_sz(int p, int p1){
    int ret = node[p].sz;
    if(ret > node[p1].sz) ret = ret - node[p1].sz;
    return ret;
}

void solve(){
    while(cur_n > 1){
        int rt;
        for(int i = 1;i <= n;i++){
            if(node[i].del == 0){
                rt = i;
                break;
            }
        }
        dfs(rt, rt);
        int p1 = get_p();
        //cout<<p1<<'\n';
        vector<int> buf;
        for(int to_p : node[p1].to){
            if(!node[to_p].del) buf.push_back(to_p);
        }
        int du = buf.size();
        buf.push_back(p1);
        if(du == 3){
            if(get_sz(buf[1], p1) < get_sz(buf[2], p1)) swap(buf[1], buf[2]);
            if(get_sz(buf[0], p1) < get_sz(buf[1], p1)) swap(buf[0], buf[1]);
            if(get_sz(buf[1], p1) < get_sz(buf[2], p1)) swap(buf[1], buf[2]);
        }
        int res = qr(buf[0], buf[1]);
        int tar;
        if(res == 0){
            tar = buf[0];
            node[buf[1]].del = 1;
            node[p1].del = 1;
        }
        else if(res == 1){
            tar = p1;
            node[buf[0]].del = 1;
            node[buf[1]].del = 1;
        }
        else{
            tar = buf[1];
            node[buf[0]].del = 1;
            node[p1].del = 1;
        }
        unordered_set<int> st;
        dfs_tag(tar, tar, st);
        cur_n = st.size();
        for(int i = 1;i <= n;i++){
            if(!st.count(i)) node[i].del = 1;
        }
        if(cur_n == 1){
            cout << "! " << (*(st.begin())) << '\n';
            fflush(stdout);
            return;
        }
    }
}

int main(){
    cin >> t;
    while(t--){
        cin >> n;
        init();
        for(int i = 1;i <= n;i++){
            cin >> x >> y;
            if(x != 0) node[i].to.push_back(x);
            if(y != 0) node[i].to.push_back(y);
            if(x != 0) node[x].to.push_back(i);
            if(y != 0) node[y].to.push_back(i);
        }
        solve();
    }
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 11316kb

input:

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

output:

? 3 1
? 5 2
! 5
? 2 1
! 1

result:

ok OK (2 test cases)

Test #2:

score: -100
Wrong Answer
time: 0ms
memory: 11348kb

input:

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

output:

? 8 6
? 3 8
? 8 5
! 8
? 2 7
? 7 8
? 8 6
! 8
? 8 3
? 6 4
? 2 8
! 6
? 2 4
? 3 2
! 3
? 6 2
? 5 7
? 5 4
? 5 1

result:

wrong answer Too many queries (test case 5)