当前位置:网站首页>树上启发式合并
树上启发式合并
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;
}
边栏推荐
- v-bind指令的详细介绍
- 12 common design ideas of design for failure
- 【592. 分数加减运算】
- Title and answer of work permit for safety management personnel of hazardous chemical business units in 2022
- VR全景拍摄,助力民宿多元化宣传
- IDC脚本文件运行
- 51单片机存储篇:EEPROM(I2C)
- RGB-T追踪——【多模态融合】Visible-Thermal UAV Tracking: A Large-Scale Benchmark and New Baseline
- 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
- Oracle creates users with query permission only
猜你喜欢

2022年危险化学品经营单位安全管理人员上岗证题目及答案
![Map of China province > City > level > District > Town > village 5-level linkage download [2019 and 2021]](/img/ea/fd799bbef5110fddf4066e76892f81.png)
Map of China province > City > level > District > Town > village 5-level linkage download [2019 and 2021]

Realize batch data enhancement | use of keras imagedatagenerator

Regular expressions for positive and negative values

Sentinel

C#简单调用FMU ,进行仿真计算

Promise实例如何解决地狱回调

2022 safety officer-b certificate examination simulated 100 questions and answers

51 single chip microcomputer storage: EEPROM (I2C)

opencv4.60版本安装和配置
随机推荐
[solution] error in [eslint] eslint is not a constructor
[C language] detailed explanation sequence table (seqlist)
01-TensorFlow计算模型(一)——计算图
《数据库系统内 幕》分布式系统
Title and answer of work permit for safety management personnel of hazardous chemical business units in 2022
Leetcode 452. minimum number of arrows to burst balloons (medium)
376. 摆动序列【贪心、动态规划------】
2022 safety officer-b certificate examination simulated 100 questions and answers
Hou Jie STL standard library and generic programming
2022年危险化学品经营单位安全管理人员上岗证题目及答案
How to use gbase C API in multithreaded environment?
阿里云服务器搭建和宝塔面板连接
剑指offer
【vscode】vscode使用
Code management platform SVN deployment practice
IP protocol of network layer
2022 high voltage electrician examination simulated 100 questions and simulated examination
The cooperation between starfish OS and metabell is just the beginning
Conference OA system
[English postgraduate entrance examination vocabulary training camp] day 15 - analyze, general, avoid, surveillance, compared