莫队算法,$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的更多相关文章

  1. CSU 1811: Tree Intersection(线段树启发式合并||map启发式合并)

    http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1811 题意:给出一棵树,每一个结点有一个颜色,然后依次删除树边,问每次删除树边之后,分开的两个 ...

  2. 【树状数组】CSU 1811 Tree Intersection (2016湖南省第十二届大学生计算机程序设计竞赛)

    题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1811 题目大意: 一棵树,N(2<=N<=105)个节点,每个节点有一种颜 ...

  3. csu oj 1811: Tree Intersection (启发式合并)

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1811 给你一棵树,每个节点有一个颜色.问删除一条边形成两棵子树,两棵子树有多少种颜色是有 ...

  4. 启发式合并CSU - 1811

    F - Tree Intersection CSU - 1811 Bobo has a tree with n vertices numbered by 1,2,…,n and (n-1) edges ...

  5. [leetcode_easy]558. Quad Tree Intersection

    problem 558. Quad Tree Intersection re 1. Leetcode_easy_558. Quad Tree Intersection; 2. Grandyang; e ...

  6. 2016湖南省赛 I Tree Intersection(线段树合并,树链剖分)

    2016湖南省赛 I Tree Intersection(线段树合并,树链剖分) 传送门:https://ac.nowcoder.com/acm/contest/1112/I 题意: 给你一个n个结点 ...

  7. CSU 1663: Tree(树链剖分)

    1663: Tree Time Limit: 5 Sec  Memory Limit: 128 MB Submit: 26  Solved: 11 [Submit][id=1663"> ...

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

  9. [LeetCode] Quad Tree Intersection 四叉树相交

    A quadtree is a tree data in which each internal node has exactly four children: topLeft, topRight,  ...

随机推荐

  1. C#通过接口与线程通信(捕获线程状态)介绍

    C#通过接口与线程通信(捕获线程状态)介绍 摘要:本文介绍C#通过接口与线程通信(捕获线程状态),并提供简单的示例代码供参考. 提示:本文所提到的线程状态变化,并不是指线程启动.暂停.停止,而是说线程 ...

  2. Xps实现文档显示、套打功能

    wpf采用Xps实现文档显示.套打功能(原创) 近期的一个项目需对数据进行套打,用户要求现场不允许安装office.页面预览显示必须要与文档完全一致,xps文档来对数据进行处理.Wpf的Documen ...

  3. GitHub删除文件

    删除单个文件 一般情况下,通常可直接在文件管理器中直接把文件删了,或者用rm命令删了 $ rm README.txt 这个时候,工作区是删除了,可版本库还没有.git status命令会立刻告诉你哪些 ...

  4. UIButton根据不同的状态设置layer

    这需要用到KVO,监听button的highlighted属性的变化,在监听回调里根据监听到得属性值设置layer 设置监听如下 [button  addObserver:self forKeyPat ...

  5. Aliexpress API 授权流程整理

    Aliexpress API 授权流程整理   前言 我零零总总用了好几个月的时间,写了一个自用的小程序,从 Aliexpress 上抓取订单的小程序.刚开始写的时候,该API还没有开放,而且没有订单 ...

  6. Go Revel - Parameters(参数绑定)

    Go Revel - Parameters(参数绑定) 参数绑定 Revel框架会尽可能的将提交参数转换为期望的Go类型.这个从一个字符串提交参数转换为另一个类型被称为数据绑定 . 参数 所有的请求参 ...

  7. wp加载本地HTML(附带图片,CSS,JS)

    wp加载本地HTML(附带图片,CSS,JS) Windows Phone:Load Local HTML with Img,Css,Js by 唐小崇 http://www.cnblogs.com/ ...

  8. 通过qsort(void * lineptr[], int left, int rifht, int (*comp)(void *, void *))解读指针函数和void指针

    原函数是<The C programint  language >5.11文本行排序的程序,如下: void qsort(void *v[], int left, int right, i ...

  9. linux内核skb操作

    1,struct sk_buff数据结构 struct sk_buff{ //这两个结构必须放在最前面 struct sk_buff *next; struct sk_buff *prev; stru ...

  10. classnull100 - The 3n + 1 problem

    新手发帖,很多方面都是刚入门,有错误的地方请大家见谅,欢迎批评指正  The 3n + 1 problem  Background Problems in Computer Science are o ...