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("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")
#include<cstdio>
#include<algorithm>
#include<cstring>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
//#define vi vector<int>
#define mod 998244353
#define ld long double
#define C 0.5772156649
//#define ls l,m,rt<<1
//#define rs m+1,r,rt<<1|1
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
#define cd complex<double>
#define ull unsigned long long
#define base 1000000000000000000
#define fio ios::sync_with_stdio(false);cin.tie(0)
using namespace std;
const double eps=1e-6;
const int N=1000000+10,maxn=50000+10,inf=0x3f3f3f3f;
vector<int> v[N];
int deep[N];
void dfs(int u,int f,int dep)
{
deep[u]=dep;
for(int i=0;i<v[u].size();i++)
{
int x=v[u][i];
if(x!=f)dfs(x,u,dep+1);
}
}
int root[N],ma[N*22],ind[N*22];
int ls[N*22],rs[N*22],tot;
inline void pushup(int o)
{
if(ma[ls[o]]>=ma[rs[o]])ma[o]=ma[ls[o]],ind[o]=ind[ls[o]];
else ma[o]=ma[rs[o]],ind[o]=ind[rs[o]];
}
inline int Merge(int x,int y,int l,int r)
{
// printf("%d----------------------------%d\n",l,r);
if(l==r)
{
if(!x||!y)
{
ma[x+y]=ma[x]+ma[y];
// printf("%d %d %d %d %d %d\n",x,ma[x],y,ma[y],l,r);
return x+y;
}
else
{
ma[x]=ma[x]+ma[y];
// printf("%d %d %d %d %d %d\n",x,ma[x],y,ma[y],l,r);
return x;
}
}
if(!x||!y)return x+y;
int m=(l+r)>>1;
ls[x]=Merge(ls[x],ls[y],l,m);
rs[x]=Merge(rs[x],rs[y],m+1,r);
pushup(x);
return x;
}
void update(int &o,int pos,int l,int r)
{
if(!o)o=++tot;
// printf("%d %d %d %d---\n",l,r,pos,o);
if(l==r){ma[o]++;ind[o]=l;return ;}
int m=(l+r)>>1;
if(pos<=m)update(ls[o],pos,l,m);
else update(rs[o],pos,m+1,r);
pushup(o);
}
void debug(int o,int l,int r)
{
// printf("%d+++%d %d %d %d\n",o,ma[o],ind[o],l,r);
if(l==r)return ;
int m=(l+r)>>1;
if(ls[o])debug(ls[o],l,m);
if(rs[o])debug(rs[o],m+1,r);
}
int ans[N],n;
void solve(int u,int f)
{
for(int i=0;i<v[u].size();i++)
{
int x=v[u][i];
if(x!=f)solve(x,u);
}
for(int i=0;i<v[u].size();i++)
{
int x=v[u][i];
if(x!=f)
{
root[u]=Merge(root[u],root[x],1,n);
// puts("****************8");
// debug(root[id[u]],1,n);
// puts("!!!!!");
}
}
// debug(root[id[u]],1,n);
// puts("------------");
ans[u]=ind[root[u]]-deep[u];
}
int main()
{
scanf("%d",&n);
for(int i=1;i<n;i++)
{
int a,b;
scanf("%d%d",&a,&b);
v[a].pb(b),v[b].pb(a);
}
dfs(1,-1,1);
for(int i=1;i<=n;i++)
update(root[i],deep[i],1,n);
solve(1,-1);
for(int i=1;i<=n;i++)printf("%d\n",ans[i]);
return 0;
}
/********************
4
1 2
1 3
1 4
********************/
Educational Codeforces Round 47 (Rated for Div. 2)F. Dominant Indices 线段树合并的更多相关文章
- Educational Codeforces Round 51 (Rated for Div. 2) G. Distinctification(线段树合并 + 并查集)
题意 给出一个长度为 \(n\) 序列 , 每个位置有 \(a_i , b_i\) 两个参数 , \(b_i\) 互不相同 ,你可以进行任意次如下的两种操作 : 若存在 \(j \not = i\) ...
- Educational Codeforces Round 71 (Rated for Div. 2)-F. Remainder Problem-技巧分块
Educational Codeforces Round 71 (Rated for Div. 2)-F. Remainder Problem-技巧分块 [Problem Description] ...
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...
- Educational Codeforces Round 47 (Rated for Div. 2) 题解
题目链接:http://codeforces.com/contest/1009 A. Game Shopping 题目: 题意:有n件物品,你又m个钱包,每件物品的价格为ai,每个钱包里的前为bi.你 ...
- Educational Codeforces Round 47 (Rated for Div. 2) :E. Intercity Travelling
题目链接:http://codeforces.com/contest/1009/problem/E 解题心得: 一个比较简单的组合数学,还需要找一些规律,自己把方向想得差不多了但是硬是找不到规律,还是 ...
- Educational Codeforces Round 47 (Rated for Div. 2) :D. Relatively Prime Graph
题目链接:http://codeforces.com/contest/1009/problem/D 解题心得: 题意就是给你n个点编号1-n,要你建立m条无向边在两个互质的点之间,最后所有点形成一个连 ...
- Educational Codeforces Round 47 (Rated for Div. 2) :C. Annoying Present(等差求和)
题目链接:http://codeforces.com/contest/1009/problem/C 解题心得: 题意就是一个初始全为0长度为n的数列,m此操作,每次给你两个数x.d,你需要在数列中选一 ...
- Educational Codeforces Round 47 (Rated for Div. 2) :B. Minimum Ternary String
题目链接:http://codeforces.com/contest/1009/problem/B 解题心得: 题意就是给你一个只包含012三个字符的字符串,位置并且逻辑相邻的字符可以相互交换位置,就 ...
- Educational Codeforces Round 47 (Rated for Div. 2) :A. Game Shopping
题目链接:http://codeforces.com/contest/1009/problem/A 解题心得: 题意就是给你两个数列c,a,你需要从c中选择一个子串从a头开始匹配,要求子串中的连续的前 ...
随机推荐
- linux服务器---代理认证
代理认证 proxy代理服务被广泛的使用,为了安全起见,可以在服务器上增加一层安全认证机制.这里使用htpasswd建立认证账号和密码 1.创建认证账号和密码 [root@localhost wj]# ...
- 20145316许心远《网络对抗》第一次实验拓展:shellcode注入+return-to-libc
shellcode注入实践 编写shellcode 这一部分在我上一篇日志里面已经详细讲过了,这里直接把验证合适的shellcode拿过来. \x31\xc0\x50\x68\x2f\x2f\x73\ ...
- python3.4学习笔记(十二) python正则表达式的使用,使用pyspider匹配输出带.html结尾的URL
python3.4学习笔记(十二) python正则表达式的使用,使用pyspider匹配输出带.html结尾的URL实战例子:使用pyspider匹配输出带.html结尾的URL:@config(a ...
- 冒泡排序法原理讲解及PHP代码示例
冒泡排序原理 冒泡排序对一个数组里的数字进行排序,把数组里两个相邻的数比较大小,将值小的数放在前面,把大的数往后面放,当然这种排序是升序,即从小到大.举例说明$array = [64, 56, 31, ...
- 什么叫集群、分布式,分布式与集群有什么区别?(康神sf讲座学习笔记)
集群是物理形态,分布式是工作方式. 只要一堆机器放在那里,就是集群.比如Nginx后面的十台服务器,就是一个集群 分布式将任务放在多个物理隔离的节点上进行. 分布式中各个子节点互不通信,统一受管控中心 ...
- 开发代码code中变量替换
除了automake/autoconfig 之外,还有其他的替换方式. 参看vdsm https://github.com/oVirt/vdsm/blob/master/Makefile.am htt ...
- js面向对象编程: js类定义函数时prototype和this区别?
参考文章的链接:http://www.2cto.com/kf/201406/307790.html 测试代码如下: function ListCommon2(afirst) { var first=a ...
- 20145104张家明 《Java程序设计》第三次实验设计
合作伙伴是20145103 下面是我们的git成果 首先下载他托管上去的代码 然后运行下载的代码 之后对下载的代码进行修改 然后推送上去 下载修改后的代码并运行 •软件工程是把系统的.有序的.可量化的 ...
- 20145105 《Java程序设计》实验五总结
实验五 JAVA网络编程 一.实验内容 用老师给的代码,实现服务器和客户端 和结对同学实现服务器与客户端的连接 客户端输入数据并加密,将密文发送给服务器,服务器接收密文并解密 二.实验步骤 用老师给的 ...
- C语言: 两个int变量相除,结果保留两位小数
#include<stdio.h> void main() { ,j=; float h; h=(*/)/; printf("%.2f",h); } 注:%f:不指定宽 ...