莫队算法,$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. 苹果推送通知服务Push Notification探究总结(序)

    刚才发了两篇几个月前写的文档,觉得太敷衍了,想了想,还是来一发实在的. 再者,刚好上周研究了苹果的推送通知服务Push Notification,还是很有心得的,赶紧趁热打铁,记录一下,望与大家谈论下 ...

  2. WPF实现打印功能

    WPF实现打印功能 在WPF 中可以通过PrintDialog 类方便的实现应用程序打印功能,本文将使用一个简单实例进行演示.首先在VS中编辑一个图形(如下图所示). 将需要打印的内容放入同一个< ...

  3. Morn简介及使用教程

    [Morn UI系列教程]Morn简介及使用教程 网页游戏开发的一大部分工作是在和UI制作上,一个好的工具及框架能使开发事半功倍,Adobe自带flash IDE和Flex各有不足. Morn UI学 ...

  4. Linux epoll总结

    Linux epoll总结 Linux  epoll epoll是Kernel 2.6后新加入的事件机制,在高并发条件下,远优于select.epoll最大的好处在于它不会随着监听fd数目的增长而降低 ...

  5. [分享]CSS美化浏览器滚动条

    今天看到一个站点的滚动条样式特别漂亮,顺便上网搜了一些相关资料,分享给大家: PS:兼容所有浏览器的滚动条样式目前是不存在的. IE下的滚动条样式 IE是最早提供滚动条的样式支持,好多年了,但是其它浏 ...

  6. 最新FFMPEG解码流程

    FFMPEG解码流程: 1. 注册所有容器格式和CODEC:  av_register_all() 2. 打开文件:                    av_open_input_file() 3 ...

  7. HDU 2076 夹角有多大(题目已修改,注意读题)

    Problem Description 时间过的好快,一个学期就这么的过去了,xhd在傻傻的看着表,出于对数据的渴望,突然他想知道这个表的时针和分针的夹角是多少.现在xhd知道的只有时间,请你帮他算出 ...

  8. 使用Bundle在Activity之间交换数据

    一:在main.xml文件中设置布局 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&q ...

  9. TCP中ECN的工作原理分析二(摘自:RFC3168)

    英文源:http://www.icir.org/floyd/ecn.html 发送端和接收端处理: The TCP Sender For a TCP connection using ECN, new ...

  10. 交流希望希望能得到一些大神的指点,加QQ群:249627436 java技术交流群

    查了好多资料,发现还是不全,干脆自己整理吧,至少保证在我的做法正确的,以免误导读者,也是给自己做个记录吧! 本人学习java,1年多,对一些基本技巧已掌握.为了使自己能力晋升建了一个QQ群:java交 ...