[Codeforces Round #221 (Div. 1)][D. Tree and Queries]
题目大意:给你一个有n个点的树,每个点都有其对应的颜色,给出m次询问(v,k),问v的子树中有多少种颜色至少出现k次
题解:先对所有的询问进行分类,即对所有相同的v合并到一起,这样就能转为离线处理(更新每个点的状态时同时求出答案)
开两个map<int,int>,cnt[i][j]表示 i 节点的子树中 j 颜色出现了多少次,f[i][j]则表示 i 节点的子树中至少出现 j 次的颜色个数。dfs的时候暴力枚举所有颜色合并能够得出正确答案,但这样做显然会TLE,因此需要用到启发式合并进行优化。即每次合并的时候是将小的集合并到大的集合中去,这样做的话就只需要枚举小集合里的元素,可以将合并的时间复杂度缩减到O(log n)的级别
#include<bits/stdc++.h>
using namespace std;
#define N 100001
int n,m,u,v,c[N],k[N],ans[N];
map<int,int>cnt[N],f[N];
vector<int>d[N],q[N];
void dfs(int cur,int pre)
{
cnt[cur][c[cur]]++,f[cur][]++;
for(auto nxt:d[cur])if(nxt!=pre)
{
dfs(nxt,cur);
if(cnt[nxt].size()>cnt[cur].size())
cnt[cur].swap(cnt[nxt]),f[cur].swap(f[nxt]);
for(auto x:cnt[nxt])
{
int y=cnt[cur][x.first];
cnt[cur][x.first]+=x.second;
for(int i=y+;i<=y+x.second;i++)f[cur][i]++;
}
}
for(auto i:q[cur])ans[i]=f[cur][k[i]];
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
scanf("%d",&c[i]);
for(int i=;i<=n;i++)
{
scanf("%d%d",&u,&v);
d[u].push_back(v);
d[v].push_back(u);
}
for(int i=;i<=m;i++)
scanf("%d%d",&v,&k[i]),q[v].push_back(i);
dfs(,);
for(int i=;i<=m;i++)
printf("%d\n",ans[i]);
}
[Codeforces Round #221 (Div. 1)][D. Tree and Queries]的更多相关文章
- Codeforces Round #499 (Div. 1) F. Tree
Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...
- 递推 Codeforces Round #186 (Div. 2) B. Ilya and Queries
题目传送门 /* 递推:用cnt记录前缀值,查询区间时,两个区间相减 */ #include <cstdio> #include <algorithm> #include &l ...
- Codeforces Round #316 (Div. 2) D. Tree Requests dfs序
D. Tree Requests time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- Codeforces Round #353 (Div. 2) D. Tree Construction 二叉搜索树
题目链接: http://codeforces.com/contest/675/problem/D 题意: 给你一系列点,叫你构造二叉搜索树,并且按输入顺序输出除根节点以外的所有节点的父亲. 题解: ...
- Codeforces Round #540 (Div. 3)--1118F1 - Tree Cutting (Easy Version)
https://codeforces.com/contest/1118/problem/F1 #include<bits/stdc++.h> using namespace std; in ...
- Codeforces Round #353 (Div. 2) D. Tree Construction 模拟
D. Tree Construction 题目连接: http://www.codeforces.com/contest/675/problem/D Description During the pr ...
- Codeforces Round #540 (Div. 3) F1. Tree Cutting (Easy Version) 【DFS】
任意门:http://codeforces.com/contest/1118/problem/F1 F1. Tree Cutting (Easy Version) time limit per tes ...
- Codeforces Round #527 (Div. 3) F. Tree with Maximum Cost 【DFS换根 || 树形dp】
传送门:http://codeforces.com/contest/1092/problem/F F. Tree with Maximum Cost time limit per test 2 sec ...
- 数据结构 - Codeforces Round #353 (Div. 2) D. Tree Construction
Tree Construction Problem's Link ------------------------------------------------------------------- ...
随机推荐
- Java利用ShutDownHook关闭系统资源
Java关闭钩子 在Java程序中能够通过加入关闭钩子,实如今程序退出时关闭资源的功能. 使用Runtime.addShutdownHook(Thread hook)向JVM加入关闭钩子 public ...
- 13、spark-submit
- Teigha.NET开发入门1- Teigha介绍
对于CAD开发,无疑较强大的方式是Lisp.AutoCAD二次开发,且学习资源丰富,依靠强大的AutoCAD的环境可以干很多事,省很多力.但若要脱离AutoCAD环境,那就当属Teigha了. 名称问 ...
- grid - 通过网格区域命名和定位网格项目
1.像网格线名称一样,网格区域的名称也可以使用grid-template-areas属性来命名.引用网格区域名称也可以设置网格项目位置. 设置网格区域的名称应该放置在单引号或双引号内,每个名称由一个空 ...
- CouchDB 简单HTTP接口使用说明
目录 1.简介 2.安装 2.HTTP接口简单使用 2.1.认证接口 2.1.1 Basic Authentication 2.1.2 Cookie Authentication 2.2 创建与删除数 ...
- NOIP2011普及组 数字反转
题目OJ链接: http://codevs.cn/problem/1130/ https://www.luogu.org/problemnew/show/P1307 2011年NOIP全国联赛普及组 ...
- Nginx Web服务应用
Nginx 指令目录 Nginx 介绍 Nginx 编译安装 Nginx 功能模块 Nginx 目录结构 Nginx 配置文件 Nginx 虚拟主机配置 Nginx 状态信息功能配置 Nginx 错误 ...
- Spark源码分析系列(目录)
记录自己学习研究 Spark 的探索过程,为后续总结奠定基础. 本文代码研究以 Spark 2.3.0 源代码为基准,如果看本文,请阅读时,下载对应的 Spark 版本. 图1 伯克利的数据分析软件栈 ...
- 解决Eclipse中“诡异”的错误:找不到或无法加载主类
记录下来遇到的(问题,解决方法),是更有效的解决问题的方式.(原谅我领悟的太晚与懒,从此用更有意义的方法,做一个更有意义的人) 因为遇到了多次,参考同一个方法,原文连接:https://blog.cs ...
- python中的目录递归的创建和目录及其下的文件递归的删除
1,删除目录data下的train及其下的所有文件,相当于linux中的rm -rf import shutil shutil.rmtree('data/train') 2,递归的创建目录data/t ...