QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#758942#9570. Binary Treewht11TL 0ms3524kbC++204.7kb2024-11-17 20:34:452024-11-17 20:34:45

Judging History

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

  • [2024-11-17 20:34:45]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3524kb
  • [2024-11-17 20:34:45]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
using PII = pair<int, int>;
#define endl "\n"
const int N = 2e5 + 5;
vector<int> G[N];
int siz[N], f[N], vis[N], cnt[N];
int lst = 0;
int mid = 0;
void dfs(int u, int fa)
{
    f[u] = fa;
    siz[u] = 1;
    int flag=1;
    for (auto v : G[u])
    {
        if (v == fa || vis[v])
            continue;
        dfs(v, u);
        if(siz[v]>lst/2) flag=0;
        siz[u] += siz[v];
    }
    if (vis[u])
        return;
    if(lst-siz[u]>lst/2) flag=0;
	if (flag)
        mid = u;
}
void del(int u, int fa)
{
    vis[u] = 1;
    cnt[u]--;
    cnt[fa]--;
    siz[u] = 1;
    for (auto v : G[u])
    {
        if (v == fa || vis[v])
            continue;del(v, u);
        siz[u] += siz[v];
    }
}
void solve()
{
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        G[i].clear();
        vis[i] = 0;
        f[i] = 0;
        siz[i] = 0;
        cnt[i]=0;
    }
    for (int i = 1; i <= n; i++)
    {
        int u, v;
        cin >> u >> v;
        if (v)
        {
            G[i].push_back(v);
            G[v].push_back(i);
            cnt[i]++;
            cnt[v]++;
        }
        if (u)
        {
            G[i].push_back(u);
            G[u].push_back(i);
            cnt[i]++;
            cnt[u]++;
        }
    }
    lst = n;
    int rt = 1;
    mid = 0;
    while (1)
    {
    	if(lst==1) {
		cout << "! " << rt << endl;
            cout.flush();
            return;
			}
        int x; 
        mid = rt;
        dfs(rt, 0);
        if (cnt[mid] == 3)
        {
            vector<pair<int, int>> ve;
            for (auto v : G[mid])
            {
                if (vis[v])
                    continue;
                if (v == f[mid])
                    ve.emplace_back(lst - siz[mid], v);
                else
                    ve.emplace_back(siz[v], v);
            }
            sort(ve.begin(), ve.end(), greater<pair<int, int>>());
            int mx1 = 0, mx2 = 0;
            int mx11 = 0, mx22 = 0;
            for (auto tmp : ve)
            {
                int val = tmp.first;
                int v = tmp.second;
                if (val > mx1)
                {
                    mx2 = mx1;
                    mx1 = val;
                    mx22 = mx11;
                    mx11 = v;
                }
                else if (val > mx2)
                {
                    mx2 = val;
                    mx22 = v;
                }
            }
            cout << "? " << mx11 << " " << mx22 << endl;
            cout.flush();
            cin >> x;
            if (x == 0)
            {

                del(mid, mx11);
                lst -= siz[mid];
                rt = mx11;
            }
            else if (x == 1)
            {
                del(mx22, mid);
                lst -= siz[mx22];
                del(mx11, mid);
                lst -= siz[mx11];
                rt = mid;
            }
            else
            {
                del(mid, mx22);
                lst -= siz[mid];
                rt = mx22;
            }
        }
        else if (cnt[mid] == 2)
        {
            vector<int> ve;
            for (auto v : G[mid])
            {
                if (vis[v])
                    continue;
                ve.push_back(v);
            }
            cout << "? " << ve[0] << " " << ve[1] << endl;
            cout.flush();
            cin >> x;
            if (x == 0)
            {
                del(mid, ve[0]);
                lst -= siz[mid];
                rt = ve[0];
            }
            else if (x == 1)
            {
                cout << "! " << mid << endl;
                return;
            }
            else
            {
                del(mid, ve[1]);
                lst -= siz[mid];
                rt = ve[1];
            }
        }
        else if (cnt[mid] == 1)
        {
			int xx; 
            for (auto v : G[mid])
            {
                if (vis[v])
                    continue;
                xx=v;
            }
            cout << "? " << mid <<" " << xx << endl;
            cout.flush();
            cin >>x;
            if(x==0)
            {
            	cout << "! " << mid << endl;
            	cout.flush();
			}
			else
			{
				cout << "! " <<xx<< endl;
            	cout.flush();
			}
            return;
        }
        else
        {
            cout << "! " << rt << endl;
            cout.flush();
            return;
        }
    }
}
signed main()
{
    IOS;
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

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

output:

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

result:

ok OK (2 test cases)

Test #2:

score: -100
Time Limit Exceeded

input:

5555
8
2 0
8 6
0 0
3 0
0 0
7 0
0 0
5 4
0
0
2
8
0 0
1 4
2 0
0 0
7 8
0 0
3 0
6 0
2
0
0
8
5 8
0 0
1 7
0 0
0 0
4 2
0 0
6 0
0
1
2
5
4 5
3 1
0 0
0 0
0 0
0
2
8
0 0
0 0
5 6
0 0
1 4
2 0
3 8
0 0
0
0
5
3 0
5 1
0 0
0 0
4 0
0
2
5
5 0
0 0
0 0
3 0
2 4
0
0
3
3 0
1 0
0 0
2
2
2 0
0 0
0
3
2 3
0 0
0 0
2
10
2 8
9 7
0 0
...

output:

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

result: