F - Dominant Indices

思路:树上启发式合并

先跑轻子树,然后清除轻子树的信息

最后跑重子树,不清除信息

然后再跑一遍轻子树,重新加回轻子树的信息

由于一个节点到根节点最多有logn个轻边,所以复杂度为nlogn

代码:

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
//#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pii pair<int, int>
#define piii pair<pii, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
//head const int N = 1e6 + ;
vector<int>g[N];
map<int, int>mp;
int deep[N], sz[N], bs[N], ans[N], mx = ;
void dfs(int o, int u) {
deep[u] = deep[o] + ;
sz[u] = ;
for (int v : g[u]) {
if(v != o) {
dfs(u, v);
sz[u] += sz[v];
if(sz[v] > sz[bs[u]]) bs[u] = v;
}
}
}
void ADD(int o,int u) {
mp[deep[u]]++;
if(mp[deep[u]] > mp[mx] || mp[deep[u]] == mp[mx] && deep[u] < mx) mx = deep[u];
for (int v : g[u]) {
if(v != o) {
ADD(u, v);
}
}
}
void DFS(int o, int u) {
for (int v : g[u]) {
if(v != o && v != bs[u]) {
DFS(u, v);
mp.clear();
mx = ;
}
}
if(bs[u])DFS(u, bs[u]);
for (int v : g[u]) {
if(v != o && v != bs[u]) {
ADD(u, v);
}
}
mp[deep[u]]++;
if(mp[deep[u]] > mp[mx] || mp[deep[u]] == mp[mx] && deep[u] < mx) mx = deep[u];
ans[u] = mx - deep[u];
}
int main() {
int n, u, v;
scanf("%d", &n);
for (int i = ; i < n; i++) {
scanf("%d %d", &u, &v);
g[u].pb(v);
g[v].pb(u);
}
dfs(, );
DFS(, );
for (int i = ; i <= n; i++) printf("%d%c", ans[i], '\n');
return ;
}

Codeforces 1009 F - Dominant Indices的更多相关文章

  1. Codeforces 1009 F. Dominant Indices(长链剖分/树上启发式合并)

    F. Dominant Indices 题意: 给一颗无向树,根为1.对于每个节点,求其子树中,哪个距离下的节点数量最多.数量相同时,取较小的那个距离. 题目: 这类题一般的做法是树上的启发式合并,复 ...

  2. CF 1009 F Dominant Indices —— 长链剖分+指针

    题目:http://codeforces.com/contest/1009/problem/F 也可以用 dsu on tree 的做法,全局记录一个 dep,然后放进堆里,因为字典序要最小,所以再记 ...

  3. Educational Codeforces Round 47 (Rated for Div. 2)F. Dominant Indices 线段树合并

    题意:有一棵树,对于每个点求子树中离他深度最多的深度是多少, 题解:线段树合并快如闪电,每个节点开一个权值线段树,递归时合并即可,然后维护区间最多的是哪个权值,到x的深度就是到根的深度减去x到根的深度 ...

  4. F. Dominant Indices

    题意:求每个点的子树中哪一层节点数最多,如果有节点数最多不唯一,取层数最小的. 题解:dus on tree 基本想法是对每一个节点都构建一个deep数组,然后从底向上更新过来,但是这样空间复杂度和时 ...

  5. 【CF1009F】Dominant Indices(长链剖分)

    [CF1009F]Dominant Indices(长链剖分) 题面 洛谷 CF 翻译: 给定一棵\(n\)个点,以\(1\)号点为根的有根树. 对于每个点,回答在它子树中, 假设距离它为\(d\)的 ...

  6. CF1009F Dominant Indices 解题报告

    CF1009F Dominant Indices 题意简述 给出一颗以\(1\)为跟的有根树,定义\(d_{i,j}\)为以\(i\)为根节点的子树中到\(i\)的距离恰好为\(j\)的点的个数,对每 ...

  7. Codeforces 959 F. Mahmoud and Ehab and yet another xor task

    \(>Codeforces\space959 F. Mahmoud\ and\ Ehab\ and\ yet\ another\ xor\ task<\) 题目大意 : 给出一个长度为 \ ...

  8. Codeforces 835 F. Roads in the Kingdom

    \(>Codeforces\space835 F. Roads in the Kingdom<\) 题目大意 : 给你一棵 \(n\) 个点构成的树基环树,你需要删掉一条环边,使其变成一颗 ...

  9. Codeforces 731 F. Video Cards(前缀和)

    Codeforces 731 F. Video Cards 题目大意:给一组数,从中选一个数作lead,要求其他所有数减少为其倍数,再求和.问所求和的最大值. 思路:统计每个数字出现的个数,再做前缀和 ...

随机推荐

  1. css文本样式text、字体样式font

    文本样式text 1.文本颜色color 例如h1 {color:red;} 2.文本方向direction,不常用 默认ltr从左到右,rtl表示从右到左 3.文本水平对齐方式text-align ...

  2. mariadb的flashback到底怎么样???防误删可以,但算不上真正的闪回--再看mariadb 10.3的System-Versioned Tables

    mariadb 在10.2.4引入闪回特性,支持DML(INSERT, DELETE, UPDATE)操作的闪回,不支持DDL语句,使用闪回,必须设置binlog_row_image=FULL. 其原 ...

  3. Zookeeper .Net客户端代码

    本来此客户端可以通过NuGet获取,如果会使用NuGet, 则可以使用命令Install-Package ZooKeeperNet(需要最新版本的NuGet) 如果不会,就去 NuGet官网了解htt ...

  4. 2018年11月20日 远交近攻 list1

    list 列表用法 li=[1,2,3,"abc"] #列表中的元素,可以为数字或者字符串或者布尔值或者就是列表等,所有都能放进去 #列表中也能嵌套列表 pi=[1,2,3,[2, ...

  5. 第三章 Web页面建设

    认识<q>元素: 简短的引用. 问:你去掉了双引号,换成了一个<q>元素,只是为了显示双引号?这样不是更复杂了吗? 答:不.在增加<q>元素之前,浏览器只知道这是一 ...

  6. linux内核中的crng是什么?

    答: 一致性随机数生成器(congruential random number generator)

  7. noip模拟【tea】

    tea [题目描述]有n个容量为V的瓶子,第i个瓶子中装着a[i]个单位的tea,使所有瓶子内的tea在不 超过其容量的前提下,非空的瓶子最少.在一个单位时间内,可以同时将多个瓶子中的tea倒入另外多 ...

  8. tp剩余未验证内容-5

    经过实践, ie678是不能正确显示解析bs的,所以要用ff和chrome浏览器. page-header类是有特殊样式的 在标题下有一条浅色的细线条,源代码中有: border-bottom: 1p ...

  9. (转)Nginx学习

    (二期)15.负载均衡nginx [课程15]nginx安装.xmind0.2MB [课程15]Nginx能做什么.xmind0.1MB [课程15]负载均衡nginx.xmind96.7KB [课程 ...

  10. (转)Awesome Object Detection

    Awesome Object Detection 2018-08-10 09:30:40 This blog is copied from: https://github.com/amusi/awes ...