Lomsat gelral cf-600e
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的更多相关文章
- 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(树上启发式合并)
600E - Lomsat gelral 题意 给出一颗以 1 为根的树,每个点有颜色,如果某个子树上某个颜色出现的次数最多,则认为它在这课子树有支配地位,一颗子树上,可能有多个有支配的地位的颜色,对 ...
- 【Codeforces】600E. Lomsat gelral
Codeforces 600E. Lomsat gelral 学习了一下dsu on tree 所以为啥是dsu而不是dfs on tree??? 这道题先把这棵树轻重链剖分了,然后先处理轻儿子,处理 ...
- codeforces 600E E. Lomsat gelral (线段树合并)
codeforces 600E E. Lomsat gelral 传送门:https://codeforces.com/contest/600/problem/E 题意: 给你一颗n个节点的树,树上的 ...
- CF 600 E. Lomsat gelral
E. Lomsat gelral http://codeforces.com/contest/600/problem/E 题意: 求每个子树内出现次数最多的颜色(如果最多的颜色出现次数相同,将颜色编号 ...
- Codeforces 600E Lomsat gelral (树上启发式合并)
题目链接 Lomsat gelral 占坑……等深入理解了再来补题解…… #include <bits/stdc++.h> using namespace std; #define rep ...
- CF EDU - E. Lomsat gelral 树上启发式合并
学习:http://codeforces.com/blog/entry/44351 E. Lomsat gelral 题意: 给定一个以1为根节点的树,每个节点都有一个颜色,问每个节点的子树中,颜色最 ...
- DSU On Tree——Codeforces 600E(E. Lomsat gelral)
有这么一类问题,要求统计一棵树上与子树相关的某些信息,比如:在一棵所有节点被染色的树上,统计每棵子树上出现次数最多的颜色编号之和. 很自然的可以想到用DFS序+主席树去求解,但是编码复杂度很高: 然后 ...
- 【CF600E】Lomsat gelral(dsu on tree)
[CF600E]Lomsat gelral(dsu on tree) 题面 洛谷 CF题面自己去找找吧. 题解 \(dsu\ on\ tree\)板子题 其实就是做子树询问的一个较快的方法. 对于子树 ...
- 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 ...
随机推荐
- ThoughtWorks技术雷达
ThoughtWorks技术雷达 技术成熟方案的一个推荐网站.
- Object.getOwnPropertyNames()
1.Object.getOwnPropertyNames(),遍历实例属性(包括不可枚举),返回属性名组成的数组 var arr = ["a", "b", &q ...
- 暴力破解zip文件
#coding=utf-8 """ 用户输入-z参数指定要破解的zip文件,输入-d参数输入字典文件,即可暴力破解加密的zip文件 """ ...
- 【小技能】如何检索苹果APP
有时候要临时在PC上查询一下苹果APP的信息,但是又没有安装itunes软件,那么你可以在必应里面使用类似如下的语句,例如来搜索“aboboo site:itunes.apple.com”,就可以搜索 ...
- iOS开发——高级篇——多线程GCD死锁
面试题 请问以下代码打印结果: - (void)interview01 { // 以下代码是在主线程执行的 NSLog(@"执行任务1"); dispatch_queue_t qu ...
- 【bzoj2809】[Apio2012]dispatching (左偏树)
我们需要枚举根,然后从其子树内选尽量多的点,薪水不超过M,可是暴力复杂度不对.于是考虑自下而上合并树(开始每棵树内只有一个节点,就是自己) 每个树是一个堆,我们维护树的节点个数和薪水总和,合并时,不断 ...
- OutputStream和InputStream的区别 + 实现java序列化
我们所说的流,都是针对内存说的,比如为什么打印到屏幕上就是System.out.println();而从屏幕等待用户输入的却是System.in呢?因为对于内存来说,把字符串打印到屏幕上是从内存流向屏 ...
- 大括号对struct进行初始化
1 partial initialization 即所谓的部分初始化. 这个时候,无论该struct变量是static的还是automic的,未显式初始化的成员都会被初始化为默认值.
- CXF Spring 使用
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...
- How to use filters in a GridPanel
You can just link statically required files in your index.html <link rel="stylesheet" t ...