zoukankan      html  css  js  c++  java
  • bzoj 1787: [Ahoi2008]Meet 紧急集合

    1787: [Ahoi2008]Meet 紧急集合

    Time Limit: 20 Sec  Memory Limit: 162 MB
    Submit: 3016  Solved: 1344
    [Submit][Status][Discuss]

    Description

    Input

    Output

    Sample Input

    6 4
    1 2
    2 3
    2 4
    4 5
    5 6
    4 5 6
    6 3 1
    2 4 4
    6 6 6

    Sample Output


    5 2
    2 5
    4 1
    6 0

    HINT

    Source

    Day1

    lca

    屠龙宝刀点击就送

    #include <ctype.h>
    #include <cstring>
    #include <cstdio>
    #define N 500005
    
    void read (int &x)
    {
        x=0;
        char ch=getchar();
        while (!isdigit(ch)) ch=getchar();
        while (isdigit(ch)) x=x*10+ch-'0',ch=getchar();
    }
    struct node
    {
        int next,to;
    }edge[N<<1];
    int dep[N],head[N<<1],cnt,n,m,dad[N][25];
    void add(int u,int v)
    {
        cnt++;
        edge[cnt].next=head[u];
        edge[cnt].to=v;
        head[u]=cnt;
    }
    void dfs(int x)
    {
        dep[x]=dep[dad[x][0]]+1;
        for(int i=0;dad[x][i];i++)
        dad[x][i+1]=dad[dad[x][i]][i];
        for(int i=head[x];i;i=edge[i].next)
        {
            if(dad[x][0]!=edge[i].to)
            {
                dad[edge[i].to][0]=x;
                dfs(edge[i].to);
            }
        }
    }
    void swap(int &x,int &y)
    {
        int tmp=x;
        x=y;
        y=tmp;
    } 
    int lca(int x,int y)
    {
        if(dep[x]>dep[y]) swap(x,y);
        for(int i=20;i>=0;i--)
        if(dep[dad[y][i]]>=dep[x]) y=dad[y][i];
        if(x==y) return x;
        for(int i=20;i>=0;i--) 
        if(dad[x][i]!=dad[y][i]) x=dad[x][i],y=dad[y][i];
        return dad[x][0];
    }
    int main()
    {
        read(n);read(m);
        for(int x,y,i=1;i<n;i++)
        {
            scanf("%d%d",&x,&y);
            add(x,y);
            add(y,x);
        }
        dfs(1);
        for(int x,y,z,i=1;i<=m;i++)
        {
            read(x);read(y);read(z);
            int root=lca(x,y)^(lca(x,z))^(lca(y,z));
            int ans=dep[x]+dep[root]-2*dep[lca(x,root)];
            ans+=dep[y]+dep[root]-2*dep[lca(y,root)];
            ans+=dep[z]+dep[root]-2*dep[lca(z,root)];
            printf("%d %d
    ",root,ans); 
        }
        return 0;
    }
    我们都在命运之湖上荡舟划桨,波浪起伏着而我们无法逃脱孤航。但是假使我们迷失了方向,波浪将指引我们穿越另一天的曙光。
  • 相关阅读:
    Vue.js
    Vue.js
    Vue.js
    Vue.js
    webpack(1)
    webpack(2)
    babel-loader7和babel8版本的问题
    [JZOJ4274] 终章-剑之魂
    [JZOJ427] 圣章-精灵使的魔法语
    BZOJ题表(红色表示已完成)
  • 原文地址:https://www.cnblogs.com/ruojisun/p/6814981.html
Copyright © 2011-2022 走看看