首页 技术 正文
技术 2022年11月15日
0 收藏 924 点赞 3,260 浏览 2060 个字

莫队算法,$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 ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,038
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,524
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,372
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,152
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,785
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,867