题目链接:375D - 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]的更多相关文章

  1. Codeforces Round #499 (Div. 1) F. Tree

    Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...

  2. 递推 Codeforces Round #186 (Div. 2) B. Ilya and Queries

    题目传送门 /* 递推:用cnt记录前缀值,查询区间时,两个区间相减 */ #include <cstdio> #include <algorithm> #include &l ...

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

  4. Codeforces Round #353 (Div. 2) D. Tree Construction 二叉搜索树

    题目链接: http://codeforces.com/contest/675/problem/D 题意: 给你一系列点,叫你构造二叉搜索树,并且按输入顺序输出除根节点以外的所有节点的父亲. 题解: ...

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

  6. Codeforces Round #353 (Div. 2) D. Tree Construction 模拟

    D. Tree Construction 题目连接: http://www.codeforces.com/contest/675/problem/D Description During the pr ...

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

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

  9. 数据结构 - Codeforces Round #353 (Div. 2) D. Tree Construction

    Tree Construction Problem's Link ------------------------------------------------------------------- ...

随机推荐

  1. C# RabbitMQ优先级队列实战项目演练

    一.需求背景 当用户在商城上进行下单支付,针对客户等级的不同和订单金额的大小划分客户级别,需要优先处理给标识为大订单的客户发送一份订单邮件提醒.那么我们应用程序如何解决这样的需求场景呢?今天阿笨给大家 ...

  2. IDEA手工添加webapp目录

    自己手工建目录,是没法识别的,在自己手工建的webapp文件夹上右键菜单,Make Directory As也没有相应的选项 解决方案是 File->Project Structure

  3. xhprof查看性能测试图一直报错:failed to execute cmd: " dot -Tpng"多种因素解决方案

    xhprof查看性能测试图一直报错:failed to execute cmd: ” dot -Tpng”多种因素解决方案最近在新环境进行php代码性能测试,用了xhprof这个工具,搭建好以后,点击 ...

  4. javascript umd esm slim

    在CDN的连接中看到多个连接时如何选择? JavaScript 模块现状 UMD和ECMAScript模块 https://cdn.bootcss.com/popper.js/1.13.0/esm/p ...

  5. ionic ion-tab图标修改, 自定义tab图标

    遇到需要自定义tab图标很简单, 只需要自定义相应的css就可以了, 这里要注意的是如何调整背景图片的大小. <ion-view hide-back-button="false&quo ...

  6. vue: 代码小记

    1.事件派发:子控件->父控件 <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ...

  7. solr+zookeeper集群配置

    将solr配置文件交给zookeeper进行管理 ./zkcli.sh -zkhost node01:2181,node02:2181,node03:2181 -cmd upconfig -confd ...

  8. 【C#】C#线程_基元线程的同步构造

    目录结构: contents structure [+] 简介 为什么需要使用线程同步 线程同步的缺点 基元线程同步 什么是基元线程 基元用户模式构造和内核模式构造的比较 用户模式构造 易变构造(Vo ...

  9. C++ OpenMp的并行编程

    基于OpenMp的并行编程 功能:并行处理比较耗时的for循环 在OpenMP中,对for循环并行化的任务调度使用schedule子句来实现: 使用格式:schedule(type[,size]) t ...

  10. LaTeX中的各种距离设置总结

    LaTeX中的各种距离设置总结   1. 页面设置 A4 会给你一个较小的页面,为了使用更多的控制,可用 geometry宏包  和  命令 \layout . 2. 改变长度 在latex里改变长度 ...