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. centos mysql错误信息处理

    mysql_secure_installation 提示错误:Enter current password for root (enter for none):ERROR 1045 (28000): ...

  2. Android四大组件与进程启动的关系(转)

    一. 概述 Android系统将进程做得很友好的封装,对于上层app开发者来说进程几乎是透明的. 了解Android的朋友,一定知道Android四大组件,但对于进程可能会相对较陌生. 一个进程里面可 ...

  3. 使用python转换markdown to html

    起因 有很多编辑器可以直接将markdown转换成html,为什么还要自己写呢?因为我想写完markdown之后,即可以保存在笔记软件中(比如有道),又可以放到github进行版本管理,还可以发布到博 ...

  4. 程序C++ to C#交互

    第一次用C#调用C/C++生成的DLL文件,感觉有点新鲜,事实上仅仅是实现了执行在公共语言执行库 (CLR) 的控制之外的"非托管代码"(执行在公共语言执行库(CLR)的控制之中的 ...

  5. phpqrcode生成带logo的二维码图片及带文字的二维码图片

    <?php require_once "./phpqrcode/phpqrcode.php"; /** * 这样就可以生成二维码了,实际上在png这个方法里还有几个参数需要使 ...

  6. ==和equals的差别

    == 和 Equals 的差别 1. == 是一个运算符. 2.Equals则是string对象的方法.能够.(点)出来. 我们比較无非就是这两种 1.基本数据类型比較 2.引用对象比較 1.基本数据 ...

  7. IO流(字节流复制)01

    package ioDemo; import java.io.*; /** * IO流(字节流复制) * Created by lcj on 2017/11/2. */ public class bu ...

  8. debug找到source lookup path以及,debug跑到另外的解决办法

    在我们使用eclipse调试的时候,有时候会出一些奇葩的问题,比如找不到Source  lookup path, 这时我们可以点击Edit Source Lookup Path.接着回弹出一个 我们只 ...

  9. Android-shareSDK

    1.当数据: 地址:http://sharesdk.mob.com/Download 2.集成数据: DOS命令: java -jar QuickIntegrater.jar    (输入自己的项目名 ...

  10. ou've likely run out of ephemeral ports on your system

    redis.exceptions.ConnectionError: Error 99 connecting to 127.0.0.1:6379. Cannot assign requested add ...