跳到正文

题解

题解:P16463 [UOI 2026] Color the Tree

_SUNMJ_约 10 分钟
原文
文章信息与版本

文章信息

发布
2026年5月17日
收录
2026年10月19日
更新
2026年11月14日
篇幅
约 10 分钟
获赞
3
题目
P16463

版本

  1. v1当前

P16463 [UOI 2026] Color the Tree 题解

先把根节点单独拿出来考虑。

设根节点颜色为 c1c_1。无论 c1=1c_1=1 还是 c1=2c_1=2,所有点到根的路径和都会整体加上同一个数,所以不会影响“路径和两两不同”这个条件。它只会给答案多乘一个 22。

于是可以把点权转到边上:对于每条边 parent→child\text{parent}\to\text{child},边权就是孩子的颜色,也就是 11 或 22。

问题等价于:

给每条非根边赋值 11 或 22,要求所有点到根的路径和互不相同。


考虑某个点当前到根的路径和为 ss。它的儿子只可能得到两种路径和:s+1s+1 或者 s+2s+2。

也就是说,一个点最多只能有 22 个儿子。 如果某个点有超过 22 个儿子,那么无论怎么染色,至少两个儿子的路径和会相同,直接不合法。

接下来再看有 22 个儿子的点。

这些“分叉点”必须全部落在同一条根到叶子的链上。否则如果两个分叉点分别出现在不同子树里,两边各自向下延伸时,路径和的可选范围会不可避免地产生重叠,从而发生冲突。

所以合法的树形大致只能长成这样:

代码
root -> ... -> 分叉点 -> ... -> 分叉点 -> ...
每个分叉点另外挂出一条简单链

如果树不是这种形态,答案就是 00。


计数方式

重点只需要看每个分叉点。

1. 最后一个分叉点

假设最后一个分叉点往下分出两条链,长度分别为 a,ba,b。

如果两条链长度相同,也就是 a=ba=b,那么方案数为 22。

否则令 d=∣a−b∣d=|a-b|

那么这一段的方案数为:3⋅2d−13\cdot 2^{d-1}


2. 不是最后一个分叉点

这时分叉点的一条路会继续通向下一个分叉点,另一条路是一条旁支链。

设:

  • gg 表示当前分叉点到下一个分叉点的距离;
  • ll 表示旁支链的长度。

旁支不能比主链更长,否则它会“伸过”下一个分叉点所在的位置,后面一定会产生路径和冲突。

因此必须满足:

l≤gl\le g

如果:

l=gl=g

这一段只有 11 种合法方式。

如果:

l<gl<g

这一段的贡献为:

3⋅2g−l−13\cdot 2^{g-l-1}

C++ 代码

C++
#include <bits/stdc++.h>
using namespace std;

const long long MOD = 1000000007LL;
const int MAXN = 200000 + 5;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    vector<long long> pow2(MAXN);
    pow2[0] = 1;

    for (int i = 1; i < MAXN; i++) {
        pow2[i] = pow2[i - 1] * 2 % MOD;
    }

    int T;
    cin >> T;

    while (T--) {
        int n;
        cin >> n;

        vector<int> p(n + 1), depth(n + 1, 0);
        vector<vector<int>> child(n + 1);

        bool bad = false;
        p[1] = 0;

        for (int i = 2; i <= n; i++) {
            cin >> p[i];
            child[p[i]].push_back(i);
            depth[i] = depth[p[i]] + 1;

            if ((int)child[p[i]].size() > 2) {
                bad = true;
            }
        }

        if (bad) {
            cout << 0 << '\n';
            continue;
        }

        vector<int> branch;

        for (int i = 1; i <= n; i++) {
            if ((int)child[i].size() == 2) {
                branch.push_back(i);
            }
        }

        if (branch.empty()) {
            cout << pow2[n] << '\n';
            continue;
        }

        vector<int> pathLen(n + 1, -1);

        for (int i = n; i >= 1; i--) {
            if (child[i].empty()) {
                pathLen[i] = 0;
            } else if ((int)child[i].size() == 1) {
                int v = child[i][0];

                if (pathLen[v] != -1) {
                    pathLen[i] = pathLen[v] + 1;
                }
            } else {
                pathLen[i] = -1;
            }
        }

        vector<int> tin(n + 1), tout(n + 1), it(n + 1, 0);
        int timer = 0;

        vector<int> st;
        st.push_back(1);

        while (!st.empty()) {
            int u = st.back();

            if (it[u] == 0) {
                tin[u] = ++timer;
            }

            if (it[u] < (int)child[u].size()) {
                int v = child[u][it[u]++];
                st.push_back(v);
            } else {
                tout[u] = timer;
                st.pop_back();
            }
        }

        auto isAncestor = [&](int u, int v) -> bool {
            return tin[u] <= tin[v] && tout[v] <= tout[u];
        };

        sort(branch.begin(), branch.end(), [&](int a, int b) {
            if (depth[a] != depth[b]) {
                return depth[a] < depth[b];
            }
            return a < b;
        });

        long long ans = 2;

        ans = ans * pow2[depth[branch[0]]] % MOD;

        for (int i = 0; i + 1 < (int)branch.size(); i++) {
            int cur = branch[i];
            int nxt = branch[i + 1];

            if (!isAncestor(cur, nxt)) {
                bad = true;
                break;
            }

            int spineChild = -1;
            int sideChild = -1;

            for (int v : child[cur]) {
                if (isAncestor(v, nxt)) {
                    spineChild = v;
                } else {
                    sideChild = v;
                }
            }

            if (spineChild == -1 || sideChild == -1) {
                bad = true;
                break;
            }

            if (pathLen[sideChild] == -1) {
                bad = true;
                break;
            }

            int g = depth[nxt] - depth[cur];
            int l = 1 + pathLen[sideChild];

            if (l > g) {
                bad = true;
                break;
            }

            if (l < g) {
                ans = ans * 3 % MOD;
                ans = ans * pow2[g - l - 1] % MOD;
            }

        }

        if (bad) {
            cout << 0 << '\n';
            continue;
        }

        int last = branch.back();

        int x = child[last][0];
        int y = child[last][1];

        if (pathLen[x] == -1 || pathLen[y] == -1) {
            cout << 0 << '\n';
            continue;
        }

        int a = 1 + pathLen[x];
        int b = 1 + pathLen[y];

        int d = abs(a - b);

        if (d == 0) {
            ans = ans * 2 % MOD;
        } else {
            ans = ans * 3 % MOD;
            ans = ans * pow2[d - 1] % MOD;
        }

        cout << ans % MOD << '\n';
    }

    return 0;
}
原文