P16463 [UOI 2026] Color the Tree 题解
先把根节点单独拿出来考虑。
设根节点颜色为 。无论 还是 ,所有点到根的路径和都会整体加上同一个数,所以不会影响“路径和两两不同”这个条件。它只会给答案多乘一个 。
于是可以把点权转到边上:对于每条边 ,边权就是孩子的颜色,也就是 或 。
问题等价于:
给每条非根边赋值 或 ,要求所有点到根的路径和互不相同。
考虑某个点当前到根的路径和为 。它的儿子只可能得到两种路径和: 或者 。
也就是说,一个点最多只能有 个儿子。 如果某个点有超过 个儿子,那么无论怎么染色,至少两个儿子的路径和会相同,直接不合法。
接下来再看有 个儿子的点。
这些“分叉点”必须全部落在同一条根到叶子的链上。否则如果两个分叉点分别出现在不同子树里,两边各自向下延伸时,路径和的可选范围会不可避免地产生重叠,从而发生冲突。
所以合法的树形大致只能长成这样:
root -> ... -> 分叉点 -> ... -> 分叉点 -> ...
每个分叉点另外挂出一条简单链如果树不是这种形态,答案就是 。
计数方式
重点只需要看每个分叉点。
1. 最后一个分叉点
假设最后一个分叉点往下分出两条链,长度分别为 。
如果两条链长度相同,也就是 ,那么方案数为 。
否则令
那么这一段的方案数为:
2. 不是最后一个分叉点
这时分叉点的一条路会继续通向下一个分叉点,另一条路是一条旁支链。
设:
- 表示当前分叉点到下一个分叉点的距离;
- 表示旁支链的长度。
旁支不能比主链更长,否则它会“伸过”下一个分叉点所在的位置,后面一定会产生路径和冲突。
因此必须满足:
如果:
这一段只有 种合法方式。
如果:
这一段的贡献为:
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;
}