启发式合并CodeForces - 1009F
E - Dominant Indices CodeForces - 1009F
You are given a rooted undirected tree consisting of nn vertices. Vertex 11 is the root.
Let's denote a depth array of vertex xx as an infinite sequence [dx,0,dx,1,dx,2,…][dx,0,dx,1,dx,2,…], where dx,idx,i is the number of vertices yy such that both conditions hold:
- xx is an ancestor of yy;
- the simple path from xx to yy traverses exactly ii edges.
The dominant index of a depth array of vertex xx (or, shortly, the dominant index of vertex xx) is an index jj such that:
- for every k<jk<j, dx,k<dx,jdx,k<dx,j;
- for every k>jk>j, dx,k≤dx,jdx,k≤dx,j.
For every vertex in the tree calculate its dominant index.
Input
The first line contains one integer nn (1≤n≤1061≤n≤106) — the number of vertices in a tree.
Then n−1n−1 lines follow, each containing two integers xx and yy (1≤x,y≤n1≤x,y≤n, x≠yx≠y). This line denotes an edge of the tree.
It is guaranteed that these edges form a tree.
Output
Output nn numbers. ii-th number should be equal to the dominant index of vertex ii.
Examples
4
1 2
2 3
3 4
0
0
0
0
4
1 2
1 3
1 4
1
0
0
0
4
1 2
2 3
2 4
2
1
0
0 题意:对于每一个节点x,可以定义一个深度数组[dx0,dx1,dx2,…dxh],代表着以节点x为根往下计算,深度为h的那层的节点的数量。
对于每一个节点x,我们可以从深度数组中,选择一个主要索引下标j,作为他的代表。这个dj需要满足以下条件,他是所有dh中,最大的那个,如果有多个dh是一样的,都是最大的,那么选择j(即深度)最小的那个。
每层节点数的众数。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long LL;
const int maxn=1e6+;
int n,m;
int mx,big,id;
int deep[maxn],si[maxn],hson[maxn],cnt[maxn],ans[maxn];
vector<int>G[maxn]; void findhson(int x,int fa,int dep)//找到所有的重儿子
{
si[x]=;
deep[x]=dep;
int len=G[x].size();
for(int i=;i<len;i++)
{
int t=G[x][i];
if(t!=fa)
{
findhson(t,x,dep+);
si[x]+=si[t];
deep[t]=deep[x]+;
if(si[t]>si[hson[x]])
hson[x]=t;
}
}
}
void cal(int x,int fa,int val)
{
cnt[deep[x]]+=val;
if(cnt[deep[x]]>mx)
{
id=deep[x];
mx=cnt[deep[x]];
}
else if(cnt[deep[x]]==mx && deep[x]<id)
id=deep[x];
int len=G[x].size();
for(int i=;i<len;i++)
{
int t=G[x][i];
if(t!=fa && t!=big)
cal(t,x,val);
}
}
void dfs(int x,int fa,int flag)
{
int len=G[x].size();
for(int i=;i<len;i++)
{
int t=G[x][i];
if(t!=fa && t!=hson[x])
dfs(t,x,);
}
if(hson[x])
{
dfs(hson[x],x,);
big=hson[x];
}
cal(x,fa,);
big=;
ans[x]=id;
if(!flag)
{
cal(x,fa,-);
mx=;id=;
}
}
int main()
{
big=;mx=;id=;
scanf("%d",&n);
int x,y;
for(int i=;i<n;i++)
{
scanf("%d %d",&x,&y);
G[x].push_back(y);
G[y].push_back(x);
}
findhson(,,);
dfs(,,);
for(int i=;i<=n;i++)
printf("%d\n",ans[i]-deep[i]);
return ;
}
启发式合并CodeForces - 1009F的更多相关文章
- 启发式合并 CodeForces - 600E
启发式合并最重要的思想就是指的是每次将小集合拷贝合并至大集合.考虑每个元素的合并开销.对于合并次数最多的那个元素来说,它每合并一次,所在集合的规模扩大两倍,最多只会合并 logN 次,因而对于所有元素 ...
- Educational Codeforces Round 2 E. Lomsat gelral 启发式合并map
E. Lomsat gelral Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/600/prob ...
- CodeForces 958F3 Lightsabers (hard) 启发式合并/分治 多项式 FFT
原文链接http://www.cnblogs.com/zhouzhendong/p/8835443.html 题目传送门 - CodeForces 958F3 题意 有$n$个球,球有$m$种颜色,分 ...
- codeforces 741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths(启发式合并)
codeforces 741D Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths 题意 给出一棵树,每条边上有一个字符,字符集大小只 ...
- codeforces#1166F. Vicky's Delivery (Service并查集+启发式合并)
题目链接: https://codeforces.com/contest/1166/problem/F 题意: 给出节点数为$n$,边数为$m$的图,保证每个点对都是互连的 定义彩虹路:这条路经过$k ...
- Codeforces 1455G - Forbidden Value(map 启发式合并+DP)
Codeforces 题面传送门 & 洛谷题面传送门 首先这个 if 与 end 配对的结构显然形成一个树形结构,考虑把这棵树建出来,于是这个程序的结构就变为,对树进行一遍 DFS,到达某个节 ...
- codeforces 375D . Tree and Queries 启发式合并 || dfs序+莫队
题目链接 一个n个节点的树, 每一个节点有一个颜色, 1是根节点. m个询问, 每个询问给出u, k. 输出u的子树中出现次数大于等于k的颜色的数量. 启发式合并, 先将输入读进来, 然后dfs完一个 ...
- Codeforces 965E Short Code 启发式合并 (看题解)
Short Code 我的想法是建出字典树, 然后让后面节点最多的点优先向上移到不能移为止, 然后gg. 正确做法是对于当前的节点如果没有被占, 那么从它的子树中选出一个深度最大的点换到当前位置. 用 ...
- Codeforces - 600E 树上启发式合并
题意:求每一个子树存在最多颜色的颜色代号和(可重复) 本题是离线统计操作,因此可以直接合并重儿子已达到\(O(nlogn)\)的复杂度 PS.不知道什么是启发式合并的可以这样感受一下:进行树链剖分,分 ...
随机推荐
- TS数据流PAT和PMT分析(转载)
转自:http://www.cnblogs.com/hjj801006/p/3837435.html TS流,是基于packet的位流格式,每个packet是188个字节或者204个字 节(一般是18 ...
- 水题 Codeforces Beta Round #70 (Div. 2) A. Haiku
题目传送门 /* 水题:三个字符串判断每个是否有相应的元音字母,YES/NO 下午网速巨慢:( */ #include <cstdio> #include <cstring> ...
- 416 Partition Equal Subset Sum 分割相同子集和
详见:https://leetcode.com/problems/partition-equal-subset-sum/description/ C++: class Solution { publi ...
- 转】SparkSQL中的内置函数
原博文来自于: http://blog.csdn.net/u012297062/article/details/52207934 感谢! 使用Spark SQL中的内置函数对数据进行分析,Spa ...
- 45 个非常有用的 Oracle 日期查询语句
日期/时间 相关查询 获取当前月份的第一天 运行这个命令能快速返回当前月份的第一天.你可以用任何的日期值替换 “SYSDATE”来指定查询的日期. SELECT TRUNC (SYSDATE, 'MO ...
- Spring框架学习-搭建第一个Spring项目
步骤一:下载Spring开发包. 官网:https://spring.io/ 下载地址:https://repo.spring.io/libs-release-local/org/ ...
- Android开发中查看未root真机的app数据库
在Android开发中,如果用到数据库来储存数据,那么难免就要查看数据库中的内容,可是对于未root的真机来说,查看数据库就不是那么容易了,如果仅仅为了查看数据库再把手机root了,有点得不偿失,所以 ...
- LoadRunner 11中Record无法自动生成脚本——解决办法
[问题描述] 安装loadRunner 11, 使用IE为默认浏览器,打开一个页面进行脚本录制:录制完成后,无法生成脚本. [问题现象] 控制台输出如下: ****** Start Log Messa ...
- 重装macOS环境配置笔记
由于早些年买mac的时候写代码的经验还不够,导致多年使用后mac上装满了乱七八糟的软件,比如python就有系统自带的,xcode里的,pyenv的,以及brew安装的各种版本,nginx,Apach ...
- (转)淘淘商城系列——SSM框架整合之Service层整合
http://blog.csdn.net/yerenyuan_pku/article/details/72721108 上文我们一起学习了Dao层的整合,本文将教大家如何整合Service层. 我们需 ...