• 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 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=450010;
    int Laxt[maxn],Next[maxn<<1],To[maxn<<1];
    int fa[maxn][20],dep[maxn],N,Q,rt,cnt;
    bitset<251>S[maxn];
    void init()
    {
        for(int i=1;i<=N;i++) S[i].reset();
        for(int i=1;i<=N;i++) Laxt[i]=0; 
        memset(Laxt,0,sizeof(Laxt));
        cnt=0;
    }
    void add(int u,int v){
        Next[++cnt]=Laxt[u];
        Laxt[u]=cnt;
        To[cnt]=v;
    }
    void dfs(int u,int f){
        fa[u][0]=f; dep[u]=dep[f]+1;
        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=1;i<20;i++)
         for(int j=1;j<=N;j++)
          fa[j][i]=fa[fa[j][i-1]][i-1];
    }
    int LCA(int u,int v){
        if(dep[u]<dep[v]) swap(u,v);
        for(int i=19;i>=0;i--)
          if(dep[fa[u][i]]>=dep[v])
            u=fa[u][i];
        if(u==v) return u;
        for(int i=19;i>=0;i--)
          if(fa[u][i]!=fa[v][i])
            u=fa[u][i],v=fa[v][i];
        return fa[u][0];
    }
    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=1;i<=N;i++){
                scanf("%d",&x);
                S[i][x]=1;
            }
            for(int i=1;i<N;i++){
                scanf("%d%d",&u,&v);
                u++; v++;
                add(u,v); add(v,u);
            }
            dfs(rt,0); RMQ();
            while(Q--){
                scanf("%d%d",&u,&v);
                u++; v++;
                lca=LCA(u,v);
                printf("%d
    ",S[lca].count());
            }
        }
        return 0;
    }
  • 相关阅读:
    Java基于Socket文件传输示例
    mysql 改变编码
    POI应用:利用word模板批量生成word文档(java中word文档的读写)
    Win 7—FTP服务器配置
    Chrome 快捷键
    JAVA中使用FTPClient上传下载 java利用ftp协议上传文件(by me)
    JAVA中使用FTPClient上传下载
    如何去除TD之间的空隙
    较丰富的教程
    输入年月 返回当月天数.html
  • 原文地址:https://www.cnblogs.com/hua-dong/p/8921033.html
Copyright © 2020-2023  润新知