题解:P10553 [ICPC 2024 Xi'an I] Guess The Tree

· · 题解

题意:

给定一棵高度为 n,有 2^n-1 个点组成的满二叉树。你需要通过不超过 4800 个询问来得知这棵二叉树每一个节点的父亲的编号。

询问的格式为 ? a b,代表询问编号为 a 的点与编号为 b 的点的 \operatorname{lca} 的编号。

思路

这道题的过程可以分成以下几个部分:

  1. 找到一个叶子节点 l
  2. 找到根到 l 的路径上的所有点
  3. 根据与 l 的 \operatorname{lca} 是路径上的哪一个节点划分出一个个子树
  4. 递归处理子树

实现

找叶子

对于找叶子节点,可以定义一个候选叶子节点 \operatorname{leaf},然后遍历所有节点,如果枚举到了一个节点比当前的 \operatorname{leaf} 更深,则将 \operatorname{leaf} 替换。

可以证明这个方法是正确的,因为如果最终的候选叶子节点 \operatorname{leaf} 不是叶子,则一定有一个 \operatorname{leaf} 的儿子。如果它的儿子在 \operatorname{leaf} 前面出现,则 \operatorname{leaf} 根本不可能变成候选叶子,如果在 \operatorname{leaf} 后面出现,则肯定会把 \operatorname{leaf} 替换掉,与 \operatorname{leaf} 是最终的候选叶子节点矛盾,所以 \operatorname{leaf} 一定是叶子。

如何得知枚举到的点 x 是否比 \operatorname{leaf} 深呢?考虑在这个过程中,只需要找 \operatorname{leaf} 子树内的节点即可,而这个判断只需要查询 ? x leaf,看是否等于 \operatorname{leaf} 即可判断是否要替换掉 \operatorname{leaf}。

求路径

然后再扫一遍,当前节点 y 和 \operatorname{leaf} 的 \operatorname{lca} 显然就是路径上的点。同时,对于不在路径上的点,\operatorname{lca} 可以代表自己挂在哪一个路径上的点的另外一半子树(和路径没有交集)上,由此我们可以开一个 map<int,int> mp;,来记录每一个路径上的节点挂在另一半的子树的大小,并且用于去重(会有多个 y 产生相同的 \operatorname{lca})。

根据子树大小可以很容易对路径进行从下到上的排序。于是就可以得出路径上这些节点的父子关系。

划子树

接下来开一个 map<int,vector<int> > sub;,用于存储每一个路径上节点的另一边的子树。

具体的,我们再扫一遍,对于不在路径上的节点,加入其与 \operatorname{leaf} 的 \operatorname{lca} 的子树。

可以发现它一定不会记录与路径同侧的节点,因为一个不在路径上的节点对于与其与 \operatorname{leaf} 的 \operatorname{lca} 来说,一定是另一边的,否则 \operatorname{lca} 就不是最近的那一个了。

然后就很简单了,枚举路径上的点,并且递归。

Code

#include <bits/stdc++.h>
using namespace std;
int n;
int lca[1030][1030];//缓存。
int fa[1030];//父亲。
int query(int x,int y)
{
    if(lca[x][y])return lca[x][y];//缓存命中。
    int t;
    cout << "? " << x << " " << y << "\n" << flush;
    cin >> t;
    lca[x][y] = t;//加入缓存。
    lca[y][x] = t;
    return t;
}
int solve(vector<int> nodes)//处理一个点集。
{
    if(nodes.empty())return 0;//空的。
    if(nodes.size() == 1)return nodes[0];//只有一个节点。
    if(nodes.size() == 3)
    {
        //3个节点,任意两个节点的 lca 便是根。
        int r = query(nodes[0],nodes[1]);
        for(auto it:nodes)
        {
            if(it != r)fa[it] = r;
        }
        return r;
    }
    int leaf = nodes[0],i = 1;
    for(auto it:nodes)
    {
        if(i == 1)
        {
            i++;
            continue;
        }
        if(query(it,leaf) == leaf)
        {
            for(auto jt:nodes)
            {
                if(jt == it)break;
                lca[jt][it] = lca[it][jt] = lca[leaf][jt];
                //这里不会把非 0 值变成 0,因为 it 只和 leaf 求过 lca,所以给非 0 值赋值的语句是 lca[leaf][it] = lca[it][leaf] = lca[leaf][leaf],而 lca[leaf][leaf] 显然不会是 0。
            }
            //前面这个循环的含义是,对于之前扫过的节点,它们与当前找到的候选叶子的 lca 等于与旧候选叶子 的 lca,因为当前候选叶子是旧候选叶子的子树内的节点。
            leaf = it;//更新候选叶子。
        }
        i++;
    }
    map<int,int> mp;
    vector<int> path;
    for(auto it:nodes)
    {
        int p = query(it,leaf);
        if(!mp[p])path.push_back(p);
        mp[p]++;
    }
    sort(path.begin(),path.end(),[&](int a,int b){return mp[a]<mp[b];});
    /*
    前面的 sort 很有特点,它所使用的比较函数是一个 lambda 表达式。其写法就是 (参数){函数体},和普通函数无异,但是为了让它能访问外部变量,要加一个[&]表示引用。
    */
    for(int i = 0;i < path.size()-1;i++)
    {
        fa[path[i]] = path[i+1];
    }
    map<int,vector<int> > sub;
    for(auto it:nodes)
    {
        if(mp.count(it))continue;//路径上的点,不可能属于要递归的子树。
        int p = query(it,leaf);
        sub[p].push_back(it);
    }
    for(auto it:path)
    {
        int r = solve(sub[it]);
        if(r)fa[r] = it;
    }
    return path.back();
}
int main()
{
    cin >> n;
    n = (1<<n)-1;
    vector<int> nodes;
    for(int i = 1;i <= n;i++)
    {
        fa[i] = -1;
        nodes.push_back(i);
        lca[i][i] = i;//去除? x x式的无效查询。
    }
    int root = solve(nodes);
    fa[root] = -1;
    cout << "! ";
    for(int i = 1;i <= n;i++)
    {
        cout << fa[i] << " ";
    }
    cout << "\n" << flush;
    return 0;
}

查询复杂度

对于一次高度为 h 的 solve(),除了前面的特判,其它的所有查询都围绕着 \operatorname{leaf} 展开,所以一次 solve 的查询次数至多是 2^h-2 次。

那么可以列出式子。

0&(x=1)\\ 1&(x=2)\\ 2^x-2+\sum\limits_{k=1}^{x-1}T(k)&(x\ge 3)\end{cases}

可解得对于 x \ge 3,有 T(x)=2^{x-3}(4x-5)。

可求出 T(10)=4480,所以可以保证不超出提问次数限制。在实际评测中也可以看到很多数据的提问次数确实是完全等于 4480。