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的更多相关文章

  1. Codeforces 600 E. Lomsat gelral (dfs启发式合并map)

    题目链接:http://codeforces.com/contest/600/problem/E 给你一棵树,告诉你每个节点的颜色,问你以每个节点为根的子树中出现颜色次数最多的颜色编号和是多少. 最容 ...

  2. codeforces 600E E. Lomsat gelral (线段树合并)

    codeforces 600E E. Lomsat gelral 传送门:https://codeforces.com/contest/600/problem/E 题意: 给你一颗n个节点的树,树上的 ...

  3. 【CodeForces】600 E. Lomsat gelral (dsu on tree)

    [题目]E. Lomsat gelral [题意]给定n个点的树,1为根,每个点有一种颜色ci,一种颜色占领一棵子树当且仅当子树内没有颜色的出现次数超过它,求n个答案——每棵子树的占领颜色的编号和Σc ...

  4. CF 600 E. Lomsat gelral

    E. Lomsat gelral http://codeforces.com/contest/600/problem/E 题意: 求每个子树内出现次数最多的颜色(如果最多的颜色出现次数相同,将颜色编号 ...

  5. 【Codeforces】600E. Lomsat gelral

    Codeforces 600E. Lomsat gelral 学习了一下dsu on tree 所以为啥是dsu而不是dfs on tree??? 这道题先把这棵树轻重链剖分了,然后先处理轻儿子,处理 ...

  6. DSU On Tree——Codeforces 600E(E. Lomsat gelral)

    有这么一类问题,要求统计一棵树上与子树相关的某些信息,比如:在一棵所有节点被染色的树上,统计每棵子树上出现次数最多的颜色编号之和. 很自然的可以想到用DFS序+主席树去求解,但是编码复杂度很高: 然后 ...

  7. CF 600 E Lomsat gelral —— 树上启发式合并

    题目:http://codeforces.com/contest/600/problem/E 看博客:https://blog.csdn.net/blue_kid/article/details/82 ...

  8. 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 ...

  9. Codeforces 600E - Lomsat gelral(树上启发式合并)

    600E - Lomsat gelral 题意 给出一颗以 1 为根的树,每个点有颜色,如果某个子树上某个颜色出现的次数最多,则认为它在这课子树有支配地位,一颗子树上,可能有多个有支配的地位的颜色,对 ...

随机推荐

  1. oracle已知会导致错误结果的bug列表(Bug Issues Known to cause Wrong Results)

    LAST UPDATE:     1 Dec 15, 2016 APPLIES TO:     1 2 3 4 Oracle Database - Enterprise Edition - Versi ...

  2. SqlServer字符串拼接

    ID    Name 2 小红 2 小明 2 小青 3 大红 3 大明 3 大青 有一张这样的表,现在要达到 把ID为2的Name合拼成一行显示出来: step1:建函数 CREATE  FUNCTI ...

  3. tf.nn.max_pool

    tf.nn.max_pool(value, ksize, strides, padding, name=None)   参数是四个,和卷积很类似: Args Annotation 第一个参数value ...

  4. 本地git远程github

    操作环境: windows系统 本地已安装git 步骤 1.右键进入git bash 2.进入本地仓库,如果有,则跳到步骤3.如果没有,则找到要作为仓库的文件夹,例如e://git//reposito ...

  5. Java——List:list.add(index, element)和list.set(index, element)的区别

    add(index, element) 含义:在集合索引为index的位置上增加一个元素element,集合list改变后list.size()会增加1 用法 testList.add(index, ...

  6. UML(统一建模语言)是通用的可视化标准建模语言。由构造块、公共机制、构架三部分组成。

    UML UML(统一建模语言)是通用的可视化标准建模语言.由构造块.公共机制.构架三部分组成. 1.构造块:包括基本的UML建模元素(类.接口.用例等).关系(关联关系.依赖关系.泛化关系.实现关系) ...

  7. sonarqube中new issue的标准

    https://docs.sonarqube.org/latest/user-guide/issues/#header-4 Understanding which Issues are "N ...

  8. 论文笔记之:Natural Language Object Retrieval

    论文笔记之:Natural Language Object Retrieval 2017-07-10  16:50:43   本文旨在通过给定的文本描述,在图像中去实现物体的定位和识别.大致流程图如下 ...

  9. (转)PaperWeekly 第二十二期---Image Caption任务综述

    本文转自:http://mp.weixin.qq.com/s?__biz=MzIwMTc4ODE0Mw==&mid=2247484014&idx=1&sn=4a053986f5 ...

  10. js 模块化规范

    模块规范 CommonJS module.exports, exports 导出模块 require 加载模块, CommonJS 同步,服务端.实践者: nodejs ES6 export, exp ...