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. Spring Boot(十四):spring boot整合shiro-登录认证和权限管理

    Spring Boot(十四):spring boot整合shiro-登录认证和权限管理 使用Spring Boot集成Apache Shiro.安全应该是互联网公司的一道生命线,几乎任何的公司都会涉 ...

  2. 20145326蔡馨熤《网络对抗》—— Web安全基础实践

    20145326蔡馨熤<网络对抗>—— Web安全基础实践 1.实验后回答问题 (1)SQL注入攻击原理,如何防御. 原理: SQL注入攻击指的是通过构建特殊的输入作为参数传入Web应用程 ...

  3. Java基础语法(上)

    Java编译报错出现非法字符,原因是存在中文字符. Java关键字的字母都是小写. Java是一种强类型语言,针对每一种数据都给出了明确的数据类型. 数据类型分类: A:基本数据类型 B:引用数据类型 ...

  4. rabbitmq install erlang faild

    我安装的时候莫名的出现这2个错误 No Presto metadata available for rabbitmq-erlangwarning: /var/cache/yum/x86_64/7/ra ...

  5. 【python39--面向对象组合】

    一.组合 定义:当几个对象是水平方向的时候,就应该考虑组合,当对象是纵向的时候用继承,组合就是用一个类把2个平级层次的类放在一起,然后实例化就可以了 #现在定义一个类,叫水池,水池里面有鱼和乌龟cla ...

  6. Git冲突与解决方法【转】

    本文转载自:https://www.cnblogs.com/gavincoder/p/9071959.html Git冲突与解决方法 1.git冲突的场景 情景一:多个分支代码合并到一个分支时: 情景 ...

  7. SpringBoot 使用Mybatis-Plus

    简介 Mybatis-Plus(简称MP)是一个 Mybatis 的增强工具,在 Mybatis 的基础上只做增强不做改变,为简化开发.提高效率而生. 特性 无侵入:Mybatis-Plus 在 My ...

  8. (转)Shiro学习

    (二期)13.权限框架shiro讲解 [课程13]自定义Realm.xmind36.8KB [课程13]用户授权流程.xmind0.2MB [课程13]shiro简介.xmind0.3MB [课程13 ...

  9. IDEA新建一个Project和Module的问题

  10. P3833 [SHOI2012]魔法树

    思路 树剖板子 注意给出点的编号是从零开始的 代码 #include <cstdio> #include <algorithm> #include <cstring> ...