c/c++语言开发共享洛谷P2664 树上游戏(点分治)

题意 “题目链接” Sol 神仙题。。Orz yyb 考虑点分治,那么每次我们只需要统计以当前点为$LCA$的点对之间的贡献以及$LCA$到所有点的贡献。 一个很神仙的思路是,对于任意两个点对的路径上的颜色,我们只统计里根最近的那个点的贡献。 有了这个思路我们就可以瞎搞了,具体的细节很繁琐,但是大概 …


题意

题目链接

sol

神仙题。。orz yyb

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

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

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

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

复杂度(o(n log n))

#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; }

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/604470.html

(0)
上一篇 2021年5月12日
下一篇 2021年5月12日

精彩推荐