SPOJ:Ada and Orange Tree (LCA+Bitset)

题目链接:https://vjudge.net/problem/SPOJ-ADAORANG

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 <bits/stdc++.h>

using namespace std;

const int maxn = 5e5 + 7;

vector<int> depth;
vector<vector<int> >g, parent;
vector<bitset<252> > col;
int n, q, root, lg;
void dfs(int u, int fa, int d)
{
    parent[u][0] = fa;
    depth[u] = d;
    for(int i = 0;i < g[u].size();i ++) {
        int v = g[u][i];
        if(v != fa) {
            dfs(v, u, d + 1);
            col[u] |= col[v];
        }
    }
}
void init()
{
    dfs(root, -1, 0);
    for(int k = 0;k + 1 < lg;k ++) {
        for(int v = 0;v < n;v ++) {
            if(parent[v][k] < 0) parent[v][k + 1] = -1;
            else parent[v][k + 1] = parent[parent[v][k]][k];
        }
    }
}
int lca(int u, int v)
{
    if(depth[u] > depth[v]) swap(u, v);
    for(int k = 0;k < lg;k ++) {
        if( (depth[v] - depth[u]) >> k & 1) v = parent[v][k];
    }
    if(u == v) return u;
    for(int k = lg - 1;k >= 0;k --) {
        if(parent[v][k] != parent[u][k]) {
            v = parent[v][k];
            u = parent[u][k];
        }
    }
    return parent[v][0];
}
int main()
{
    int T;
    scanf("%d", &T);
    while(T --) {
        g.clear();
        parent.clear();
        depth.clear();
        col.clear();
        scanf("%d %d %d", &n, &q, &root);
        g.resize(n);
        parent.resize(n);
        depth.resize(n);
        col.resize(n);
        lg = 0;
        int tmp = 1;
        while(tmp <= n) lg ++, tmp *= 2;
        for(int i = 0;i < parent.size();i ++) {
            parent[i].resize(lg, -1);
        }
        for(int i = 0;i < n;i ++) {
            int t;
            scanf("%d", &t);
            col[i].set(t);
        }
        for(int i = 0;i < n - 1;i ++) {
            int u, v;
            scanf("%d %d", &u, &v);
            g[u].push_back(v);
            g[v].push_back(u);
        }
        init();
        while(q --) {
            int u, v;
            scanf("%d %d", &u, &v);
            int p = lca(u, v);
            printf("%d\n", col[p].count());
        }
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值