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开发】图片拉伸
最近在做一个项目 其中要自己定制一个View 如图: 但是美工给了我的图片尺寸却是不一样的. 分别是599*80 26*61 于是就成了这样的效果. 很明显的发现取消四周不对劲. 于是我就去找美工姐 ...
- 事件聚合IEventAggregator和 Ihandle<T>
-事件聚合IEventAggregator和 Ihandle<T> 今天 说一下Caliburn.Micro的IEventAggregator和IHandle<T>分成两篇 ...
- 使用VS2010命令提示窗口操作程序集强命名
说明:文中示例均以将文件置于D盘根目录(D:\)下为例. 一.查看程序集是否具有强命名 sn -T d:\LicBase.dll 若有则会显示PublicKeyToken值,反之不会. 二.给无强命名 ...
- Leetcode:Unique Binary Search Trees & Unique Binary Search Trees II
Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...
- 圆形头像以及一些常见需求形状自定义ImageView组件
在实际开发中,我们经常会遇到这样的需求,就是无论图片长啥样,我们都要其显示成圆形.圆形加一个边框.矩形加边框,带圆角的矩形等等,lib和demo下载地址:https://github.com/mapl ...
- cocos2d(CCSprite 用贝塞尔做抛物线,足球精灵并且同时做旋转放大效果)
今天刚学到Cocos2d中的动作哪一张,自己做了一个用贝塞尔曲线足球精灵实现同时放大旋转和抛物线动作. 使用 [CCSpawn actions:,,]链接这几个动作,同时做.与CCSequence(一 ...
- Java学习笔记——MySQL的安装使用以及SQL语法简介
在 Java 的开发中,数据库的应用是非常必要的,下面,我们为Java对于数据库的应用做一些必要的准备工作.. Java 对数据库的应用统称为 JDBC. JDBC(Java Data Base Co ...
- 竞价广告系统-逻辑回归优化方法-L-BFGS
逻辑回归优化方法-L-BFGS 逻辑回归的优化方法是一个经典的问题,如果我们把它视为一个最大熵模型,那么我们知道最早的优化方法是IIS,这个方法就不细讲了,因为它速度很慢.后来发现在最优化领域中非常常 ...
- 7z文件格式及其源码的分析
7z文件格式及其源码的分析 本文是一个系列. 主要是分享我最近一年做7z文件开发的经验. 主要包括7z官方源码的结构分析, 以及7z文件格式的分析. 其中涉及到7z源码结构的各个细节, 以及7z文件格 ...
- OpenCV3.0.0+win10 64位+vs2015环境的下载,安装,配置
操作系统:WIN10 pro 64 软件版本:VS2015+OpenCV3.0.0 1. 下载安装 http://opencv.org/ https://www.visualstudio.com/ ...