CSU 1811 Tree Intersection
莫队算法,$dfs$序。
题目要求计算将每一条边删除之后分成的两棵树的颜色的交集中元素个数。
例如删除$u->v$,我们只需知道以$v$为$root$的子树中有多少种不同的颜色(记为$qq$),有多少种颜色达到了最多数量(记为$pp$),那么以$v$为$root$的子树与另一棵树的颜色交集中元素个数为$qq-pp$。
因为是计算子树上的量,所以可以将树转换成序列,每一个子树对应了序列中一段区间,具体计算只要用莫队算法分块就可以无脑的计算了。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
char c = getchar(); x = ;while(!isdigit(c)) c = getchar();
while(isdigit(c)) { x = x * + c - ''; c = getchar(); }
} const int maxn=;
struct Edge { int u,v,nx; bool f; }e[*maxn];
int h[maxn],sz,w[maxn],cnt[maxn],pos[maxn],g[maxn];
int n,a[*maxn],L[*maxn],R[*maxn];
struct Q { int L,R,id; }q[maxn];
int Ans[maxn],pp,qq; void add(int u,int v)
{
e[sz].u=u; e[sz].v=v; e[sz].nx=h[u];
e[sz].f=; h[u]=sz++;
} void dfs(int x,int fa)
{
sz++; a[sz]=w[x]; L[x]=sz;
for(int i=h[x];i!=-;i=e[i].nx)
{
if(e[i].v==fa) continue;
e[i].f=; dfs(e[i].v,x);
}
sz++; a[sz]=w[x]; R[x]=sz;
} bool cmp(Q a,Q b) { if (pos[a.L]==pos[b.L]) return a.R<b.R; return a.L<b.L; }
void op1(int x) { if(cnt[a[x]]-g[a[x]]==) pp--; if(g[a[x]]==) qq--; }
void op2(int x) { if(g[a[x]]==cnt[a[x]]) pp++; if(g[a[x]]==) qq++; } int main()
{
while(~scanf("%d",&n))
{
memset(cnt,sz=,sizeof cnt);
memset(h,-,sizeof h);
memset(g,,sizeof g);
memset(Ans,,sizeof Ans); for(int i=;i<=n;i++) { scanf("%d",&w[i]); cnt[w[i]]++; }
for(int i=;i<=n;i++) cnt[i]=*cnt[i]; for(int i=;i<n;i++)
{
int u,v; scanf("%d%d",&u,&v);
add(u,v); add(v,u);
}
sz=; dfs(,-); for(int i=;i<*(n-);i++)
{
if(e[i].f==) continue;
q[i/].L=L[e[i].v]; q[i/].R=R[e[i].v];
q[i/].id=i/;
} sz=sqrt(*n); for(int i=;i<=*n;i++) pos[i]=i/sz;
sort(q,q+(n-),cmp); pp=,qq=;
for(int i=q[].L;i<=q[].R;i++) { g[a[i]]++; op2(i); }
Ans[q[].id]=qq-pp; int L=q[].L,R=q[].R;
for(int i=;i<n-;i++)
{
while(L<q[i].L) { g[a[L]]--; op1(L); L++; }
while(L>q[i].L) { L--; g[a[L]]++; op2(L); }
while(R>q[i].R) { g[a[R]]--; op1(R); R--; }
while(R<q[i].R) { R++; g[a[R]]++; op2(R); }
Ans[q[i].id]=qq-pp;
}
for(int i=;i<n-;i++) printf("%d\n",Ans[i]);
}
return ;
}
CSU 1811 Tree Intersection的更多相关文章
- CSU 1811: Tree Intersection(线段树启发式合并||map启发式合并)
		
http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1811 题意:给出一棵树,每一个结点有一个颜色,然后依次删除树边,问每次删除树边之后,分开的两个 ...
 - 【树状数组】CSU 1811 Tree Intersection (2016湖南省第十二届大学生计算机程序设计竞赛)
		
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1811 题目大意: 一棵树,N(2<=N<=105)个节点,每个节点有一种颜 ...
 - csu oj 1811: Tree Intersection (启发式合并)
		
题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1811 给你一棵树,每个节点有一个颜色.问删除一条边形成两棵子树,两棵子树有多少种颜色是有 ...
 - 启发式合并CSU - 1811
		
F - Tree Intersection CSU - 1811 Bobo has a tree with n vertices numbered by 1,2,…,n and (n-1) edges ...
 - [leetcode_easy]558. Quad Tree Intersection
		
problem 558. Quad Tree Intersection re 1. Leetcode_easy_558. Quad Tree Intersection; 2. Grandyang; e ...
 - 2016湖南省赛 I Tree Intersection(线段树合并,树链剖分)
		
2016湖南省赛 I Tree Intersection(线段树合并,树链剖分) 传送门:https://ac.nowcoder.com/acm/contest/1112/I 题意: 给你一个n个结点 ...
 - CSU 1663: Tree(树链剖分)
		
1663: Tree Time Limit: 5 Sec Memory Limit: 128 MB Submit: 26 Solved: 11 [Submit][id=1663"> ...
 - CSUOJ1811  Tree Intersection (启发式合并)
		
Bobo has a tree with n vertices numbered by 1,2,…,n and (n-1) edges. The i-th vertex has color c i, ...
 - [LeetCode] Quad Tree Intersection 四叉树相交
		
A quadtree is a tree data in which each internal node has exactly four children: topLeft, topRight, ...
 
随机推荐
- 【ios开发】ios开发问题集锦
			
1. ARC forbids explicit message send of'release' 'release' is unavailable: not available inautomatic ...
 - go语言defer使用
			
defer Go语言中有种不错的设计,即延迟(defer)语句,你可以在函数中添加多个defer语句.当函数执行到最后时,这些defer语句会按照逆序执行,最后该函数返回.特别是当你在进行一些打开资源 ...
 - ESB与SOA的关系
			
ESB与SOA的关系 一.SOA和ESB一直是没有明确概念的两个缩略词 SOA----面向服务架构,实际上强调的是软件的一种架构,一种支撑软件运行的相对稳定的结构,表面含义如此,其实SOA是一种通过服 ...
 - socket网络编程快速上手(二)——细节问题(3)
			
3.SIGPIPE问题 人怕牺牲,我们写的程序也一样,人有死不瞑目,程序又何尝不是?程序跑着跑着,突然就崩掉了.好一点的牺牲前告诉你些打印,差点的也能用core文件等一些手段查出死在哪了,最惨不忍睹的 ...
 - web form中自定义HttpHandler仿mvc
			
web form中自定义HttpHandler仿mvc 前言 在mvc大行其道的今天,仍然有不少公司的项目还是使用web form来实现的(其实mvc也是基于web form的),如果要在项目中引入m ...
 - wxWidgets的安装编译、相关配置、问题分析处理
			
wxWidgets的安装编译.相关配置.问题分析处理 一.介绍部分 (win7 下的 GUI 效果图见 本篇文章的最后部分截图2张) wxWidgets是一个开源的跨平台的C++构架库(framewo ...
 - 批处理安装nodejs
			
@echo offcd nodejsecho 正在安装nodejs-v4.2.2,请稍等...start /wait node-v4.2.2-x64.msi /qnecho 安装nodejs结束 cd ...
 - SystemTray文字颜色问题
			
今天想给SystemTray的ForegroundColor设置为白色,但是模拟器正确,真机仍为黑色.经过一番折腾,发现是微软做了限制,背景是什么颜色,ForegroundColor就不能为什么颜色. ...
 - Nhibernate1
			
Nhibernate随手记(1) 学习Nhibernate的萌芽 今早有群里有人问Nhibernate的问题,没学过,刚好来了兴趣,无意很快在园子里下载到了一本Nhibernate3.0的电子书,内容 ...
 - PB导出规定格式DBF文件
			
最近在做一个给卫计委做数据上报的数据接口,接口要求使用奇葩的dBase 3数据库存储上报数据,忙活了几天总算搞好了,使用开发工具为powerbuild 12,222个字段的上报数据表生成DBF文件,写 ...