当前位置: 移动技术网 > IT编程>开发语言>C/C++ > 洛谷P2664 树上游戏(点分治)

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

2019年04月02日  | 移动技术网IT编程  | 我要评论

题意

题目链接

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

  • C语言字符数组应用示例1:编写一个程序,将两个字符串连接起来,不用strcat函数。

    C语言字符数组应用示例1:编写一个程序,将两个字符串连接起来,不用strcat函数。

    字符串的连接如图所示: 如果字符串 1 中有 n 个元素,那么就是把字符串 2 中的第 i 个元素赋值给字符串 1 中的第 i + n 个元素。 n 可... [阅读全文]
  • 前缀和

    [toc] "前缀和" 一维前缀和 Skips 快速计算一个区间内数的和 [l,r] 定义一个数组 ,下标要从1 开始 ,边界值 定义 s[0]=0 (... [阅读全文]
  • C 实战练习题目7

    题目:输出特殊图案,请在c环境中运行,看一看,Very Beautiful! 程序分析:字符共有256个。不同字符,图形不一样。 VC6.0下出现中文乱... [阅读全文]
  • C 实战练习题目8

    题目:输出9*9口诀。 程序分析:分行与列考虑,共 9 行 9 列,i 控制行,j 控制列。 程序源代码: 1 #include<stdio.h&... [阅读全文]
  • 单链表实现贪吃蛇

    终于把学的单链表塞进贪吃蛇里的. 相比于上一篇的数组,链表的理解程度可能高一些. 上一篇的链接 上代码: #include <stdio.h>... [阅读全文]
  • 博弈--尼姆博弈

    今天我们来聊一聊另一种博弈--尼姆博弈,这一种博弈可以说是巴什博弈的一种变体,巴什博弈中“石子”的堆数为1堆,而在利姆博弈中“石子”的堆数为n堆,还有在... [阅读全文]
  • warshall 判断某无向图是否是一个树

    判断一个图是否构成树 问题 给定一个无向图,判断该图是否构成树。 输入 输入有若干测试样例。第一行是测试样例个数,接下来若干测试样例。 每个测试样例的第... [阅读全文]
  • 二叉排序树

    二叉排序树 二叉排序树是为了实现数据的有序排列,并可方便的对树中的数据进行插入和删除操作,提高查找效率。 性质: 若它的左子树不为空,则左子树上的所有值... [阅读全文]
  • 图,有向图,无向图,图在存储结构

    图 图在数据结构中是多对多的关系,一个顶点可以和多个顶点有联系。其通常表示为: ,其中 表示一个图, 表示图的顶点集合, 表示图的边集合。 1.图的定义... [阅读全文]
  • 二叉树

    二叉树 每个结点最多有两个孩子,其余结构和树的结构一样。 1. 二叉树特点 二叉树的特点有: 每个结点最多有两棵子树,所以二叉树中不存在度大于2的结点。... [阅读全文]
验证码:
移动技术网