[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 ------------------------------------------------------------------- ...
随机推荐
- arcgis pro指数库
来自:https://pro.arcgis.com/zh-cn/pro-app/help/data/imagery/indices-gallery.htm 植被和土壤指数 MSAVI “修正土壤调节植 ...
- 《Python数据分析与挖掘实战》读书笔记
大致扫了一遍,具体的代码基本都没看了,毕竟我还不懂python,并且在手机端的排版,这些代码没法看. 有收获,至少了解到以下几点: 一. Python的语法挺有意思的 有一些类似于JavaSc ...
- GPG(GnuPG)入门
GPG(GnuPG)入门 下载: https://gnupg.org/download/index.html 或者 http://www.hellopp.cn/page/5b9a1405c3f1f7d ...
- Print all attributes and values in a Javascript Object
function printObject(o) { var out = ''; for (var p in o) { out += '\n' + ':: ' + p + '(' + typeof(o[ ...
- jvm系列
一.jvm理论 1.1.jvm理论-总述 1.2.jvm理论-class文件 1.2.1.jvm理论-常量池-string 1.2.2.jvm理论-常量池-8种基本类型 1.3.jvm理论-字节码指令 ...
- No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer
异常信息如下所示: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for cla ...
- SQL SERVER 批量生成编号
开始: 在testing中,为了模拟orders,有个要求给数据库dba,如何通过后台数据库脚本快速批量生成orders. 分析 站在数据库角度,批量生成orders,也就是批量生成表中的行数据. s ...
- [moosefs] storage class
chapter 1 moosefs 3.1 storage class 功能的介绍 1.1 什么是storage class 在moosefs中,storage class允许指定文件的chunks存 ...
- 试试SQLServer 2014的内存优化表
SQL Server2014存储引擎:行存储引擎,列存储引擎,内存引擎 SQL Server 2014中的内存引擎(代号为Hekaton)将OLTP提升到了新的高度. 现在,存储引擎已整合进当前的数据 ...
- word,excel,ppt,txt转换为 PDF
/// <summary> /// 将word文档转换成PDF格式 /// </summary> /// <param name="sourcePath&quo ...