HDU4358 Boring counting【dsu on tree】
Boring counting
Problem Description
In this problem we consider a rooted tree with N vertices. The vertices are numbered from 1 to N, and vertex 1 represents the root. There are integer weights on each vectice. Your task is to answer a list of queries, for each query, please tell us among all the vertices in the subtree rooted at vertice u, how many different kinds of weights appear exactly K times?
给出一根为\(1\)的树,每个点都有权值,每次询问以\(u\)为根的子树中正好出现\(k\)次的权值有几个
权值可以离散化,离散化之后范围在\([1,n]\),然后通过树上启发式合并可以得到所有点的答案,对于每个询问查询答案即可
具体方法就是维护每个权值出现的次数,和每个权值出现的次数的次数
//#pragma GCC optimize("O3")
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<bits/stdc++.h>
using namespace std;
function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
const int MAXN = 1e5+7;
int n,k,w[MAXN],ret[MAXN],sz[MAXN],son[MAXN],cnt[MAXN],app[MAXN];
vector<int> G[MAXN];
void dfs(int u, int par){
son[u] = 0; sz[u] = 1;
for(int v : G[u]){
if(v==par) continue;
dfs(v,u);
sz[u] += sz[v];
if(sz[son[u]]<sz[v]) son[u] = v;
}
}
void inc(int x){
cnt[x]++;
app[cnt[x]]++;
app[cnt[x]-1]--;
}
void dec(int x){
cnt[x]--;
app[cnt[x]]++;
app[cnt[x]+1]--;
}
void update(int u, int par, bool add){
if(add) inc(w[u]);
else dec(w[u]);
for(int v : G[u]) if(v!=par) update(v,u,add);
}
void search(int u, int par, bool clear){
for(int v : G[u]) if(v!=par and v!=son[u]) search(v,u,true);
if(son[u]) search(son[u],u,false);
for(int v : G[u]) if(v!=par and v!=son[u]) update(v,u,true);
inc(w[u]);
ret[u] = app[k];
if(clear) update(u,par,false);
}
void solve(int kase){
if(kase!=1) puts("");
scanf("%d %d",&n,&k);
for(int i = 1; i <= n; i++) G[i].clear();
vector<int> vec;
for(int i = 1; i <= n; i++){
scanf("%d",&w[i]);
vec.emplace_back(w[i]);
}
sort(vec.begin(),vec.end());
vec.erase(unique(vec.begin(),vec.end()),vec.end());
for(int i = 1; i <= n; i++) w[i] = lower_bound(vec.begin(),vec.end(),w[i]) - vec.begin() + 1;
for(int i = 1; i < n; i++){
int u, v; scanf("%d %d",&u,&v);
G[u].emplace_back(v); G[v].emplace_back(u);
}
dfs(1,0);
search(1,0,true);
printf("Case #%d:\n",kase);
int q; scanf("%d",&q);
while(q--){
int x; scanf("%d",&x);
printf("%d\n",ret[x]);
}
}
int main(){
int T; scanf("%d",&T);
for(int kase = 1; kase <= T; kase++) solve(kase);
return 0;
}
HDU4358 Boring counting【dsu on tree】的更多相关文章
- 【DSU on tree】【CF741D】Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths
Description 给定一棵 \(n\) 个节点的树,每条边上有一个字符,字符集大小 \(22\),求每个节点的子树内最长的简单路径使得路径上的字符经过重排后构成回文串. Limitation \ ...
- HDU6504 Problem E. Split The Tree【dsu on tree】
Problem E. Split The Tree Problem Description You are given a tree with n vertices, numbered from 1 ...
- HDU6430 Problem E. TeaTree【dsu on tree】
Problem E. TeaTree Problem Description Recently, TeaTree acquire new knoledge gcd (Greatest Common D ...
- CF 375D. Tree and Queries【莫队 | dsu on tree】
题意: 一棵树,询问一个子树内出现次数$≥k$的颜色有几种 强制在线见上一道 用莫队不知道比分块高到哪里去了,超好写不用调7倍速度!!! 可以用分块维护出现次数这个权值,实现$O(1)-O(\sqrt ...
- HDU6191 Query on A Tre【dsu on tree + 01字典树】
Query on A Tree Problem Description Monkey A lives on a tree, he always plays on this tree. One day, ...
- 【Invert Binary Tree】cpp
题目: Invert Binary Tree Total Accepted: 20346 Total Submissions: 57084My Submissions Question Solutio ...
- 【Balanced Binary Tree】cpp
题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...
- Openjudge1388 Lake Counting【DFS/Flood Fill】
http://blog.csdn.net/c20182030/article/details/52327948 1388:Lake Counting 总时间限制: 1000ms 内存限制: ...
- 【遍历二叉树】10判断二叉树是否平衡【Balanced Binary Tree】
平衡的二叉树的定义都是递归的定义,所以,用递归来解决问题,还是挺容易的额. 本质上是递归的遍历二叉树. ++++++++++++++++++++++++++++++++++++++++++++++++ ...
随机推荐
- js 鼠标点击页面出现文字
<script type="text/javascript"> var a_idx = 0; jQuery(document).ready(function($) { ...
- new ArrayList(0) 和 new ArrayList() 和一样吗?
第一感觉是一样的,盲猜后者调用了前者,并传入参数 0.然而,无论是 JDK 7 还是 JDK 8,这两个方法构造的结果都是不一样的.JDK 开发人员在这方面作了优化. JDK 7 在 Java 7 中 ...
- 【MyBatis】MyBatis 延迟加载策略
MyBatis 延迟加载策略 文章源码 什么是延迟加载 延迟加载,就是在需要用到数据时才进行加载,不需要用到数据时就不加载数据,也被成为懒加载. 好处:先从单表查询,需要时再从关联表去关联查询,大大提 ...
- #2020征文-开发板# 用鸿蒙开发AI应用(五)HDF 驱动补光灯
目录: 前言 硬件准备 HDF 驱动开发 总结 前言上一篇,我们在鸿蒙上运行了第一个程序,这一篇我们来编写一个驱动开启摄像头的红外补光灯,顺便熟悉一下鸿蒙上的 HDF 驱动开发. 硬件准备先查一下原理 ...
- /var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh: line 19: mysql: command not found
[root@test ~]# tail -f /tmp/zabbix_agentd.log /var/lib/zabbix/percona/scripts/get_mysql_stats_wrappe ...
- 【Linux】CentOS8 初体验
一.部署CentOS8虚拟机 1.下载Centos8镜像 下载地址: https://www.centos.org/download/ 可以选择国内的下载源,比较快,这里推荐清华的和阿里的 2.下载完 ...
- 【Linux】Linux进程间通信的几种方式
一.进程间通信的目的 数据传输:一个进程需要将它的数据发送给另一个进程,发送的数据量在一个字节到几M字节之间 共享数据:多个进程要操作共享数据,一个进程对共享数据 信息传递:一个进程需要向另一个进程发 ...
- PHP反序列化 - Pikachu
概述 序列化serialize()序列化说通俗点就是把一个对象变成可以传输的字符串,比如下面是一个对象: class S{ public $test="pikachu"; } $s ...
- Databricks 第7篇:管理Secret
有时,访问数据要求您通过JDBC对外部数据源进行身份验证,可以使用Azure Databricks Secret来存储凭据,并在notebook和job中引用它们,而不是直接在notebook中输入凭 ...
- scrapy异步的爬虫框架简单的使用
scrapy异步的爬虫框架 异步的爬虫框架 高性能的数据解析,持久化存储,全栈数据的爬取,中间件,分布式 框架:就是一个集成好了各种功能且具有很强通用性的一个项目模板. 环境安装: Linux: pi ...