http://codeforces.com/contest/600/problem/E

暴力启发式合并就行了

提示:set的swap的复杂度是常数,这方面可以放心

我先打了一个很naive的算法

 #include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> P;
typedef set<P> s;
//P(颜色出现次数,颜色编号)
typedef map<LL,LL> m;
//颜色编号->颜色出现次数
typedef pair<s,m> P2;
LL n,anss[],c[];
vector<LL> e[];
P2 dfs(LL u,LL fa)
{
P2 t,ans;
ans.first.emplace(,c[u]);
ans.second[c[u]]=;
for(auto v:e[u])
if(v!=fa)
{
t=dfs(v,u);
if(t.second.size()>ans.second.size()) swap(t,ans);
for(auto it:t.second)
{
ans.first.erase(P(ans.second[it.first],it.first));
ans.second[it.first]+=it.second;
ans.first.emplace(ans.second[it.first],it.first);
}
}
int maxsz=ans.first.rbegin()->first;
for(auto it=ans.first.rbegin();it!=ans.first.rend()&&it->first==maxsz;it++) anss[u]+=it->second;
return ans;
}
int main()
{
LL i,x,y;
scanf("%lld",&n);
for(i=;i<=n;i++) scanf("%lld",&c[i]);
for(i=;i<n;i++)
{
scanf("%lld%lld",&x,&y);
e[x].push_back(y);e[y].push_back(x);
}
dfs(,);
for(i=;i<=n;i++) printf("%lld ",anss[i]);
return ;
}

毫不意外的被卡掉了~看第34行,怎么看都不对嘛

有两种解决方法:

第一种:每一个节点dfs的时候额外返回一个值,记录当前子树的答案。

第二种:将set换成另一个map<int,int>,记录颜色出现次数->编号之和

 #include<cstdio>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;
typedef long long LL;
typedef map<LL,LL> m;
//颜色出现次数->编号之和
//颜色编号->颜色出现次数
typedef pair<m,m> P2;
LL n,anss[],c[];
vector<LL> e[];
P2 dfs(LL u,LL fa)
{
P2 t,ans;
ans.first[]=c[u];
ans.second[c[u]]=;
for(auto v:e[u])
if(v!=fa)
{
t=dfs(v,u);
if(t.second.size()>ans.second.size()) swap(t,ans);
for(auto it:t.second)
{
ans.first[ans.second[it.first]]-=it.first;
ans.second[it.first]+=it.second;
ans.first[ans.second[it.first]]+=it.first;
}
}
anss[u]=ans.first.rbegin()->second;
return ans;
}
int main()
{
LL i,x,y;
scanf("%lld",&n);
for(i=;i<=n;i++) scanf("%lld",&c[i]);
for(i=;i<n;i++)
{
scanf("%lld%lld",&x,&y);
e[x].push_back(y);e[y].push_back(x);
}
dfs(,);
for(i=;i<=n;i++) printf("%lld ",anss[i]);
return ;
}

也可以将每个节点dfs返回的值改成全局变量(似乎可以避免一些玄学的常数问题)

Lomsat gelral cf-600e的更多相关文章

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

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

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

  3. 【Codeforces】600E. Lomsat gelral

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

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

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

  5. CF 600 E. Lomsat gelral

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

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

    题目链接 Lomsat gelral 占坑……等深入理解了再来补题解…… #include <bits/stdc++.h> using namespace std; #define rep ...

  7. CF EDU - E. Lomsat gelral 树上启发式合并

    学习:http://codeforces.com/blog/entry/44351 E. Lomsat gelral 题意: 给定一个以1为根节点的树,每个节点都有一个颜色,问每个节点的子树中,颜色最 ...

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

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

  9. 【CF600E】Lomsat gelral(dsu on tree)

    [CF600E]Lomsat gelral(dsu on tree) 题面 洛谷 CF题面自己去找找吧. 题解 \(dsu\ on\ tree\)板子题 其实就是做子树询问的一个较快的方法. 对于子树 ...

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

随机推荐

  1. [教程]Delphi 中三种回调函数形式解析

    Delphi 支持三种形式的回调函数 全局函数这种方式几乎是所有的语言都支持的,类的静态函数也可以归为此类,它保存的只是一个函数的代码起始地址指针( Pointer ).在 Delphi 中声明一般为 ...

  2. centos No module named setuptools解决方案

    wget http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz tar zxvf setuptool ...

  3. [Zlib]_[0基础]_[使用zlib库压缩文件]

    场景: 1. WIndows上没找到系统提供的win32 api来生成zip压缩文件, 有知道的大牛麻烦留个言. 2. zlib比較经常使用,编译也方便,使用它来做压缩吧. MacOSX平台默认支持z ...

  4. unix时间戳(unix timestamp)与北京时间的互转方法

    1.在linux bash下北京时间与unix时间戳互转: 获取unix timestamp: 命令:date "+%s" 输出:1372654714 获取北京时间: 命令:dat ...

  5. robotframework接口自动化

    robot framework框架在测试接口上比soapUI好用的多,在此介绍下get方法的HTTP接口,其实这个接口也是把POST数据作为参数进行get请求,使用post 方法也是一样,一共6步就可 ...

  6. 自己定义验证器——用Struts2框架以框架师的思维灵活做好该事情

    面对的问题:自己定义一个18位身份验证器.编写验证器.在validators.xml文件里进行注冊.在验证配置文件里使用? 第一部分:理解Struts2中自带的验证器 第二部分:如何通过server( ...

  7. 深度学习笔记之关于总结、展望、参考文献和Deep Learning学习资源(五)

    不多说,直接上干货! 十.总结与展望 1)Deep learning总结 深度学习是关于自动学习要建模的数据的潜在(隐含)分布的多层(复杂)表达的算法.换句话来说,深度学习算法自动的提取分类需要的低层 ...

  8. Makefile详解 (转--不错就是有点长)

    概述 —— 什么是makefile?或许很多Winodws的程序员都不知道这个东西,因为那些Windows的IDE都为你做了这个工作,但我觉得要作一个好的和 professional的程序员,make ...

  9. getifaddrs

    getifaddrs 获取本地网络接口的信息.在路由器上可以用这个接口来获取wan/lan等接口当前的ip地址,广播地址等信息. #include <sys/types.h> #inclu ...

  10. druid 参考配置

    mysql <!-- 配置druid连接池 --> <context:property-placeholder location="classpath:jdbc.prope ...