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 ...
随机推荐
- android 服务
1.创建服务 Exported:是否允许除了当前程序之外的其他程序访问这个服务 Enable:是否启用这个服务 点击完成后自动生成 import android.app.Service; import ...
- BZOJ 4810 [Ynoi2017]由乃的玉米田 (莫队 + bitset)
题目链接 BZOJ 4810 首先对询问离线, 莫队算法处理. 首先我们可以用bitset维护处当前区间中是否存在某个数. 对于询问1, 我们可以用 ((f >> q[i].x) &am ...
- 迁移桌面程序到MS Store(8)——通过APPX下载Win32Component
在上一篇<迁移桌面程序到MS Store(7)——APPX + Service>中,我们提到将desktop application拆分成UI Client+Service两部分.其中UI ...
- Callable和Runnable和FutureTask
http://www.cnblogs.com/dolphin0520/p/3949310.html 一.Callable与Runnable 二.Future 三.FutureTask 四.使用示例 一 ...
- BZOJ1017魔兽地图DotR 樹形DP
@(BZOJ)[樹形DP, 三維DP] Description DotR (Defense of the Robots) Allstars是一个风靡全球的魔兽地图,他的规则简单与同样流行的地图DotA ...
- java.nio.ByteBuffer 以及flip,clear及rewind区别
Buffer 类 定义了一个可以线性存放primitive type数据的容器接口.Buffer主要包含了与类型(byte, char…)无关的功能. 值得注意的是Buffer及其子类都不是线程安全的 ...
- foobar2000设置关闭按钮最小化到系统托盘
1.设置托盘选项: 2.[File]->[Preferences]->[Advanced]->[Display]->[Default User Interface]->[ ...
- keras函数式编程(多任务学习,共享网络层)
https://keras.io/zh/ https://keras.io/zh/getting-started/functional-api-guide/ https://github.com/ke ...
- poj1159--Palindrome(dp:最长公共子序列变形 + 滚动数组)
Palindrome Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 53414 Accepted: 18449 Desc ...
- 高速清除winXP系统中explorer.exe病毒
关于这个explorer.exe病毒.是眼下xp最为常见的一个病毒,会大量的消耗系统资源,造成电脑特别的卡顿. 1.关闭还原(假设没有,则跳过),为的是防止我们改动后,还原之后又回来了. 2.打开注冊 ...