前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >洛谷P2664 树上游戏(点分治)

洛谷P2664 树上游戏(点分治)

作者头像
attack
发布2019-04-09 17:36:55
4790
发布2019-04-09 17:36:55
举报

题意

题目链接

Sol

神仙题。。Orz yyb

考虑点分治,那么每次我们只需要统计以当前点为\(LCA\)的点对之间的贡献以及\(LCA\)到所有点的贡献。

一个很神仙的思路是,对于任意两个点对的路径上的颜色,我们只统计里根最近的那个点的贡献。

有了这个思路我们就可以瞎搞了,具体的细节很繁琐,但是大概思路是事实维护每个点的子树中的点会产生的贡献。比如某个点的颜色在它到根的路径上第一次出现,那么它子树中的所有点\(siz[x]\),都会对外面的点产生贡献。

统计子树的时候只需要先消除掉子树的影响,然后dfs的时候考虑一下新加的颜色的贡献。。

复杂度\(O(n \log n)\)

代码语言:javascript
复制
#include<bits/stdc++.h> 
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
#define LL long long 
#define ull unsigned long long 
#define Fin(x) {freopen(#x".in","r",stdin);}
#define Fout(x) {freopen(#x".out","w",stdout);}
#define pb push_back 
using namespace std;
const int MAXN = 1e6 + 10, mod = 1e9 + 7, INF = 1e9 + 10;
const double eps = 1e-9;
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;}
template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}
template <typename A> inline void debug(A a){cout << a << '\n';}
template <typename A> inline LL sqr(A x){return 1ll * x * x;}
template <typename A, typename B> inline LL fp(A a, B p, int md = mod) {int b = 1;while(p) {if(p & 1) b = mul(b, a);a = mul(a, a); p >>= 1;}return b;}
template <typename A> A inv(A x) {return fp(x, mod - 2);}
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int N, c[MAXN], cnt[MAXN], vis[MAXN], siz[MAXN], Lim, mx[MAXN], root;
LL ans[MAXN], num[MAXN], Sum;
vector<int> v[MAXN];
void FindRoot(int x, int fa) {
    siz[x] = 1; mx[x] = 1;
    for(auto &to : v[x]) {
        if(to == fa || vis[to]) continue;
        FindRoot(to, x);
        siz[x] += siz[to];
        chmax(mx[x], siz[to]);
    }
    chmax(mx[x], Lim - siz[x]);
    if(mx[x] < mx[root]) 
        root = x;
}

void dfs(int x, int fa, int opt) {
    cnt[c[x]]++;
    if(cnt[c[x]] == 1) Sum += siz[x] * opt, num[c[x]] += siz[x] * opt;
    for(auto &to : v[x])
        if(to != fa && !vis[to]) dfs(to, x, opt);
    cnt[c[x]]--;
}
void calc(int x, int fa) {
    cnt[c[x]]++;
    if(cnt[c[x]] == 1) Sum += Lim - num[c[x]];
    ans[x] += Sum;
    for(auto &to : v[x]) {
        if(to == fa || vis[to]) continue;
        calc(to, x);
    }
    cnt[c[x]]--;
    if(cnt[c[x]] == 0) Sum -= Lim - num[c[x]];
}
void Divide(int x) {
    if(vis[x]) return ; vis[x] = 1;
    Sum = 0; FindRoot(x, 0);
    dfs(x, 0, 1); ans[x] += Sum;
    for(auto &to : v[x]) {
        if(vis[to]) continue;
        num[c[x]] -= siz[to]; Sum -= siz[to]; Lim -= siz[to];
        cnt[c[x]] = 1; dfs(to, x, -1); cnt[c[x]] = 0;
        calc(to, x);
        cnt[c[x]] = 1; dfs(to, x, 1); cnt[c[x]] = 0;
        num[c[x]] += siz[to]; Sum += siz[to]; Lim += siz[to];
    }
    dfs(x, 0, -1);
    for(auto &to : v[x]) 
        if(!vis[to]) {
            root = 0, Lim = siz[to], FindRoot(to, x);
            Divide(root);
    }
}
signed main() {
    //freopen("a.in", "r", stdin);freopen("b.out", "w", stdout);
    N = read(); mx[0] = 1e9;
    for(int i = 1; i <= N; i++) c[i] = read();
    for(int i = 1; i < N ; i++) {
        int x = read(), y = read();
        v[x].pb(y); v[y].pb(x);
    }
    Lim = N; root = 0; FindRoot(1, 0);
    Divide(root);
    for(int i = 1; i <= N; i++) cout << ans[i] << '\n';
    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-04-01 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题意
  • Sol
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档