传送门
题意
分析
这道题有两种做法,因为是树上静态统计,所以dsu on tree是肯定可以做的
还有一种做法是线段树合并,有时间补上
代码
dsu on tree
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define dl(x) printf("%lld\n",x);
#define di(x) printf("%d\n",x);
#define _CRT_SECURE_NO_WARNINGS
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef vector<int> VI;
const int INF = 0x3f3f3f3f;
const int N = 2e5 + 10, M = N * 2;
const ll mod = 1000000007;
const double eps = 1e-9;
const double PI = acos(-1);
template<typename T>inline void read(T &a) {
char c = getchar(); T x = 0, f = 1; while (!isdigit(c)) {if (c == '-')f = -1; c = getchar();}
while (isdigit(c)) {x = (x << 1) + (x << 3) + c - '0'; c = getchar();} a = f * x;
}
int gcd(int a, int b) {return (b > 0) ? gcd(b, a % b) : a;}
int h[N], e[M], ne[M], idx;
int sz[N], son[N], cnt[N];
int n, Son, mx;
ll sum;
int a[N];
ll ans[N];
void add(int x, int y) {
ne[idx] = h[x], e[idx] = y, h[x] = idx++;
}
void dfs(int u, int fa) {
sz[u] = 1;
for (int i = h[u]; ~i; i = ne[i]) {
int j = e[i];
if (j == fa) continue;
dfs(j, u);
sz[u] += sz[j];
if (sz[son[u]] < sz[j]) son[u] = j;
}
}
void cal(int u, int fa, int st) {
if (st == 1) {
cnt[a[u]]++;
if(cnt[a[u]] > mx) mx = cnt[a[u]],sum = a[u];
else if(cnt[a[u]] == mx) sum += a[u];
for (int i = h[u]; ~i; i = ne[i]) {
if (e[i] != Son && e[i] != fa)
cal(e[i], u, st);
}
}
else {
cnt[a[u]]--;
for (int i = h[u]; ~i; i = ne[i]) {
if (e[i] != Son && e[i] != fa)
cal(e[i], u, st);
}
}
}
void dsu(int u, int fa, bool st) {
for (int i = h[u]; ~i; i = ne[i]) {
int j = e[i];
if (j == fa || j == son[u]) continue;
dsu(j, u, 0);
}
if (son[u]) dsu(son[u], u, 1);
Son = son[u];
cal(u, fa, 1);
Son = 0;
ans[u] = sum;
if (!st) cal(u, fa, -1),mx = sum = 0;
}
int main() {
memset(h, -1, sizeof h);
read(n);
for (int i = 1; i <= n; i++) read(a[i]);
for (int i = 1; i < n; i++) {
int a, b;
read(a), read(b);
add(a, b), add(b, a);
}
dfs(1, 0);
dsu(1, 0, 0);
for (int i = 1; i <= n; i++) printf("%lld ", ans[i]);
return 0;
}
线段树合并
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define dl(x) printf("%lld\n",x);
#define di(x) printf("%d\n",x);
#define _CRT_SECURE_NO_WARNINGS
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef vector<int> VI;
const int INF = 0x3f3f3f3f;
const int N = 1e5 + 10,M = N * 2;
const ll mod = 1000000007;
const double eps = 1e-9;
const double PI = acos(-1);
template<typename T>inline void read(T &a) {
char c = getchar(); T x = 0, f = 1; while (!isdigit(c)) {if (c == '-')f = -1; c = getchar();}
while (isdigit(c)) {x = (x << 1) + (x << 3) + c - '0'; c = getchar();} a = f * x;
}
int gcd(int a, int b) {return (b > 0) ? gcd(b, a % b) : a;}
struct Node{
int l,r;
ll sum;
int mx;
}tr[N * 40];
int h[N],e[M],ne[M],idx,num;
int a[N],n,root[N];
ll ans[N];
void add(int x,int y){
ne[idx] = h[x],e[idx] = y,h[x] = idx++;
}
void push(int u){
tr[u].mx = max(tr[tr[u].l].mx,tr[tr[u].r].mx);
tr[u].sum = 0;
if(tr[tr[u].l].mx == tr[u].mx) tr[u].sum += tr[tr[u].l].sum;
if(tr[tr[u].r].mx == tr[u].mx) tr[u].sum += tr[tr[u].r].sum;
}
void modify(int &u,int l,int r,int x){
if(!u) u = ++num;
if(l == r) {
tr[u].sum = l,tr[u].mx = 1;
return;
}
int mid = (l + r) >> 1;
if(x <= mid) modify(tr[u].l,l,mid,x);
else modify(tr[u].r,mid + 1,r,x);
push(u);
}
int merge(int u,int v,int l,int r){
if(!u || !v) return u + v;
if(l == r){
tr[u].mx += tr[v].mx;
tr[u].sum = l;
return u;
}
int mid = (l + r) >> 1;
tr[u].l = merge(tr[u].l,tr[v].l,l,mid);
tr[u].r = merge(tr[u].r,tr[v].r,mid + 1,r);
push(u);
return u;
}
void dfs(int u,int fa){
modify(root[u],1,n,a[u]);
for(int i = h[u];~i;i = ne[i]){
int j = e[i];
if(j == fa) continue;
dfs(j,u);
root[u] = merge(root[u],root[j],1,n);
}
ans[u] = tr[root[u]].sum;
}
int main() {
memset(h,-1,sizeof h);
read(n);
for(int i = 1;i <= n;i++) read(a[i]);
for(int i = 1;i < n;i++){
int a,b;
read(a),read(b);
add(a,b),add(b,a);
}
dfs(1,0);
for(int i = 1;i <= n;i++) printf("%lld ",ans[i]);
return 0;
}