当前位置:网站首页>Heuristic merging simple problem on tree
Heuristic merging simple problem on tree
2022-07-28 15:22:00 【Soup key】
Lomsat gelral( Process in notes )
Topic translation
- There is a tree n n n The number of nodes is 1 1 1 Node number is the root of Rooting tree .
- Each node has a color , Colors are represented by numbers , i i i The color number of node No. is c i c_i ci.
- If a color is in x x x The subtree with root appears the most times , Call it in x x x In a subtree with roots Dominance . obviously , Multiple colors may dominate in the same subtree .
- Your task is for every i ∈ [ 1 , n ] i\in[1,n] i∈[1,n], Find out to i i i In the subtree of the root , The number and number of the dominant colors .
- n ≤ 1 0 5 , c i ≤ n n\le 10^5,c_i\le n n≤105,ci≤n
Title Description
You are given a rooted tree with root in vertex 1 1 1 .
Each vertex is coloured in some colour.
Let’s call colour c c c dominating in the subtree of vertex v v v if there are no other colours that appear in the subtree of vertex v v v more times than colour c c c .
So it’s possible that two or more colours will be dominating in the subtree of some vertex.
The subtree of vertex v v v is the vertex v v v and all other vertices that contains vertex v v v in each path to the root.
For each vertex v v v find the sum of all dominating colours in the subtree of vertex v v v .
Input format
The first line contains integer n n n ( 1 < = n < = 1 0 5 1<=n<=10^{5} 1<=n<=105 ) — the number of vertices in the tree.
The second line contains n n n integers c i c_{i} ci ( 1 < = c i < = n 1<=c_{i}<=n 1<=ci<=n ), c i c_{i} ci — the colour of the i i i -th vertex.
Each of the next n − 1 n-1 n−1 lines contains two integers x j , y j x_{j},y_{j} xj,yj ( 1 < = x j , y j < = n 1<=x_{j},y_{j}<=n 1<=xj,yj<=n ) — the edge of the tree.
The first vertex is the root of the tree.
Output format
Print $ n $ integers — the sums of dominating colours for each vertex.
Examples #1
The sample input #1
4
1 2 3 4
1 2
2 3
2 4
Sample output #1
10 9 3 4
Examples #2
The sample input #2
15
1 2 3 1 2 3 3 1 1 3 2 2 1 2 3
1 2
1 3
1 4
1 14
1 15
2 5
2 6
2 7
3 8
3 9
3 10
4 11
4 12
4 13
Sample output #2
6 5 4 3 2 3 3 1 1 3 2 2 1 2 3
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
typedef long long ll;
int head[N],cnt;
int siz[N],son[N];
int col[N],cntt[N];
ll ans[N],sum;
int flag,kmax;
struct stuu{
int to,nt;
}ed[N*2];
void add(int u,int v){
// Chain forward star edge
ed[cnt]=stuu{
v,head[u]};
head[u]=cnt++;
}
void dfs1(int u,int f){
//u Is the current node ,f Is the parent node of the current node ; initialization 1
siz[u]=1;
int maxsize=-1;// It is a temporary variable to judge whether it is a heavy son
for(int i=head[u];i!=-1;i=ed[i].nt){
// Traverse all the sons , Keep updating and find your son
int v=ed[i].to;
if(v==f) continue;// My father must have skipped it directly
dfs1(v,u);// Depth traversal , The current node becomes the parent node , The found son becomes the current node and continues to traverse
siz[u]+=siz[v];// After traversal , Let the size of the current node plus the size of the son
if(siz[v]>maxsize){
// If the size of the son is larger than the temporary variable
maxsize=siz[v];// Just assign it to a temporary variable
son[u]=v;// Change the heavy son of the current node
}
}
}
void count(int u,int f,int val){
// Count the contributions of a node and all its young sons
cntt[col[u]]+=val;//val Positive or negative can control whether to increase or delete contributions
if(cntt[col[u]]>kmax){
// look for max
kmax=cntt[col[u]];
sum=col[u];// Find out u In the subtree of the root , The number and number of the dominant colors
}
else if(cntt[col[u]]==kmax) sum+=col[u];// If the number of two colors is the same, it should be counted
for(int i=head[u];i!=-1;i=ed[i].nt){
// Exclude the marked heavy son , Statistics of other son subtree information
int v=ed[i].to;
if(v==f||v==flag) continue;
count(v,u,val);
}
}
void dfs2(int u,int f,int keep){
// Traverse all light sons and their subtrees to calculate their answer deletion contribution
for(int i=head[u];i!=-1;i=ed[i].nt){
// Traverse all the light sons
int v=ed[i].to;
if(v==f||v==son[u]) continue;// If it is a parent node or a son, skip
dfs2(v,u,false);
}
// Deal with the heavy son and his subtree to calculate his answer without deleting the contribution
if(son[u]){
dfs2(son[u],u,true);
flag=son[u];// Mark heavy son , It is convenient to skip
}
count(u,f,1);// Violence statistics u And all the contributions of the light son are merged into the heavy son information just calculated
flag=0;
ans[u]=sum;
if(!keep) count(u,f,-1),sum=kmax=0;// Delete the contributions that need to be deleted And clear it for next use 0
}
int main(){
memset(head,-1,sizeof(head));
int n;
cin>>n;
for(int i=1;i<=n;i++) scanf("%d",&col[i]);
int u,v;
for(int i=1;i<n;i++){
scanf("%d%d",&u,&v);
add(u,v),add(v,u);
}
dfs1(1,0),dfs2(1,0,0);
for(int i=1;i<=n;i++) printf("%lld ",ans[i]);
}
边栏推荐
- 3564. Date category
- The first self introduction quotation
- 4518. Minimum ticket price
- Pyinstaller packages py as an EXE file
- 3438. 数制转换
- The automatic prompt of vs code code is missing - a tick to solve it
- Crmeb Standard Version window+phpstudy8 installation tutorial (I)
- 3715. Minimum number of exchanges
- Shader顶点着色器修改顶点高度的一个思路
- 新版数据同步问题
猜你喜欢

汇编学习

Jds-12 time relay

3588. 排列与二进制

PMP practice once a day | don't get lost in the exam -7.28 (including agility + multiple choices)

idea调试burpsuit插件

Crmeb v4.3 deployment process

R introduction example details

Publish raspberry pie web page with cpolar (release of apache2 web page)

Establish binary tree + C language code from preorder and middle order

流畅到让人头皮发麻的单商户商城,你用过吗?
随机推荐
In 2022, the average salary of global programmers was released, and China ranked unexpectedly
3540. Binary search tree
Mysql易错知识点整理(待更新)
Untitled may
3559. Ring counting
JS learning notes 24-28: end
svg 验证码识别体验
JDS-12时间继电器
SRTT-110VDC-4H-C时间继电器
Three pop-up boxes commonly used in JS
What are the main threats to cloud security
crmeb v4.3部署流程
解决pycharm使用powershell出错问题
An idea of modifying vertex height with shader vertex shader
MPLS LDP的原理与配置
SystemVerilog
Understanding of entropy and cross entropy loss function
crmeb标准版附带的客服系统
Mlx90640 infrared thermal imager sensor module development notes (VIII)
4518. 最低票价