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

  本来想用这个题学一下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. ubuntu 下使用 jsoncpp库

    做项目的时候需要用c++解析json文件, 之前使用的是libjson 库, 但当g++ 开启 -std=c++11 选项时, 该库的很多功能不能用, 而且还有一些其他的问题, 不推荐使用. 后来采用 ...

  2. co模块源码学习笔记

    // Sorrow.X --- 添加注释,注释纯属个人理解 /** * slice变量 引用 数组的 slice方法 */ var slice = Array.prototype.slice; /** ...

  3. vba报表制作

    Option Explicit Dim sql, tj As String, rnum As Double, r As Integer  Private Sub CommandButton1_Clic ...

  4. vba基础知识

    VBA中dim,static和public,private的区别 转载的文章:出自 红袖的博客http://blog.sina.com.cn/19850701xx     1.Public和Priva ...

  5. Linux下安装jdk+maven +git

            Linux系统下的操作,一直不是很熟悉.作为一名java开发工程师,感到很惭愧.因此把自己的阿里云服务器安装环境相关的东西给记录下来,方便后续查阅.         本文所采用的Lin ...

  6. hexo——轻量、简易、高逼格的博客

    背景 写blog虽然经历了N多不同时代的产品,恒久不变的始终是自己无人问津的网站.虽然没几个人看,还是隔断时间就要折腾一下.从最开始的wordpress,到tale,到现在的hexo,网站变得越来越简 ...

  7. 浅谈移动端设备标识码:DeviceID、IMEI、IDFA、UDID和UUID

    ---恢复内容开始--- 转:https://www.jianshu.com/p/38f4d1a4763b [心路历程] 最近刚好在思考工作中统计数据所用的标识码产生的数据误差到底有多大,借此机会几番 ...

  8. 矩形A + B HDU2524

    题意 给你n*m的棋盘问有多少个矩形 分析 先看只有一行或一列的情况有1+2+....+n个,因为矩形的类型有1个最小单位格子n个,2个最小单位格子n-1个,n个最小单位格子有一个 code #inc ...

  9. 12.16 Daily Scrum

      Today's Task Tomorrow's Task 丁辛 实现和菜谱相关的餐厅列表. 实现和菜谱相关的餐厅列表.             邓亚梅             美化搜索框UI. 美 ...

  10. Linux内核期中

    Linux内核期中总结 一.计算机是如何工作的 个人理解:计算机就是通过和用户进行交互,执行用户的指令,这些指令存放在内存中,通过寄存器存储,堆栈变化,来一步步顺序执行. 二.存储程序计算机工作模型 ...