Codeforces ECR47F Dominant Indices(线段树合并)

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

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

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
int read()
{
    int x=0,f=1;char c=getchar();
    while (c<‘0‘||c>‘9‘) {if (c==‘-‘) f=-1;c=getchar();}
    while (c>=‘0‘&&c<=‘9‘) x=(x<<1)+(x<<3)+(c^48),c=getchar();
    return x*f;
}
#define N 1000010
int n,p[N],root[N],deep[N],ans[N],t=0,cnt=0;
struct data{int to,nxt;
}edge[N<<1];
struct data2{int l,r,x,s;
}tree[N*22];
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>>1;
        tree[x].l=merge(tree[x].l,tree[y].l,l,mid);
        tree[x].r=merge(tree[x].r,tree[y].r,mid+1,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>>1;
    if (x<=mid) ins(tree[k].l,x,l,mid);
    else ins(tree[k].r,x,mid+1,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]+1;
        dfs(edge[i].to,k);
        root[k]=merge(root[k],root[edge[i].to],1,n);
    }
    ins(root[k],deep[k],1,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=1;i<n;i++)
    {
        int x=read(),y=read();
        addedge(x,y),addedge(y,x);
    }
    deep[1]=1;dfs(1,1);
    for (int i=1;i<=n;i++) printf("%d\n",ans[i]);
    return 0;
}

  

原文地址:https://www.cnblogs.com/Gloid/p/9502618.html

时间: 2024-07-30 14:11:23

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

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

题意:有一棵树,对于每个点求子树中离他深度最多的深度是多少, 题解:线段树合并快如闪电,每个节点开一个权值线段树,递归时合并即可,然后维护区间最多的是哪个权值,到x的深度就是到根的深度减去x到根的深度复杂度O(nlogn) //#pragma comment(linker, "/stack:200000000") //#pragma GCC optimize("Ofast,no-stack-protector") //#pragma GCC target("

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

You are given a rooted tree with root in vertex 1. Each vertex is coloured in some colour. Let's call colour c dominating in the subtree of vertex v if there are no other colours that appear in the subtree of vertex v more times than colour c. So it'

CodeForces 600E Lomsat gelral(线段树合并)

题目链接:http://codeforces.com/problemset/problem/600/E You are given a rooted tree with root in vertex 1. Each vertex is coloured in some colour. Let's call colour c dominating in the subtree of vertex v if there are no other colours that appear in the

Codeforces Gym 101194G Pandaria (2016 ACM-ICPC EC-Final G题, 并查集 + 线段树合并)

题目链接  2016 ACM-ICPC EC-Final Problem G 题意  给定一个无向图.每个点有一种颜色. 现在给定$q$个询问,每次询问$x$和$w$,求所有能通过边权值不超过w的边走到$x$的点的集合中,哪一种颜色的点出现的次数最多. 次数相同时输出编号最小的那个颜色.强制在线. 求哪种颜色可以用线段树合并搞定. 关键是这个强制在线. 当每次询问的时候,我们先要求出最小生成树在哪个时刻恰好把边权值不超过$w$的边都用并查集合并了. 在做最小生成树的时候每合并两个节点,另外开一个

Codeforces.1051G.Distinctification(线段树合并 并查集)

题目链接 \(Description\) 给定\(n\)个数对\(A_i,B_i\).你可以进行任意次以下两种操作: 选择一个位置\(i\),令\(A_i=A_i+1\),花费\(B_i\).必须存在一个位置\(j\),满足\(A_i=A_j,\ i\neq j\),才可以进行. 选择一个位置\(i\),令\(A_i=A_i-1\),花费\(-B_i\).必须存在一个位置\(j\),满足\(A_i=A_j+1\),才可以进行. 你需要对于所有\(i\in[1,n]\),求使得\(A_1,A_2,

Alyona and a tree CodeForces - 739B (线段树合并)

大意: 给定有根树, 每个点$x$有权值$a_x$, 对于每个点$x$, 求出$x$子树内所有点$y$, 需要满足$dist(x,y)<=a_y$. 刚开始想错了, 直接打线段树合并了.....因为范围是$long \space long$常数极大, 空间很可能会被卡, 不过竟然过了. 实际上本题每个点对树链上的贡献是单调的, 直接二分就行了 放一下线段树合并代码 #include <iostream> #include <algorithm> #include <cs

【BZOJ4399】魔法少女LJJ 线段树合并

[BZOJ4399]魔法少女LJJ Description 在森林中见过会动的树,在沙漠中见过会动的仙人掌过后,魔法少女LJJ已经觉得自己见过世界上的所有稀奇古怪的事情了LJJ感叹道“这里真是个迷人的绿色世界,空气清新.淡雅,到处散发着醉人的奶浆味:小猴在枝头悠来荡去,好不自在:各式各样的鲜花争相开放,各种树枝的枝头挂满沉甸甸的野果:鸟儿的歌声婉转动听,小河里飘着落下的花瓣真是人间仙境”SHY觉得LJJ还是太naive,一天,SHY带着自己心爱的图找到LJJ,对LJJ说:“既然你已经见识过动态树

【BZOJ2733】永无乡[splay启发式合并or线段树合并]

题目大意:给你一些点,修改是在在两个点之间连一条无向边,查询时求某个点能走到的点中重要度第k大的点.题目中给定的是每个节点的排名,所以实际上是求第k小:题目求的是编号,不是重要度的排名.我一开始差点被这坑了. 网址:http://www.lydsy.com/JudgeOnline/problem.php?id=2733 这道题似乎挺经典的(至少我看许多神犇很早就做了这道题).这道题有两种写法:并查集+(splay启发式合并or线段树合并).我写的是线段树合并,因为--splay不会打+懒得学.

BZOJ 4756 线段树合并(线段树)

思路: 1.最裸的线段树合并 2. 我们可以观察到子树求一个东西 那我们直接DFS序好了 入队的时候统计一下有多少比他大的 出的时候统计一下 减一下 搞定~ 线段树合并代码: //By SiriusRen #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int N=100050; int n,col[N],cpy[N],tree[N*100],lso