当前位置:网站首页>Heuristic merging on tree
Heuristic merging on tree
2022-07-28 09:28: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;}// Its lowest 1 And behind it 0 The value of composition
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)// Heavy chain subdivision
{
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)// The contribution of the light side of violence statistics ,op==0 It means that the influence on this point is eliminated after recursion
{
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;
}
边栏推荐
- 数据泄漏、删除事件频发,企业应如何构建安全防线?
- 01-TensorFlow计算模型(一)——计算图
- Map of China province > City > level > District > Town > village 5-level linkage download [2019 and 2021]
- [gossip] the development of programmers needs two abilities most
- 【一花一世界-郑一教授-繁简之道】可解释神经网络
- [one flower, one world - Professor Zheng Yi - the way of simplicity] interpretable neural network
- 股指期货开户的条件和流程
- [multithreading] non atomic agreement of long and double
- Hexadecimal representation of negative numbers
- Activiti startup error: cannot create poolableconnectionfactory (could not create connection to database server
猜你喜欢

Title and answer of work permit for safety management personnel of hazardous chemical business units in 2022

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

【SwinTransformer源码阅读二】Window Attention和Shifted Window Attention部分
![[C language] detailed explanation sequence table (seqlist)](/img/60/c8cee6a6afe57247aba583291cc99b.png)
[C language] detailed explanation sequence table (seqlist)

ShardingSphere简介(一)

Activiti启报错: Cannot create PoolableConnectionFactory (Could not create connection to database server

2022 high voltage electrician examination simulated 100 questions and simulated examination

树上启发式合并

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

《PyTorch深度学习实践》第九课多分类问题(手写数字MNIST)
随机推荐
Final keyword and enumeration type
Activiti startup error: cannot create poolableconnectionfactory (could not create connection to database server
Problems encountered in upgrading golang to version 1.18.4
What is it like to use gbase C API to execute stored procedures?
v-bind指令的详细介绍
咸鱼ESP32实例—MQTT 点亮LED
The cooperation between starfish OS and metabell is just the beginning
【解决】ERROR in [eslint] ESLint is not a constructor
Title and answer of work permit for safety management personnel of hazardous chemical business units in 2022
F - Jealous Two-二维逆序对
C signed and unsigned byte variables
VR panoramic shooting helps promote the diversity of B & B
Introduction to official account
2022 safety officer-b certificate examination simulated 100 questions and answers
[gossip] the development of programmers needs two abilities most
376. 摆动序列【贪心、动态规划------】
ES6 let and Const
Promise学习笔记
Train your own classification [Bao Jiaobao, the data are ready]
How to use gbase C API in multithreaded environment?