首页 > 其他分享 >CF600E Lomsat gelral

CF600E Lomsat gelral

时间:2023-11-14 16:45:25浏览次数:32  
标签:fir cnt cur int son gelral CF600E array Lomsat

题意

给定一棵根为 \(1\) 的 有根树

每个节点有颜色,求每个节点子树内出现最多的颜色编号之和。

Sol

Dsu on tree板子题。

首先对于整棵树进行轻重链剖分,注意到一个关键性质:轻边只有 \(log\) 条

\(n ^ 2\) 的暴力是 \(trivial\) 的,不再赘述。

注意到中间有很多节点被重复计算了多次。

考虑如何减少重复计算的次数。

不难想到保留重儿子的贡献,每次重新计算轻儿子的贡献。

结合上述性质,不难发现时间复杂度为 \(O(n log n)\)

Code

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <array>
#include <queue>
#define int long long
using namespace std;
#ifdef ONLINE_JUDGE

#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
char buf[1 << 23], *p1 = buf, *p2 = buf, ubuf[1 << 23], *u = ubuf;

#endif
int read() {
	int p = 0, flg = 1;
	char c = getchar();
	while (c < '0' || c > '9') {
		if (c == '-') flg = -1;
		c = getchar();
	}
	while (c >= '0' && c <= '9') {
		p = p * 10 + c - '0';
		c = getchar();
	}
	return p * flg;
}
void write(int x) {
	if (x < 0) {
		x = -x;
		putchar('-');
	}
	if (x > 9) {
		write(x / 10);
	}
	putchar(x % 10 + '0');
}
const int N = 1e5 + 5, M = 2e5 + 5;

namespace G {

array <int, N> fir;
array <int, M> nex, to;
int cnt;
void add(int x, int y) {
	cnt++;
	nex[cnt] = fir[x];
	to[cnt] = y;
	fir[x] = cnt;
}

}

array <int, N> s;
array <int, N> ans;

namespace Dsu {

using G::fir; using G::nex; using G::to;

array <int, N> siz, dep, fa, son;
array <int, N> dfn, idx;

array <int, N> cur;
queue <int> q;
int tot, _ans;

void add(int x) {
	cur[s[x]]++;
	q.push(s[x]);
	if (cur[s[x]] > tot) tot = cur[s[x]], _ans = s[x];
	else if (cur[s[x]] == tot) _ans += s[x];
}
void clear() {
	while (!q.empty())
		cur[q.front()] = 0, q.pop();
	tot = 0, _ans = 0;
}

int cnt;
void dfs1(int x) {
	cnt++;
	idx[cnt] = x;
	dfn[x] = cnt;
	siz[x] = 1;
	for (int i = fir[x]; i; i = nex[i]) {
		if (to[i] == fa[x]) continue;
		fa[to[i]] = x;
		dep[to[i]] = dep[x] + 1;
		dfs1(to[i]);
		siz[x] += siz[to[i]];
		if (siz[to[i]] > siz[son[x]]) son[x] = to[i];
	}
}

void dfs2(int x) {
	for (int i = fir[x]; i; i = nex[i]) {
		if (to[i] == fa[x] || to[i] == son[x]) continue;
		dfs2(to[i]), clear();
	}

	if (son[x]) dfs2(son[x]);

	for (int i = fir[x]; i; i = nex[i]) {
		if (to[i] == fa[x] || to[i] == son[x]) continue;
		for (int j = dfn[to[i]]; j <= dfn[to[i]] + siz[to[i]] - 1; j++)
			add(idx[j]);
	}
	add(x);
	ans[x] += _ans;
}

}

signed main() {
	int n = read();
	for (int i = 1; i <= n; i++)
		s[i] = read();
	for (int i = 2; i <= n; i++) {
		int x = read(), y = read();
		G::add(x, y), G::add(y, x);
	}
	Dsu::dfs1(1), Dsu::dfs2(1);
	for (int i = 1; i <= n; i++)
		write(ans[i]), putchar(32);
	puts("");
	return 0;
}

标签:fir,cnt,cur,int,son,gelral,CF600E,array,Lomsat
From: https://www.cnblogs.com/cxqghzj/p/17831960.html

相关文章

  • CF600E Lomsat gelral
    树上启发式合并(dsuontree)通常用来查询不带修的子树信息,信息要求可合并。对于一个结点\(u\),其步骤如下:求解其轻儿子的答案,同时清除递归产生的影响。求解其重儿子的答案,保留递归产生的影响。将轻儿子子树内的每个结点都合并进答案中,同时成为以\(u\)为根的子树产生的影响。......
  • CF600E Lomsat gelral(树上启发式合并)
    题目链接:https://codeforces.com/problemset/problem/600/E这是一道树上启发式合并的题,就只是在模板的基础上稍微变化了一下解题思路:我们需要计算当前u节点的答案,要计算加入非重链节点对此答案的影响,在计算加入节点对ans影响的时候,遍历u除了重链外的所有子树节点(因为重链部分的......
  • Educational Codeforces Round 2 E Lomsat gelral 线段树合并
    题目链接大致题意给你一棵有n个点的树,树上每个节点都有一种颜色ci(ci≤n),让你求每个点子树出现最多的颜色/编号的和记性不好,主要是为了防止自己忘掉,今天和队友合作写题......
  • Lomsat gelral
    Lomsatgelral统计节点出现次数最多的颜色之和概要:运用树链剖分,每次不用重新跑重儿子,从而将复杂度降低到nlogn#include<bits/stdc++.h>usingnamespacestd;usingl......