SPOJ:Ada and Orange Tree (LCA+Bitset)
Ada the Ladybug lives near an orange tree. Instead of reading books, she investigates the oranges. The oranges on orange tree can be in up to 5*50 Shades of Orange. She walks from orange to orange, examining different properties of orange tree. The oranges are connected by branches. There is more oranges then branches, yet it is still possible to get from any orange to any other orange [through branches]. The tree is rooted.
Ada has many questions in following form: She goes from orange A to orange B (by shortest path) and is interested in total number different Shades of Orange among all subtrees of all edges on shortest path.
Input
The first line of input consists of 1 ≤ T ≤ 100, the number of test-cases.
The first line of each test case contains three integers 1 ≤ N, Q ≤ 4.5*105, 0 ≤ R < N, the number of oranges, the number of questions and number of root.
Next line contains N integers 1 ≤ Si ≤ 250, the shade of orange of orange i.
Next N-1 lines contains two integers 0 ≤ I, J < N, I ≠ J , the numbers of oranges which are connected by branch.
Next Q lines contains two integers 0 ≤ A, B < N, the path Ada is interested about.
The sum of all N and all Q among all test-cases won't exceed 106
Output
For each question answer the number of shades in all subtrees of all nodes on shortest path from A to B.
Example Input
1
10 7 1
1 2 1 4 5 6 6 8 9 9
0 9
9 3
3 4
4 6
4 5
4 8
1 3
1 2
2 7
4 4
8 6
0 6
7 0
7 2
0 0
2 3
Example Output
3
3
5
7
2
1
7
题意:给定一棵树,每个节点有一种颜色的橘子;Q次询问,每次询问,给出u、v,回答u到v的最短路径上的节点的子树一共有多少种颜色的橘子。
思路:其实就是问最小公共祖先LCA的子树的颜色种类。因为颜色只有250种,我们DFS时就用Bitset记录子树的颜色种类数。
#include<bitset>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
int Laxt[maxn],Next[maxn<<],To[maxn<<];
int fa[maxn][],dep[maxn],N,Q,rt,cnt;
bitset<>S[maxn];
void init()
{
for(int i=;i<=N;i++) S[i].reset();
for(int i=;i<=N;i++) Laxt[i]=;
memset(Laxt,,sizeof(Laxt));
cnt=;
}
void add(int u,int v){
Next[++cnt]=Laxt[u];
Laxt[u]=cnt;
To[cnt]=v;
}
void dfs(int u,int f){
fa[u][]=f; dep[u]=dep[f]+;
for(int i=Laxt[u];i;i=Next[i]){
if(To[i]!=f){
dfs(To[i],u);
S[u]|=S[To[i]];
}
}
}
void RMQ()
{
for(int i=;i<;i++)
for(int j=;j<=N;j++)
fa[j][i]=fa[fa[j][i-]][i-];
}
int LCA(int u,int v){
if(dep[u]<dep[v]) swap(u,v);
for(int i=;i>=;i--)
if(dep[fa[u][i]]>=dep[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 main()
{
int T,x,u,v,lca;
scanf("%d",&T);
while(T--){
scanf("%d%d%d",&N,&Q,&rt);
rt++; init();
for(int i=;i<=N;i++){
scanf("%d",&x);
S[i][x]=;
}
for(int i=;i<N;i++){
scanf("%d%d",&u,&v);
u++; v++;
add(u,v); add(v,u);
}
dfs(rt,); RMQ();
while(Q--){
scanf("%d%d",&u,&v);
u++; v++;
lca=LCA(u,v);
printf("%d\n",S[lca].count());
}
}
return ;
}
SPOJ:Ada and Orange Tree (LCA+Bitset)的更多相关文章
- BZOJ 2588: Spoj 10628. Count on a tree( LCA + 主席树 )
Orz..跑得还挺快的#10 自从会树链剖分后LCA就没写过倍增了... 这道题用可持久化线段树..点x的线段树表示ROOT到x的这条路径上的权值线段树 ----------------------- ...
- Bzoj 2588: Spoj 10628. Count on a tree 主席树,离散化,可持久,倍增LCA
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2588 2588: Spoj 10628. Count on a tree Time Limit ...
- 【BZOJ2588】Spoj 10628. Count on a tree 主席树+LCA
[BZOJ2588]Spoj 10628. Count on a tree Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lasta ...
- Bzoj 2588 Spoj 10628. Count on a tree(树链剖分LCA+主席树)
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MB Description 给定一棵N个节点的树,每个点 ...
- 主席树 || 可持久化线段树 || LCA || BZOJ 2588: Spoj 10628. Count on a tree || Luogu P2633 Count on a tree
题面: Count on a tree 题解: 主席树维护每个节点到根节点的权值出现次数,大体和主席树典型做法差不多,对于询问(X,Y),答案要计算ans(X)+ans(Y)-ans(LCA(X,Y) ...
- BZOJ 2588: Spoj 10628. Count on a tree 树上跑主席树
2588: Spoj 10628. Count on a tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/J ...
- BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 5217 Solved: 1233 ...
- 2588: Spoj 10628. Count on a tree
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 5766 Solved: 1374 ...
- bzoj 2588 Spoj 10628. Count on a tree (可持久化线段树)
Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 7669 Solved: 1894[Submi ...
随机推荐
- nginx源码学习资源(不断更新)转
原文地址:http://www.cnblogs.com/yjf512/archive/2012/06/13/2548515.html nginx源码学习是一个痛苦又快乐的过程,下面列出了一些nginx ...
- ELK之收集Nginx、Tomcat的json格式日志
1.安装Nginx yum -y install nginx vim /etc/nginx/nginx.conf # 修改日志格式为json格式,并创建一个nginxweb的网站目录 log_form ...
- Codeforces 959 E Mahmoud and Ehab and the xor-MST
Discription Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him ...
- How do I get an image from an UIButton? 如何获取uibutton设置的uiimage
UIImage*img =[button imageForState:UIControlStateNormal];
- gdb源码安装过程中的问题:no termcap library found
gdb使用源码安装的时候遇到错误:no termcap library found ./configure --> make --> make install 解决办法,下载termca ...
- DICOM医学图像显示算法改进与实现——LUT
引言 随着Ul(超声成像).CT(计算机断层成像).MRI(核磁共振成像).CR(计算机X线成像).电子内窥镜.盯(正电子发射断层成像)和MI(分子影像)等医学影像设备不断涌现,利用计算机对医学影像设 ...
- 【Todo】【读书笔记】Career Cup 150笔记
下载了第五版:/Users/baidu/Documents/Data/Interview/算法与数据结构/<CareerCup+Top+150+Questions+5th.pdf> 参考这 ...
- NBUT 1457 Sona (莫队算法)
题目大意: 求一段区间内 出现的数字的次数的三次方的和 思路分析: 这要水过去的题目真是难,各种优化. 不能用map , 要离散化之后 先处理lowerbound. 优化输入. . . 时间卡的非常紧 ...
- SQL基础-->层次化查询(START BY ... CONNECT BY PRIOR)
--====================================================== --SQL基础-->层次化查询(START BY ... CONNECT BY ...
- SQL数据库 更改数据类型
向表中添加数据 alter table 表名 add 列名 类型 更改表中列的数据类型 alter table 表名 alter column 列名 类型 删除表中的指定列 alter table 表 ...