CF2252F Spectral Components (T809245 颜色连通块)
CF2252F Spectral Components 颜色连通块
time limit per test 3 seconds
memory limit per test 256 megabytes
题目描述
给你一棵包含
对于树中出现的每种颜色
对于每种颜色
所选连通块的代价为所有颜色
对于每个颜色
输入格式
每个测试包含多个测试用例。第一行包含测试用例数
每个测试用例的第一行包含一个整数
第二行包含
第三行包含
接下来
保证所有测试用例的
输出格式
对于每个测试用例,输出
输入输出样例 #1
输入 #1
3
5
1 1 2 1 2
2 1 1 1 1
1 2
2 3
2 4
4 5
6
2 1 1 1 1 1
3 1 1 1 1 1
1 2
1 3
1 4
1 5
1 6
6
1 2 1 2 1 2
2 3 1 1 1 1
1 2
2 3
3 4
4 5
5 6
输出 #1
1 3 -1 -1 -1
3 0 -1 -1 -1 -1
3 2 -1 -1 -1 -1
说明/提示
在第一个测试用例中,树有
在第二个测试用例中,树是以
在第三个测试用例中,树是一条链
解题思路
我们针对单种颜色
首先考虑
其中求和遍历树中的所有边
当
为了最小化
然而,对所有颜色朴素计算会达到
- 对于每种颜色
c ,使用 LCA 在\mathcal{O}(M \log N) 时间内构建其M 个顶点的虚树。 - 在虚树上进行自底向上的 DP,计算每个虚子树中的
m_{\mathrm{in}} 。 - 虚边
(u, v) 表示一条压缩路径,长度为L = \operatorname{depth}[v] - \operatorname{depth}[u] 条实边。由于这条路径上没有其他颜色c 的分支,因此路径上的每条实边都具有相同的权值:w' = \min(m_{\mathrm{in}}, m_c - m_{\mathrm{in}}) 。 - 对于颜色
c ,收集所有虚边对应的二元组(权值 =w' ,数量 =L )。 - 将这些二元组按权值降序排序,贪心地选取最多
K - 1 条边,从所有w[e] 的总和中减去。
由于所有颜色虚树大小之和为
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int LOG = 19;
int n;
vector<int> C, K;
vector<vector<int>> adj;
vector<int> tin, tout, depth;
vector<vector<int>> up;
int timer = 0;
void dfs(int v, int p, int d)
{
tin[v] = ++timer;
depth[v] = d;
up[v][0] = p;
for (int i = 1; i < LOG; i++)
{
up[v][i] = up[up[v][i - 1]][i - 1];
}
for (int u : adj[v])
{
if (u != p)
dfs(u, v, d + 1);
}
tout[v] = ++timer;
}
bool is_ancestor(int u, int v)
{
return tin[u] <= tin[v] && tout[u] >= tout[v];
}
int get_lca(int u, int v)
{
if (is_ancestor(u, v))
return u;
if (is_ancestor(v, u))
return v;
for (int i = LOG - 1; i >= 0; i--)
{
if (!is_ancestor(up[u][i], v))
u = up[u][i];
}
return up[u][0];
}
void solve()
{
cin >> n;
C.assign(n + 1, 0);
K.assign(n + 1, 0);
adj.assign(n + 1, vector<int>());
tin.assign(n + 1, 0);
tout.assign(n + 1, 0);
depth.assign(n + 1, 0);
up.assign(n + 1, vector<int>(LOG, 1));
timer = 0;
vector<vector<int>> colors(n + 1);
for (int i = 1; i <= n; i++)
{
cin >> C[i];
colors[C[i]].push_back(i);
}
for (int i = 1; i <= n; i++)
cin >> K[i];
for (int i = 0; i < n - 1; i++)
{
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1, 1, 0);
vector<long long> ans(n + 1, -1);
vector<int> cnt(n + 1, 0);
for (int c = 1; c <= n; c++)
{
if (colors[c].empty())
continue;
int M_c = colors[c].size();
int K_c = K[c];
vector<int> nodes = colors[c];
sort(nodes.begin(), nodes.end(), [](int a, int b)
{ return tin[a] < tin[b]; });
int sz = nodes.size();
for (int i = 0; i < sz - 1; i++)
{
nodes.push_back(get_lca(nodes[i], nodes[i + 1]));
}
sort(nodes.begin(), nodes.end(), [](int a, int b)
{ return tin[a] < tin[b]; });
nodes.erase(unique(nodes.begin(), nodes.end()), nodes.end());
for (int u : colors[c])
cnt[u] = 1;
vector<int> st;
vector<pair<int, int>> vt_edges;
for (int u : nodes)
{
while (!st.empty() && !is_ancestor(st.back(), u))
st.pop_back();
if (!st.empty())
vt_edges.push_back({st.back(), u});
st.push_back(u);
}
long long total_dist = 0;
vector<pair<long long, long long>> edge_weights; // {weight, count}
for (int i = (int)vt_edges.size() - 1; i >= 0; i--)
{
int p = vt_edges[i].first;
int u = vt_edges[i].second;
cnt[p] += cnt[u];
long long w = min((long long)cnt[u], (long long)M_c - cnt[u]);
long long L = depth[u] - depth[p];
total_dist += w * L;
edge_weights.push_back({w, L});
}
sort(edge_weights.rbegin(), edge_weights.rend());
long long saved = 0;
long long needed = K_c - 1;
for (auto &ew : edge_weights)
{
long long take = min(needed, ew.second);
saved += take * ew.first;
needed -= take;
if (needed == 0)
break;
}
ans[c] = total_dist - saved;
for (int u : nodes)
cnt[u] = 0;
}
for (int i = 1; i <= n; i++)
{
cout << ans[i] << (i == n ? "" : " ");
}
cout << "\n";
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int t;
cin >> t;
while (t--)
solve();
return 0;
}