一个比较显然的做法:对每棵子树用线段树维护其中的深度,线段树合并即可。

  本来想用这个题学一下dsu on tree,结果还是弃疗了。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
int read()
{
int x=,f=;char c=getchar();
while (c<''||c>'') {if (c=='-') f=-;c=getchar();}
while (c>=''&&c<='') x=(x<<)+(x<<)+(c^),c=getchar();
return x*f;
}
#define N 1000010
int n,p[N],root[N],deep[N],ans[N],t=,cnt=;
struct data{int to,nxt;
}edge[N<<];
struct data2{int l,r,x,s;
}tree[N*];
void addedge(int x,int y){t++;edge[t].to=y,edge[t].nxt=p[x],p[x]=t;}
void up(int k)
{
if (tree[tree[k].l].s>=tree[tree[k].r].s)
tree[k].s=tree[tree[k].l].s,tree[k].x=tree[tree[k].l].x;
else tree[k].s=tree[tree[k].r].s,tree[k].x=tree[tree[k].r].x;
}
int merge(int x,int y,int l,int r)
{
if (!x||!y) return x|y;
if (l==r) tree[x].s+=tree[y].s;
else
{
int mid=l+r>>;
tree[x].l=merge(tree[x].l,tree[y].l,l,mid);
tree[x].r=merge(tree[x].r,tree[y].r,mid+,r);
up(x);
}
return x;
}
void ins(int &k,int x,int l,int r)
{
if (!k) k=++cnt;
if (l==r) {tree[k].s++,tree[k].x=x;return;}
int mid=l+r>>;
if (x<=mid) ins(tree[k].l,x,l,mid);
else ins(tree[k].r,x,mid+,r);
up(k);
}
void dfs(int k,int from)
{
for (int i=p[k];i;i=edge[i].nxt)
if (edge[i].to!=from)
{
deep[edge[i].to]=deep[k]+;
dfs(edge[i].to,k);
root[k]=merge(root[k],root[edge[i].to],,n);
}
ins(root[k],deep[k],,n);
ans[k]=tree[root[k]].x-deep[k];
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("dsu.in","r",stdin);
freopen("dsu.out","w",stdout);
const char LL[]="%I64d\n";
#else
const char LL[]="%lld\n";
#endif
n=read();
for (int i=;i<n;i++)
{
int x=read(),y=read();
addedge(x,y),addedge(y,x);
}
deep[]=;dfs(,);
for (int i=;i<=n;i++) printf("%d\n",ans[i]);
return ;
}

  

Codeforces ECR47F Dominant Indices(线段树合并)的更多相关文章

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

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

  2. codeforces 600E . Lomsat gelral (线段树合并)

    You are given a rooted tree with root in vertex 1. Each vertex is coloured in some colour. Let's cal ...

  3. codeforces 893F - Physical Education Lessons 动态开点线段树合并

    https://codeforces.com/contest/893/problem/F 题意: 给一个有根树, 多次查询,每次查询对于$x$i点的子树中,距离$x$小于等于$k$的所有点中权值最小的 ...

  4. Codeforces 671D Roads in Yusland [树形DP,线段树合并]

    洛谷 Codeforces 这是一个非正解,被正解暴踩,但它还是过了. 思路 首先很容易想到DP. 设\(dp_{x,i}\)表示\(x\)子树全部被覆盖,而且向上恰好延伸到\(dep=i\)的位置, ...

  5. Codeforces 666E Forensic Examination SAM or SA+线段树合并

    E. Forensic Examination http://codeforces.com/problemset/problem/666/E 题目大意:给模式串S以及m个特殊串,q个询问,询问S的子串 ...

  6. codeforces 600E E. Lomsat gelral (线段树合并)

    codeforces 600E E. Lomsat gelral 传送门:https://codeforces.com/contest/600/problem/E 题意: 给你一颗n个节点的树,树上的 ...

  7. 【Codeforces 1037H】Security(SAM & 线段树合并)

    Description 给出一个字符串 \(S\). 给出 \(Q\) 个操作,给出 \(L, R, T\),求字典序最小的 \(S_1\),使得 \(S^\prime\) 为\(S[L..R]\) ...

  8. Codeforces 1276F - Asterisk Substrings(SAM+线段树合并+虚树)

    Codeforces 题面传送门 & 洛谷题面传送门 SAM hot tea %%%%%%% 首先我们显然可以将所有能够得到的字符串分成六类:\(\varnothing,\text{*},s, ...

  9. Codeforces 700E. Cool Slogans 字符串,SAM,线段树合并,动态规划

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF700E.html 题解 首先建个SAM. 一个结论:对于parent树上任意一个点x,以及它所代表的子树内任 ...

随机推荐

  1. postgrepsql 创建函数

    -- 这里的CREATE OR REPLACE FUNCTION 为固定写法:   "public"."function_info_a1" 这个为函数名   C ...

  2. webpackt入门1:webpack介绍&webpack安装&使用webpack打包

    本篇博客不是原创,简书的zhangwang写的,原文太长,我这里只是提取了一部分. 原文地址:入门webpack,看这篇就够了 一.Webpack解决了什么问题 问题1.JavaScript这个脚本化 ...

  3. @media响应式的屏幕适配

    当页面小于960px的时候执行 @media screen and (max-width: 960px){ body{ background: #000; } } 等于960px尺寸的代码 @medi ...

  4. Android自动化测试之Monkeyrunner使用方法及实例

    目前Android SDK里自带的现成的测试工具有monkey 和 monkeyrunner两个.大家别看这俩兄弟名字相像,但其实是完完全全不同的两个工具,应用在不同的测试领域.总的来说,monkey ...

  5. python中和生成器协程相关的yield之最详最强解释,一看就懂(一)

    yield是python中一个非常重要的关键词,所有迭代器都是yield实现的,学习python,如果不把这个yield的意思和用法彻底搞清楚,学习python的生成器,协程和异步io的时候,就会彻底 ...

  6. 【微服务】使用spring cloud搭建微服务框架,整理学习资料

    写在前面 使用spring cloud搭建微服务框架,是我最近最主要的工作之一,一开始我使用bubbo加zookeeper制作了一个基于dubbo的微服务框架,然后被架构师否了,架构师曰:此物过时.随 ...

  7. (第十二周)final预发布视频

    项目名:食物链教学工具 组名:奋斗吧兄弟 组长:黄兴 组员:李俞寰.杜桥.栾骄阳.王东涵 Final阶段视频发布 平台:优酷 链接:http://v.youku.com/v_show/id_XMTg0 ...

  8. 【目标跟踪】相关滤波算法之MOSSE

    简要 2010年David S. Bolme等人在CVPR上发表了<Visual Object Tracking using Adaptive Correlation Filters>一文 ...

  9. java可变参数长度

    一: 在python中 有可变参数*args和万能参数**args参数分别为列表和字典.在java中也有类似的可变参数列表.不过传递进去的是可变参数数组. package com.company; p ...

  10. Balanced Ternary String CodeForces - 1102D (贪心+思维)

    You are given a string ss consisting of exactly nn characters, and each character is either '0', '1' ...