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 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)的更多相关文章

  1. BZOJ 2588: Spoj 10628. Count on a tree( LCA + 主席树 )

    Orz..跑得还挺快的#10 自从会树链剖分后LCA就没写过倍增了... 这道题用可持久化线段树..点x的线段树表示ROOT到x的这条路径上的权值线段树 ----------------------- ...

  2. 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 ...

  3. 【BZOJ2588】Spoj 10628. Count on a tree 主席树+LCA

    [BZOJ2588]Spoj 10628. Count on a tree Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lasta ...

  4. 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个节点的树,每个点 ...

  5. 主席树 || 可持久化线段树 || 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) ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 图片裁剪上传插件——jquery.photoClip.js

    想要裁剪图片上传: 需要依赖的的插件为: [jquery.photoClip.js] 插件[iscroll-zoom.js] 插件[hammer.js] 插件 [lrz.all.bundle.js] ...

  2. curl抓取数据

    抓取数据的代码: $url='抓取数据的网站路径'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); //参数为1表示传输数据,为0表示 ...

  3. Educational Codeforces Round 51 (Rated for Div. 2) The Shortest Statement

    题目链接:The Shortest Statement 今天又在群里看到一个同学问$n$个$n$条边,怎么查询两点直接最短路.看来这种题还挺常见的. 为什么最终答案要从42个点的最短路(到$x,y$) ...

  4. 更改bootstrap的默认样式

    很久没用bootstrap,对与按自己的需求修改样式都忘了. 一上来就添加了新的class类,重写css样式让其覆盖原有的样式,实际上不起作用.因为没考虑的选择器的优先级.面试的时候会问到一些这个问题 ...

  5. stm32f103和s3c2440配置

    stm32                                  s3c2440 外部晶振:  8MHz                                   12MHz

  6. Struts2 与SpringMVC之比较

    1.Struts2是类级别的拦截, 一个类对应一个request上下文,SpringMVC是方法级别的拦截,一个方法对应一个request上下文,而方法同时又跟一个url对应,所以说从架构本身上Spr ...

  7. Adding an Exception Breakpoint - Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 25 bey

    用如下的方法可以非常方便停留到具体crash的某行代码 Adding an Exception Breakpoint Add an exception breakpoint to your proje ...

  8. 如何在ASP.NET Core自定义中间件中读取Request.Body和Response.Body的内容?

    原文:如何在ASP.NET Core自定义中间件中读取Request.Body和Response.Body的内容? 文章名称: 如何在ASP.NET Core自定义中间件读取Request.Body和 ...

  9. Android 集成支付宝支付详解

    一说到支付宝,相信没有人不知道,生活中付款,转账都会用到. 今天来详细介绍下在Android中如何集成支付宝支付到自己的APP中去.让APP能够拥有方便,快捷的支付功能. 准备工作: 商户在b.ali ...

  10. Donser Online Judge 完成运行使命~

    复试成功完成~ 2018年网研机考难度不大,仍然有些遗憾,前两题水题后两个题纯暴力 排行榜 排名 用户 题数 罚时 A B C D retest2018_INT246 (INT246) (+) (+) ...