codeforces 600E. Lomsat gelral 启发式合并
给一颗树, 每个节点有初始的颜色值。 1为根节点。定义一个节点的值为, 它的子树中出现最多的颜色的值, 如果有多种颜色出现的次数相同, 那么值为所有颜色的值的和。
每一个叶子节点是一个map, 然后从叶子节点并上去, 注意并的时候把小的map并到大的map里面。
- #include<bits/stdc++.h>
- using namespace std;
- #define pb(x) push_back(x)
- #define ll long long
- #define mk(x, y) make_pair(x, y)
- #define lson l, m, rt<<1
- #define mem(a) memset(a, 0, sizeof(a))
- #define rson m+1, r, rt<<1|1
- #define mem1(a) memset(a, -1, sizeof(a))
- #define mem2(a) memset(a, 0x3f, sizeof(a))
- #define rep(i, a, n) for(int i = a; i<n; i++)
- #define ull unsigned long long
- typedef pair<int, int> pll;
- const double PI = acos(-1.0);
- const double eps = 1e-;
- const int mod = 1e9+;
- const int inf = ;
- const int dir[][] = { {-, }, {, }, {, -}, {, } };
- const int maxn = 1e5+;
- int head[maxn*], color[maxn], num, cnt[maxn], id[maxn];
- ll anss[maxn], ans[maxn];
- struct node
- {
- int to, nextt;
- }e[maxn*];
- void add(int u, int v) {
- e[num].to = v, e[num].nextt = head[u], head[u] = num++;
- }
- map <int, int> m[maxn];
- void combine(int &u, int &v) {
- if(m[u].size()<m[v].size())
- swap(u, v);
- for(auto it = m[v].begin(); it!=m[v].end(); it++) {
- m[u][it->first] += it->second;
- if(m[u][it->first]>cnt[u]) {
- cnt[u] = m[u][it->first];
- ans[u] = it->first;
- } else if(m[u][it->first] == cnt[u]) {
- ans[u] += it->first;
- }
- }
- }
- void dfs(int u, int fa) {
- for(int i = head[u]; ~i; i = e[i].nextt) {
- int v = e[i].to;
- if(v == fa)
- continue;
- dfs(v, u);
- combine(id[u], id[v]);
- }
- anss[u] = ans[id[u]];
- }
- int main()
- {
- mem1(head);
- int n, x, y;
- cin>>n;
- for(int i = ; i<=n; i++) {
- scanf("%d", &x);
- id[i] = i;
- m[i][x] = ;
- cnt[i] = ;
- ans[i] = x;
- }
- for(int i = ; i<n-; i++) {
- scanf("%d%d", &x, &y);
- add(x, y);
- add(y, x);
- }
- dfs(, );
- for(int i = ; i<=n; i++) {
- printf("%I64d ", anss[i]);
- }
- return ;
- }
codeforces 600E. Lomsat gelral 启发式合并的更多相关文章
- Codeforces 600E - Lomsat gelral(树上启发式合并)
600E - Lomsat gelral 题意 给出一颗以 1 为根的树,每个点有颜色,如果某个子树上某个颜色出现的次数最多,则认为它在这课子树有支配地位,一颗子树上,可能有多个有支配的地位的颜色,对 ...
- Codeforces 600E Lomsat gelral (树上启发式合并)
题目链接 Lomsat gelral 占坑……等深入理解了再来补题解…… #include <bits/stdc++.h> using namespace std; #define rep ...
- 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(Dsu on tree学习)
题目链接:http://codeforces.com/problemset/problem/600/E n个点的有根树,以1为根,每个点有一种颜色.我们称一种颜色占领了一个子树当且仅当没有其他颜色在这 ...
- Lomsat gelral CodeForces - 600E (树上启发式合并)
You are given a rooted tree with root in vertex 1. Each vertex is coloured in some colour. Let's cal ...
- codeforces 600E . Lomsat gelral (线段树合并)
You are given a rooted tree with root in vertex 1. Each vertex is coloured in some colour. Let's cal ...
- Codeforces 600E Lomsat gelral(dsu on tree)
dsu on tree板子题.这个trick保证均摊O(nlogn)的复杂度,要求资瓷O(1)将一个元素插入集合,清空集合时每个元素O(1)删除.(当然log的话就变成log^2了) 具体的,每次先遍 ...
- codeforces 600E Lomsat gelral
题面:codeforces600E 学习一下$dsu \ on \ tree$.. 这个东西可以处理很多无修改子树问题,复杂度通常为$O(nlogn)$. 主要操作是:我们先把整棵树链剖一下,然后每次 ...
- Codeforces.600E.Lomsat gelral(dsu on tree)
题目链接 dsu on tree详见这. \(Description\) 给定一棵树.求以每个点为根的子树中,出现次数最多的颜色的和. \(Solution\) dsu on tree模板题. 用\( ...
随机推荐
- [Android]Plug-in com.android.ide.eclipse.adt was unable to load class com.android.ide
今天启动eclipse的时候报了上述错误,打开xml是都报错.其实解决方法很简单:重启eclipse即可.
- tmux 命令
创建并指定session名字tmux new -s $session_name 删除sessionCtrl+b :kill-session 临时退出sessionCtrl+b d 列出sessiont ...
- Ubuntu安装Mysql及使用
(1)在线安装:sudo apt-get install mysql-server(2)管理mysql: sudo /etc/init.d/mysql start===========>开启my ...
- WGS84、GCJ-02(火星坐标)、百度坐标,Web墨卡托坐标
GCJ-02坐标系统(火星坐标)简介:http://blog.csdn.net/giswens/article/details/8775121(存档:http://mapbd.com/cms/2012 ...
- 移动端使用rem方法
(function (doc, win) { var docEl = doc.documentElement, resizeEvt = 'orientationchange' in window ? ...
- 解析:用 CSS3 和 JavaScript 制作径向动画菜单
原作者的解析(英文):http://creative-punch.net/2014/02/making-animated-radial-menu-css3-javascript/ 原作者的解析(译文) ...
- 数组初始化(c, c++, gcc, g++)
这是很基础的东西,但基础的重要性不言而喻,我敢肯定这个知识点我肯定曾经了解过,但现在,我不敢确定,由此可见纪录的重要性,这世界没有什么捷径,找对方向,然后不停重复.所以从今天开始,我会比较详细的纪录这 ...
- MVC自学第四课
处理表单 前面的列子我们已经把表单提交给了HomeControllers类中的RsvpForm动作方法,只是在这个动作方法内,我们并没有做什么具体的逻辑处理,而是直接返回一个 “Thanks”的视图. ...
- Web自动化基础分享
一.Selenium 简介 Selenium 是 ThoughtWorks 专门为 Web 应用程序编写的一个验收测试工具. 与其他测试工具相比,使用 Selenium 的最大好处是: Seleniu ...
- MySQL----cluster安装
第一步.下载MySQL cluster: http://cdn.mysql.com/Downloads/MySQL-Cluster-7.4/mysql-cluster-gpl-7.4.7-linux- ...