zoukankan      html  css  js  c++  java
  • 【BZOJ-1787&1832】Meet紧急集合&聚会 倍增LCA

    1787: [Ahoi2008]Meet 紧急集合

    Time Limit: 20 Sec  Memory Limit: 162 MB
    Submit: 2259  Solved: 1023
    [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

    1832: [AHOI2008]聚会

    Time Limit: 10 Sec  Memory Limit: 64 MB
    Submit: 1288  Solved: 505
    [Submit][Status][Discuss]

    Description

    Y岛风景美丽宜人,气候温和,物产丰富。Y岛上有N个城市,有N-1条城市间的道路连接着它们。每一条道路都连接某两个城市。幸运的是,小可可通过这些道路可以走遍Y岛的所有城市。神奇的是,乘车经过每条道路所需要的费用都是一样的。小可可,小卡卡和小YY经常想聚会,每次聚会,他们都会选择一个城市,使得3个人到达这个城市的总费用最小。 由于他们计划中还会有很多次聚会,每次都选择一个地点是很烦人的事情,所以他们决定把这件事情交给你来完成。他们会提供给你地图以及若干次聚会前他们所处的位置,希望你为他们的每一次聚会选择一个合适的地点。

    Input

    第一行两个正整数,N和M。分别表示城市个数和聚会次数。后面有N-1行,每行用两个正整数A和B表示编号为A和编号为B的城市之间有一条路。城市的编号是从1到N的。再后面有M行,每行用三个正整数表示一次聚会的情况:小可可所在的城市编号,小卡卡所在的城市编号以及小YY所在的城市编号。

    Output

    一共有M行,每行两个数Pos和Cost,用一个空格隔开。表示第i次聚会的地点选择在编号为Pos的城市,总共的费用是经过Cost条道路所花费的费用。

    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

    数据范围:
    100%的数据中,N<=500000,M<=500000。
    40%的数据中N<=2000,M<=2000。

    HINT

    Source

    Solution

    水题,随便倍增一下求LCA就可以

    Code

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    inline int read()
    {
        int x=0,f=1; char ch=getchar();
        while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();}
        while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();}
        return x*f;
    }
    #define maxn 500010
    int n,m,deep[maxn],father[maxn][21];
    struct EdgeNode{int next,to;}edge[maxn<<1];
    int head[maxn],cnt;
    void add(int u,int v) {cnt++; edge[cnt].next=head[u]; head[u]=cnt; edge[cnt].to=v;}
    void insert(int u,int v) {add(u,v); add(v,u);}
    void DFS(int now,int last)
    {
        for (int i=1; i<=20; i++)
            if (deep[now]>=(1<<i)) father[now][i]=father[father[now][i-1]][i-1];
            else break;
        for (int i=head[now]; i; i=edge[i].next)
            if (edge[i].to!=last)
                {
                    father[edge[i].to][0]=now;
                    deep[edge[i].to]=deep[now]+1;
                    DFS(edge[i].to,now);
                }
    }
    int LCA(int x,int y)
    {
        if (deep[x]<deep[y]) swap(x,y);
        int dd=deep[x]-deep[y];
        for (int i=0; i<=20; i++)
            if ((1<<i)&dd) x=father[x][i];
        for (int i=20; i>=0; i--)
            if (father[x][i]!=father[y][i])
                x=father[x][i],y=father[y][i];
        if (x==y) return x; return father[x][0];
    }
    int Dist(int u,int v)
    {
        int lca=LCA(u,v);
        return deep[u]+deep[v]-(deep[lca]<<1);
    }
    int ans,rt;
    int main()
    {
        n=read(); m=read();
        for (int u,v,i=1; i<=n-1; i++)
            u=read(),v=read(),insert(u,v);
        DFS(1,0);
        for (int x,y,z,i=1; i<=m; i++)
            {
                x=read(),y=read(),z=read();
                int fxy=LCA(x,y),fyz=LCA(y,z),fxz=LCA(x,z);
                rt=(fxy==fyz)? fxz : fxy==fxz? fyz : fxy;
                ans=Dist(x,rt)+Dist(y,rt)+Dist(z,rt);
                printf("%d %d
    ",rt,ans);
            }
        return 0;
    }

    在学校被***毒的不能自拔,感觉已经不想在班里待下去了

  • 相关阅读:
    Git:本地文件到远程仓库
    logstash.conf 配置:input kafka,filter,output elasticsearch/mysql
    PowerDesigner根据Excel设计数据表结构 Excel表结构导入PowerDesigner
    VBScript PowerDesigner使用手册
    PowerDesigner导出数据表结构到Excel 一个表一个Sheet 带链接目录
    PowerDesigner导出数据表结构到Excel 所有表结构在同一个Sheet中
    Windows下如何用virtualenv创建虚拟环境
    解决ValueError: day is out of range for month的问题
    正则表达式
    移动端库和框架
  • 原文地址:https://www.cnblogs.com/DaD3zZ-Beyonder/p/5634322.html
Copyright © 2011-2022 走看看