当前位置:网站首页>树上启发式合并
树上启发式合并
2022-07-28 08:51:00 【leimingzeOuO】
3189. Lomsat gelral


#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1);
const double eps=1e-7;
#define x first
#define y second
#define int long long
#define lb long double
#define pb push_back
#define endl '\n'
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define rep(i,x,n) for(int i=x;i<=n;i++)
#define dwn(i,n,x) for(int i=n;i>=x;i--)
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
int Mod(int a,int mod){
return (a%mod+mod)%mod;}
int lowbit(int x){
return x&-x;}//最低位1及其后面的0构成的数值
int qmi(int a, int k, int p){
int res = 1 % p;while (k){
if (k & 1) res = Mod(res * a , p);a = Mod(a * a , p);k >>= 1;}return res;}
int inv(int a,int mod){
return qmi(a,mod-2,mod);}
const int N=100010,M=N*2;
int n;
int h[N],e[M],ne[M],idx;
int color[N],cnt[N],sz[N],son[N];
int ans[N],sum;
int maxv;
void add(int a,int b)
{
e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
int dfs_son(int u,int father)//轻重链剖分
{
sz[u]=1;
for(int i=h[u];~i;i=ne[i])
{
int j=e[i];
if(j==father)continue;
sz[u]+=dfs_son(j,u);
if(sz[j]>sz[son[u]])son[u]=j;
}
return sz[u];
}
void update(int u,int father,int sign,int pson)
{
int c=color[u];
cnt[c]+=sign;
if(cnt[c]>maxv)maxv=cnt[c],sum=c;
else if(cnt[c]==maxv)sum+=c;
for(int i=h[u];~i;i=ne[i])
{
int j=e[i];
if(j==father||j==pson)continue;
update(j,u,sign,pson);
}
}
void dfs(int u,int father,int op)//暴力统计轻边的贡献,op==0表示递归完成后消除对该点的影响
{
for(int i=h[u];~i;i=ne[i])
{
int j=e[i];
if(j==father||j==son[u])continue;
dfs(j,u,0);
}
if(son[u])dfs(son[u],u,1);
update(u,father,1,son[u]);
ans[u]=sum;
if(!op)update(u,father,-1,0),sum=maxv=0;
}
void solve()
{
cin>>n;
rep(i,1,n)cin>>color[i];
memset(h,-1,sizeof h);
rep(i,0,n-2)
{
int a,b;
cin>>a>>b;
add(a,b),add(b,a);
}
dfs_son(1,-1);
dfs(1,-1,1);
rep(i,1,n)cout<<ans[i]<<' ';
}
signed main()
{
io;
int _;_=1;
// cin>>_;
while(_--)solve();
return 0;
}
边栏推荐
- Promise learning notes
- 2022高压电工考试模拟100题及模拟考试
- Hou Jie STL standard library and generic programming
- Bluetooth technology | the total scale of charging piles in Beijing will reach 700000 in 2025. Talk about the indissoluble relationship between Bluetooth and charging piles
- 19c SYSAUX表空间SQLOBJ$PLAN表过大,如何清理
- Recommend an artifact to get rid of the entanglement of variable names and a method to modify file names in batches
- OpenShift 4 之AMQ Streams(1) - 多个Consumer从Partition接收数据
- Mysql5.7.38 start keepalived in the container
- 面经-手撕代码
- 使用 GBase C API 执行存储过程是怎样的?
猜你喜欢

5 运算符、表达式和语句
![[592. Fraction addition and subtraction]](/img/3a/1a76a8acd60a6d45eebed612fd3971.png)
[592. Fraction addition and subtraction]

2022年安全员-B证考试模拟100题及答案

51 single chip microcomputer storage: EEPROM (I2C)

Starfish Os打造的元宇宙生态,跟MetaBell的合作只是开始

Problems encountered in upgrading golang to version 1.18.4

Personal blog applet

AMQ streams (1) of openshift 4 - multiple consumers receive data from partition

2022 examination question bank and simulation examination of crane driver (limited to bridge crane)

Oracle-11gr2 default system job
随机推荐
What is the difference between these two sets of code?
19c SYSAUX表空间SQLOBJ$PLAN表过大,如何清理
正则表达式为十六进制数字?
【杂谈】程序员的发展最需要两点能力
力扣题(1)—— 两数之和
快速上手Flask(一) 认识框架Flask、项目结构、开发环境
A perfect cross compilation environment records the shell scripts generated by PETA
12 common design ideas of design for failure
[592. Fraction addition and subtraction]
19c sysaux tablespace sqlobj$plan table is too large. How to clean it up
技术分享| 快对讲综合调度系统
mysql 最大建议行数2000w,靠谱吗?
【多线程】println方法底层原理
Promise实例如何解决地狱回调
golang 协程的实现原理
ES6 let and Const
正负数值的正则表达式
【592. 分数加减运算】
【leetcode周赛总结】LeetCode第 83场双周赛(7.23)
IP protocol of network layer