Codeforces 600 E - Lomsat gelral
思路1:
树上启发式合并
代码:
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
//#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pii pair<int, int>
#define piii pair<int,pii>
#define mem(a, b) memset(a, b, sizeof(a))
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
//head const int N = 1e5 + ;
int color[N], sz[N], bs[N], mx = ;
LL ans[N], t = ;
vector<int>g[N];
map<int, int>mp;
void dfs(int o, int u) {
sz[u] = ;
for (int v : g[u]) {
if(v != o) {
dfs(u, v);
sz[u] += sz[v];
if(sz[v] > sz[bs[u]]) bs[u] = v;
}
}
}
void ADD(int o, int u) {
mp[color[u]] ++;
if(mp[color[u]] > mx) t = color[u], mx = mp[color[u]];
else if(mp[color[u]] == mx) t += color[u];
for (int v : g[u]) {
if(v != o) {
ADD(u, v);
}
}
}
void DFS(int o, int u) {
for (int v : g[u]) {
if(v != o && v != bs[u]) {
DFS(u, v);
mp.clear();
mx = ;
t = ;
}
}
if(bs[u]) DFS(u, bs[u]);
for (int v : g[u]) {
if(v != o && v != bs[u]) {
ADD(u, v);
}
}
mp[color[u]] ++;
if(mp[color[u]] > mx) t = color[u], mx = mp[color[u]];
else if(mp[color[u]] == mx) t += color[u];
ans[u] = t;
}
int main() {
int n, u, v;
scanf("%d", &n);
for (int i = ; i <= n; i++) scanf("%d", &color[i]);
for (int i = ; i < n; i++) {
scanf("%d %d", &u, &v);
g[u].pb(v);
g[v].pb(u);
}
dfs(, );
DFS(, );
for (int i = ; i <= n; i++) printf("%lld%c", ans[i], " \n"[i==n]);
return ;
}
思路2:
dfs序+分块 求区间众数和
代码:
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
//#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pli pair<LL, int>
#define pii pair<int, int>
#define piii pair<pii, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
//head const int N = 1e5 + ;
const int M = ;
vector<int> g[N];
vector<int> pos[N];
int a[N], t[N], in[N], out[N], cnt[N], bl[N], blo, tot = , n;
int f[M][M];
LL fres[M][M];
void dfs(int o, int u) {
in[u] = ++tot;
t[tot] = a[u];
for (int v : g[u]) {
if(v != o) {
dfs(u, v);
}
}
out[u] = tot;
}
void init(int x) {
for (int i = ; i <= n; i++) cnt[i] = ;
int ans = , c = ;
LL res = ;
for (int i = (x-)*blo + ; i <= n; i++) {
cnt[t[i]]++;
if(cnt[t[i]] > c) {
res = ans = t[i];
c = cnt[t[i]];
}
else if(cnt[t[i]] == c) {
ans = t[i];
res += t[i];
}
f[x][bl[i]] = ans;
fres[x][bl[i]] = res;
}
}
int cal(int l, int r, int x) {
if(l > r) return ;
return upper_bound(pos[x].begin(), pos[x].end(), r) - lower_bound(pos[x].begin(), pos[x].end(), l);
}
LL query(int l, int r) {
LL res = ;
int ans = , c = ;
if(bl[l] == bl[r]) {
vector<int> vc;
for (int i = l; i <= r; i++) vc.pb(t[i]);
sort(vc.begin(), vc.end());
vc.erase(unique(vc.begin(), vc.end()), vc.end());
for (int i = ; i < vc.size(); i++) {
int tot = cal(l, r, vc[i]);
if(tot > c) {
res = ans = vc[i];
c = tot;
}
else if(tot == c) {
ans = vc[i];
res += vc[i];
}
}
return res;
}
ans = f[bl[l]+][bl[r]-];
res = fres[bl[l]+][bl[r]-];
int L = bl[l]*blo+, R = (bl[r]-)*blo;
c = cal(L, R, ans);
vector<int> vc;
for (int i = l; i <= bl[l]*blo; i++) vc.pb(t[i]);
for (int i = (bl[r]-)*blo + ; i <= r; i++) vc.pb(t[i]);
sort(vc.begin(), vc.end());
vc.erase(unique(vc.begin(), vc.end()), vc.end());
for (int i = ; i < vc.size(); i++) {
int tot = cal(l, r, vc[i]);
if(tot > c) {
res = ans = vc[i];
c = tot;
}
else if(tot == c) {
ans = vc[i];
res += vc[i];
}
}
return res;
}
int main() {
int u, v;
scanf("%d", &n);
blo = sqrt(n);
for (int i = ; i <= n; i++) scanf("%d", &a[i]);
for (int i = ; i < n; i++) {
scanf("%d %d", &u, &v);
g[u].pb(v);
g[v].pb(u);
}
dfs(, );
for (int i = ; i <= n; i++) bl[i] = (i-)/blo + ;
for (int i = ; i <= bl[n]; i++) init(i);
for (int i = ; i <= n; i++) pos[t[i]].pb(i);
for (int i = ; i <= n; i++) printf("%lld%c", query(in[i], out[i]), " \n"[i==n]);
return ;
}
Codeforces 600 E - Lomsat gelral的更多相关文章
- Codeforces 600 E. Lomsat gelral (dfs启发式合并map)
题目链接:http://codeforces.com/contest/600/problem/E 给你一棵树,告诉你每个节点的颜色,问你以每个节点为根的子树中出现颜色次数最多的颜色编号和是多少. 最容 ...
- codeforces 600E E. Lomsat gelral (线段树合并)
codeforces 600E E. Lomsat gelral 传送门:https://codeforces.com/contest/600/problem/E 题意: 给你一颗n个节点的树,树上的 ...
- 【CodeForces】600 E. Lomsat gelral (dsu on tree)
[题目]E. Lomsat gelral [题意]给定n个点的树,1为根,每个点有一种颜色ci,一种颜色占领一棵子树当且仅当子树内没有颜色的出现次数超过它,求n个答案——每棵子树的占领颜色的编号和Σc ...
- CF 600 E. Lomsat gelral
E. Lomsat gelral http://codeforces.com/contest/600/problem/E 题意: 求每个子树内出现次数最多的颜色(如果最多的颜色出现次数相同,将颜色编号 ...
- 【Codeforces】600E. Lomsat gelral
Codeforces 600E. Lomsat gelral 学习了一下dsu on tree 所以为啥是dsu而不是dfs on tree??? 这道题先把这棵树轻重链剖分了,然后先处理轻儿子,处理 ...
- DSU On Tree——Codeforces 600E(E. Lomsat gelral)
有这么一类问题,要求统计一棵树上与子树相关的某些信息,比如:在一棵所有节点被染色的树上,统计每棵子树上出现次数最多的颜色编号之和. 很自然的可以想到用DFS序+主席树去求解,但是编码复杂度很高: 然后 ...
- CF 600 E Lomsat gelral —— 树上启发式合并
题目:http://codeforces.com/contest/600/problem/E 看博客:https://blog.csdn.net/blue_kid/article/details/82 ...
- Educational Codeforces Round 2 E. Lomsat gelral 启发式合并map
E. Lomsat gelral Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/600/prob ...
- Codeforces 600E - Lomsat gelral(树上启发式合并)
600E - Lomsat gelral 题意 给出一颗以 1 为根的树,每个点有颜色,如果某个子树上某个颜色出现的次数最多,则认为它在这课子树有支配地位,一颗子树上,可能有多个有支配的地位的颜色,对 ...
随机推荐
- keepalived与zookeeper
keepalived与zookeeper都可以用来实现高可用,高可用一般跟负载均衡会一起考虑,所以通常也会考虑到相应的负载均衡能力, 1.概括对比: 1.1.Keepalived: 优点:简单,基本不 ...
- jsxyhelu的GitHub使用方法
如果只是使用Clone不能称得上是完全使用了GitHub,必须完成PullRequest,而且最好是对大型.带自动构建项目进行PR(比如OpenCV),这样才叫完全掌握GitHub的使用方法,这里分享 ...
- 《C Elements of Style》 书摘
<C Elements of Style> 书摘 学完C语言和数据结构后,虽然能解决一些问题,但总觉得自己写的程序丑陋,不专业.这时候看到了Steve Oualline写的<C El ...
- VC++使用MapFileAndCheckSum完成自检测
转载:https://blog.csdn.net/lekonpeng/article/details/4150464 当我们完成一个工程,编译成可执行文件后,一般是不希望这个可执行文件被人改动的,那么 ...
- Hacker
https://hackertarget.com/nikto-website-scanner/
- Linux内核中的wake_lock[【转】
本文转载自:https://blog.csdn.net/wuyb2011/article/details/78542233?locationNum=11&fps=1 #include < ...
- cygwin下如何编译安装minicom?
1. 安装依赖的软件和库 apt-cyg install autoconf automake make libncurses-devel (apt-cyg工具的安装方法在此) 2. 获取源码 wget ...
- Asp.net 之 window 操作命令
命令:cmd 打开执行窗口 命令:inetmgr.打开iis管理器 命令:dcomcnfg 打开组件服务 命令:regedit 打开注册表
- Hierarchical Question-Image Co-Attention for Visual Question Answering
Hierarchical Question-Image Co-Attention for Visual Question Answering NIPS 2016 Paper: https://arxi ...
- Visual Question Answering with Memory-Augmented Networks
Visual Question Answering with Memory-Augmented Networks 2018-05-15 20:15:03 Motivation: 虽然 VQA 已经取得 ...