HDU - 4358 Boring counting (dsu on tree)
Boring counting: http://acm.hdu.edu.cn/showproblem.php?pid=4358
题意:
求一棵树上,每个节点的子节点中,同一颜色出现k次 的 个数。
思路:
由于是子树中出现了k次,sum+1。所以增加某种颜色的时候,如果这个颜色+1==k,那么sum++。如果删除的时候这个数+1 == k+1,那么sum--;
#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert>
#include <unordered_map>
using namespace std;
//#pragma GCC optimize(3)
//#pragma comment(linker, "/STACK:102400000,102400000") //c++
// #pragma GCC diagnostic error "-std=c++11"
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize("-fdelete-null-pointer-checks,inline-functions-called-once,-funsafe-loop-optimizations,-fexpensive-optimizations,-foptimize-sibling-calls,-ftree-switch-conversion,-finline-small-functions,inline-small-functions,-frerun-cse-after-loop,-fhoist-adjacent-loads,-findirect-inlining,-freorder-functions,no-stack-protector,-fpartial-inlining,-fsched-interblock,-fcse-follow-jumps,-fcse-skip-blocks,-falign-functions,-fstrict-overflow,-fstrict-aliasing,-fschedule-insns2,-ftree-tail-merge,inline-functions,-fschedule-insns,-freorder-blocks,-fwhole-program,-funroll-loops,-fthread-jumps,-fcrossjumping,-fcaller-saves,-fdevirtualize,-falign-labels,-falign-loops,-falign-jumps,unroll-loops,-fsched-spec,-ffast-math,Ofast,inline,-fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2",3) #define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue
#define max3(a,b,c) max(max(a,b),c) typedef long long ll;
typedef unsigned long long ull; typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行
#define REP(i , j , k) for(int i = j ; i < k ; ++i)
//priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const int mod = 1e9+;
const double esp = 1e-;
const double PI=acos(-1.0); template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} /*-----------------------showtime----------------------*/
const int maxn = 1e5+;
vector<int>g[maxn];
// vector<int>v[maxn];
int col[maxn];
unordered_map<ll,int>cnt;
ll ans[maxn];
bool big[maxn];
int n,k;
int sz[maxn]; void dfs1(int v,int fa){
sz[v] = ;
for(int i=; i<g[v].size(); i++){
int u = g[v][i];
if(u!=fa){
dfs1(u,v);
sz[v]+=sz[u];
}
}
}
ll sum = ;
// priority_queue<int>que;
void add(int v, int fa,int x){
cnt[col[v]] += x;
// if(v==1)cout<<"$$"<<cnt[col[v]]<<endl;
if(cnt[col[v]] == k) sum++;
else if(cnt[col[v]] == k+) sum--; for(int i=;i<g[v].size(); i++){
int u = g[v][i];
if(u!=fa && big[u]==){
add(u,v,x);
}
}
} void dfs(int v,int fa,int keep){
int mx = -,bigChild = -; for(int i=; i<g[v].size(); i++){
int u = g[v][i];
if(u!=fa && sz[u] > mx)
mx = sz[u], bigChild = u;
} for(int i=; i<g[v].size(); i++){
int u = g[v][i];
if(u!=fa && u!=bigChild){
dfs(u,v,);
}
} if(bigChild!=-){
dfs(bigChild,v,),big[bigChild] = ;
} add(v,fa,);
ans[v] = sum; if(bigChild!=-) big[bigChild] = ;
if(keep==){
add(v,fa,-);
sum = ;
} }
void solve(){
sum = ;
read(n),read(k);
for(int i=; i<=n; i++)g[i].clear(),big[i] = ;
cnt.clear();
for(int i=; i<=n; i++)read(col[i]);
for(int i=; i<n; i++){
int u,v;
read(u),read(v);
g[u].pb(v);
g[v].pb(u);
}
dfs1(,);
dfs(,,);
int q;read(q);
while(q--){
int x;read(x);
printf("%lld\n", ans[x]);
}
}
int main(){
int cas;scanf("%d", &cas);
for(int i=; i<=cas;i++){
printf("Case #%d:\n",i);
solve();
if(i<cas)puts("");
}
return ;
}
HDU - 4358
HDU - 4358 Boring counting (dsu on tree)的更多相关文章
- HDU 4358 Boring counting(莫队+DFS序+离散化)
Boring counting Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 98304/98304 K (Java/Others) ...
- hdu 4358 Boring counting dfs序+莫队+离散化
Boring counting Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 98304/98304 K (Java/Others) ...
- HDU - 4358 Boring counting (树上启发式合并/线段树合并)
题目链接 题意:统计树上每个结点中恰好出现了k次的颜色数. dsu on tree/线段树合并裸题. 启发式合并1:(748ms) #include<bits/stdc++.h> usin ...
- hdu 4358 Boring counting 离散化+dfs序+莫队算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4358 题意:以1为根节点含有N(N <= 1e5)个结点的树,每个节点有一个权值(weight ...
- HDU 4358 Boring counting 树状数组+思路
研究了整整一天orz……直接上官方题解神思路 #include <cstdio> #include <cstring> #include <cstdlib> #in ...
- HDU 4358 Boring counting dfs序+莫队算法
题意:N个节点的有根树,每个节点有一个weight.有Q个查询,问在以u为根的子树中,有恰好出现了K次的weight有多少种. 这是第一次写莫队算法,之前也只是偶有耳闻. 看了别人的代码打的,还是贴上 ...
- 后缀数组 --- HDU 3518 Boring counting
Boring counting Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=3518 Mean: 给你一个字符串,求:至少出 ...
- HDU 3518 Boring counting
题目:Boring counting 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3518 题意:给一个字符串,问有多少子串出现过两次以上,重叠不能算两次 ...
- 【HDOJ】4358 Boring counting
基本思路是将树形结构转线性结构,因为查询的是从任意结点到叶子结点的路径.从而将每个查询转换成区间,表示从该结点到叶子结点的路径.离线做,按照右边界升序排序.利用树状数组区间修改.树状数组表示有K个数据 ...
随机推荐
- Docker 容器基本操作[Docker 系列-2]
Docker 入门及安装[Docker 系列-1] 镜像就像是一个安装程序,而容器则是程序运行时的一个状态. 查看容器 查看容器 启动 docker 后,使用 docker ps 命令可以查看当前正 ...
- Android通过辅助功能实现抢微信红包原理简单介绍
简书文章:https://www.jianshu.com/p/e1099a94b979 附抢红包开源项目地址,代码已全改为Kotlin了,已适配到最新微信7.0.5版本,如果对你有所帮助赏个star吧 ...
- web渗透---第一天
了解黑客 黑客: 黑客是一个中文词语,皆源自英文hacker,随着灰鸽子的出现, 灰鸽子成为了很多假借黑客名义控制他人电脑的黑客技术,于是 出现了“骇客”与黑客”分家. 黑客:Hacker 骇 ...
- Dubbo里面线程池的拒绝策略
Dubbo里面线程池的拒绝策略 public class AbortPolicyWithReport extends ThreadPoolExecutor.AbortPolicy { protecte ...
- Java equal() 和 == 详细分析
1 == 返回值是true/false; (1) 基本数据类型比较的就是值(2)引用型数据类型就是地址值 public class Test1 { public static void main(S ...
- python基础之变量与数据类型
变量在python中变量可以理解为在计算机内存中命名的一个存储空间,可以存储任意类型的数据.变量命名变量名可以使用英文.数字和_命名,且不能用数字开头使用赋值运算符等号“=”用来给变量赋值.变量赋值等 ...
- Unbutu在VMWare中挂载共享文件夹
第一,安装VMTools,步骤自行搜索,安装成功后重启虚拟机. 第二,重启后,在虚拟机管理页面设置共享目录,选择总是启用,开启虚拟机. 第三,在终端进入挂载目录cd /mnt/hgfs/,通过命令su ...
- Unix-IO-同步,异步,阻塞,非阻塞-笔记篇
概念更正 https://www.zhihu.com/question/19732473 错误的四个象限分类 https://www.ibm.com/developerworks/cn/linux/l ...
- vue过滤器微信小程序过滤器和百度智能小程序过滤器
因为最近写了微信小程序和百度小程序,用到了过滤器,感觉还挺好用的,所以就来总结一下,希望能帮到你们. 1. 微信小程序过滤器: 1.1:首先建一个单独的wxs后缀的文件,一般放在utils文件夹里面. ...
- [GO语言的并发之道] Goroutine调度原理&Channel详解
并发(并行),一直以来都是一个编程语言里的核心主题之一,也是被开发者关注最多的话题:Go语言作为一个出道以来就自带 『高并发』光环的富二代编程语言,它的并发(并行)编程肯定是值得开发者去探究的,而Go ...