SPOJ COT2 Count on a tree II (树上莫队)
题目链接:http://www.spoj.com/problems/COT2/
参考博客:http://www.cnblogs.com/xcw0754/p/4763804.html
上面这个人推导部分写的简洁明了,方便理解,但是最后的分情况讨论有些迷,感觉是不必要的,更简洁的思路看下面的博客
传送门:http://blog.csdn.net/kuribohg/article/details/41458639
题意是这样的,给你一棵无根树,给你m个查询,每次查询输出节点x到节点y路径上不同颜色的节点有多少个(包括xy)
没什么说的,直接上代码吧
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
int const MAX_N=+;
int const MAX_M=+;
int n,m,ans,nowLca,lastL,lastR,tempAns;
int color[MAX_N];
vector<int> allColor;
vector<int>::iterator it;
int lin[MAX_N],e_cnt;
struct Query{
int x;
int y;
int id;
int ans;
}query[MAX_M];
struct Edge{
int next;
int y;
}e[*MAX_N];
void insert(int u,int v){
e[++e_cnt].next=lin[u];
e[e_cnt].y=v;
lin[u]=e_cnt;
}
int b_len,b_cnt,bk[MAX_N];
int fa[MAX_N][],deep[MAX_N];
int n_cnt,dfn[MAX_N],top,stack[MAX_N];
bool vis[MAX_N];
void dfs(int u){
dfn[u]=++n_cnt;
int btm=top;
for(int i=lin[u];i;i=e[i].next){
int v=e[i].y;
if(!dfn[v]){
fa[v][]=u;
deep[v]=deep[u]+;
dfs(v);
if(top-btm>=b_len){
b_cnt++;
while(top>btm){
bk[stack[top--]]=b_cnt;
}
}
}
}
stack[++top]=u;
}
bool queryCmp_xkb_ydfn(Query a,Query b){
if(bk[a.x]==bk[b.x])return dfn[a.y]<dfn[b.y];
return bk[a.x]<bk[b.x];
}
bool queryCmp_id(Query a,Query b){
return a.id<b.id;
}
int lca(int u,int v){
if(deep[u]<deep[v])swap(u,v);
for(int i=;~i;i--)
if(deep[fa[u][i]]>=deep[v])
u=fa[u][i];
if(u == v) return u;
for(int i=;~i;i--)
if(fa[u][i]!=fa[v][i])
u=fa[u][i],v=fa[v][i];
return fa[u][];
}
int c_cnt[MAX_N];
void MoveToLca(int u){
for(;u!=nowLca;u=fa[u][]){
if(vis[u]){
vis[u]=false;
c_cnt[color[u]]--;
if(!c_cnt[color[u]])ans--;
}else{
vis[u]=true;
if(!c_cnt[color[u]])ans++;
c_cnt[color[u]]++;
}
}
}
int GetAns(int x,int u,int v){
int tlca=lca(u,v);
if(c_cnt[color[tlca]])return x;
else return x+;
} void FirstMo(){
int L=query[].x,R=query[].y;
nowLca=lca(L,R);
MoveToLca(L);
MoveToLca(R);
query[].ans=GetAns(ans,L,R);
lastL=L;lastR=R;
}
int main()
{
while(~scanf("%d%d",&n,&m)){
allColor.clear();
//read colors
for(int i=;i<=n;i++){
scanf("%d",&color[i]);
allColor.push_back(color[i]);
}
//Discretization the color
sort(allColor.begin(),allColor.end());
it=unique(allColor.begin(),allColor.end());
allColor.resize(distance(allColor.begin(),it));
for(int i=;i<=n;i++){
color[i]=lower_bound(allColor.begin(),allColor.end(),color[i])-allColor.begin()+;
}
//read the tree
memset(lin,,sizeof(lin));
e_cnt=;
for(int i=;i<n;i++){
int u,v;
scanf("%d%d",&u,&v);
insert(u,v);
insert(v,u);
}
//divide in blocks
//prepare for lca: get deep, dfs order, get father
b_len=sqrt((double)n);
b_cnt=;
n_cnt=;
top=;
deep[]=,deep[]=;
memset(fa,,sizeof(fa));
memset(dfn,,sizeof(dfn));
memset(vis,,sizeof(vis));
memset(stack,,sizeof(stack));
dfs();
b_cnt++;
while(top){
bk[stack[top--]]=b_cnt;
}
for(int i = ; (<<i) <= n; i++)
for(int j = ; j <= n; j++)
fa[j][i] = fa[fa[j][i-]][i-];
//read the query
for(int i=;i<=m;i++){
scanf("%d%d",&query[i].x,&query[i].y);
query[i].id=i;
if(dfn[query[i].x]>dfn[query[i].y])
swap(query[i].x,query[i].y);
}
//mo's algorithm
sort(query+,query+m+,queryCmp_xkb_ydfn);
memset(vis,,sizeof(vis));
memset(c_cnt,,sizeof(c_cnt));
lastL=,lastR=,ans=;
for(int i=;i<=m;i++){
int L=query[i].x,R=query[i].y;
nowLca=lca(L,lastL);
MoveToLca(L);
MoveToLca(lastL);
nowLca=lca(R,lastR);
MoveToLca(R);
MoveToLca(lastR);
query[i].ans=GetAns(ans,L,R);
lastL=L;lastR=R;
}
sort(query+,query+m+,queryCmp_id);
for(int i=;i<=m;i++){
printf("%d\n",query[i].ans);
}
}
return ;
}
wa了无数发,最后发现是dfs的时候用vis数组判断点是否访问过,结果忘记初始化vis[1]了。。。。好蠢,后来改成了直接用dfn数组判断是否访问过了,本意是节省资源,结果居然A了。。。人生处处是惊喜。
这里顺便总结下lca写法吧。
这里的是倍增法求LCA
int fa[MAX_N][20],deep[MAX_N];
int dfn[MAX_N];void dfs(int u){
dfn[u]=++n_cnt;
for(int i=lin[u];i;i=e[i].next){
int v=e[i].y;
if(!dfn[v]){
fa[v][0]=u;
deep[v]=deep[u]+1;
dfs(v);
}
}
}
int lca(int u,int v){
if(deep[u]<deep[v])swap(u,v);
for(int i=17;~i;i--)
if(deep[fa[u][i]]>=deep[v])
u=fa[u][i];
if(u == v) return u;
for(int i=17;~i;i--)
if(fa[u][i]!=fa[v][i])
u=fa[u][i],v=fa[v][i];
return fa[u][0];
}int main()
{
n_cnt=0;
deep[0]=0,deep[1]=1;
memset(fa,0,sizeof(fa));
memset(dfn,0,sizeof(dfn));
dfs(1);for(int i = 1; (1<<i) <= n; i++)
for(int j = 1; j <= n; j++)
fa[j][i] = fa[fa[j][i-1]][i-1];
return 0;
}
dfn换成vis也是一样的
首先初始化deep数组表示节点深度,然后是fa[i][j]表示节点i的第2^j的父亲节点是什么,
先dfs求出deep和直系父亲
然后双重循环处理fa数组
lca的时候,先对齐u和v,就是让他们处于同一深度,然后一起向上,直到lca的儿子为止,返回他的父亲,就是lca。
SPOJ COT2 Count on a tree II (树上莫队)的更多相关文章
- spoj COT2 - Count on a tree II 树上莫队
题目链接 http://codeforces.com/blog/entry/43230树上莫队从这里学的, 受益匪浅.. #include <iostream> #include < ...
- SPOJ COT2 Count on a tree II 树上莫队算法
题意: 给出一棵\(n(n \leq 4 \times 10^4)\)个节点的树,每个节点上有个权值,和\(m(m \leq 10^5)\)个询问. 每次询问路径\(u \to v\)上有多少个权值不 ...
- SP10707 COT2 - Count on a tree II (树上莫队)
大概学了下树上莫队, 其实就是在欧拉序上跑莫队, 特判lca即可. #include <iostream> #include <algorithm> #include < ...
- SP10707 COT2 - Count on a tree II [树上莫队学习笔记]
树上莫队就是把莫队搬到树上-利用欧拉序乱搞.. 子树自然是普通莫队轻松解决了 链上的话 只能用树上莫队了吧.. 考虑多种情况 [X=LCA(X,Y)] [Y=LCA(X,Y)] else void d ...
- [SPOJ]Count on a tree II(树上莫队)
树上莫队模板题. 使用欧拉序将树上路径转化为普通区间. 之后莫队维护即可.不要忘记特判LCA #include<iostream> #include<cstdio> #incl ...
- SPOJ COT2 - Count on a tree II(LCA+离散化+树上莫队)
COT2 - Count on a tree II #tree You are given a tree with N nodes. The tree nodes are numbered from ...
- spoj COT2 - Count on a tree II
COT2 - Count on a tree II http://www.spoj.com/problems/COT2/ #tree You are given a tree with N nodes ...
- SPOJ COT2 Count on a tree II (树上莫队,倍增算法求LCA)
题意:给一个树图,每个点的点权(比如颜色编号),m个询问,每个询问是一个区间[a,b],图中两点之间唯一路径上有多少个不同点权(即多少种颜色).n<40000,m<100000. 思路:无 ...
- SPOJ COT2 Count on a tree II(树上莫队)
题目链接:http://www.spoj.com/problems/COT2/ You are given a tree with N nodes.The tree nodes are numbere ...
随机推荐
- All Metro Apps on Windows 8.1 Do Not Work
所有的Metro Apps不能够正常打开,表现为打开后自动最小化到任务栏,并且不能恢复正常状态.在Event Viewer\Application中相应的错误信息为: Activation of ap ...
- JavaScript实现网页换肤
<html> <head> <meta charset="utf-8"> <title>无标题文档</title> &l ...
- [原创]一道基本ACM试题的启示——多个测试用例的输入问题。
Problem Description 输入两点坐标(X1,Y1),(X2,Y2),计算并输出两点间的距离. Input 输入数据有多组,每组占一行,由4个实数组成,分别表示x1,y1,x2,y2,数 ...
- nyoj___大数阶乘
http://acm.nyist.net/JudgeOnline/problem.php?pid=28 大数阶乘 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 我们都知 ...
- PostgreSQL 满足条件时插入数据
例如:当表中不存在某记录时,才插入这条记录. INSERT INTO 表名(列名1, 列名2) SELECT '值1', '值2' WHERE NOT EXISTS ( SELECT * FROM 表 ...
- 手动实现aop编程
手动实现aop编程(运用代理模式实现) aop:aspect object programming 功能:让关注点与业务代码分离 关注点:重复代码就叫做关注点 切面:关注点形成的类,就叫切面(类) 面 ...
- 铁大FaceBook的使用体验
铁大FaceBook是一个类似QQ和微信等聊天程序的缩小版网站,并且其针对领域较为狭窄:即只针对校园的学生和导员等人员.但其有值得推广的潜力性和可能性. 对于使用它的体验:第一点我感觉这个网站的界面很 ...
- iview关于menu结合router问题
#iview关于menu结合router问题 1. Menu.Item下router问题: 直接在Menu标签上绑定on-select事件,可以获取到name(name为元素绑定name) <M ...
- HDU1058 - Humble Numbers
A number whose only prime factors are 2,3,5 or 7 is called a humble number. The sequence 1, 2, 3, 4, ...
- 【XSY2384】【GDOI2017】微信
致去年的我:这是道广义SAM模板题啊…… 题意: Description Input Output HINT $1\leq N\leq 20$,$1\leq Q\leq 10^5$,字符串总长$\le ...