zoukankan      html  css  js  c++  java
  • Codeforces Round #328 (Div. 2)D. Super M 虚树直径

    D. Super M

    Ari the monster is not an ordinary monster. She is the hidden identity of Super M, the Byteforces’ superhero. Byteforces is a country that consists of n cities, connected by n - 1 bidirectional roads. Every road connects exactly two distinct cities, and the whole road system is designed in a way that one is able to go from any city to any other city using only the given roads. There are m cities being attacked by humans. So Ari... we meant Super M have to immediately go to each of the cities being attacked to scare those bad humans. Super M can pass from one city to another only using the given roads. Moreover, passing through one road takes her exactly one kron - the time unit used in Byteforces.

    However, Super M is not on Byteforces now - she is attending a training camp located in a nearby country Codeforces. Fortunately, there is a special device in Codeforces that allows her to instantly teleport from Codeforces to any city of Byteforces. The way back is too long, so for the purpose of this problem teleportation is used exactly once.

    You are to help Super M, by calculating the city in which she should teleport at the beginning in order to end her job in the minimum time (measured in krons). Also, provide her with this time so she can plan her way back to Codeforces.

    Input

    The first line of the input contains two integers n and m (1 ≤ m ≤ n ≤ 123456) - the number of cities in Byteforces, and the number of cities being attacked respectively.

    Then follow n - 1 lines, describing the road system. Each line contains two city numbers ui and vi (1 ≤ ui, vi ≤ n) - the ends of the roadi.

    The last line contains m distinct integers - numbers of cities being attacked. These numbers are given in no particular order.

    Output

    First print the number of the city Super M should teleport to. If there are many possible optimal answers, print the one with the lowest city number.

    Then print the minimum possible time needed to scare all humans in cities being attacked, measured in Krons.

    Note that the correct answer is always unique.

    Sample test(s)
    input
    7 2
    1 2
    1 3
    1 4
    3 5
    3 6
    3 7
    2 7
    output
    2
    3
     
    Note

    In the first sample, there are two possibilities to finish the Super M's job in 3 krons. They are:

     and .

    However, you should choose the first one as it starts in the city with the lower number.

     题意:给一棵树,其中有些点遭到了攻击,问从哪一点开始可以用最短的时间访问所有被攻击的点,并输出最少需要多少单位时间.

    题解: 我们先构造出一颗包含所以被攻击点的虚树,再求其直径

             答案那么就是 (2−直径  )。

    ///1085422276
    
    #include<bits/stdc++.h>
    using namespace std;
    //#pragma comment(linker, "/STACK:102400000,102400000")
    using namespace std ;
    typedef long long ll;
    #define mem(a) memset(a,0,sizeof(a))
    #define pb push_back
    inline ll read()
    {
        ll 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;
    }
    //****************************************
    const int  N=123456+50;
    #define mod 1000000007
    #define inf 1000000007
    
    vector<int >G[N*2];
    int Mark[N*2],d[N*2],q[N*2];
    void dfs(int x,int pre) {
         int bo=Mark[x];
         for(int i=0;i<G[x].size();i++) {
            if(G[x][i]==pre) continue;
            d[G[x][i]]=d[x]+1;
            dfs(G[x][i],x);
            if(q[G[x][i]]) bo=1;
         }
         q[x]=bo;
    }
    int main() {
        int n,m,v,u,x,mx=0;
       n=read(),m=read();
       for(int i=1;i<=n-1;i++) {
        scanf("%d%d",&u,&v);
        G[u].pb(v);G[v].pb(u);
       }mem(Mark);
       for(int i=1;i<=m;i++) {
        scanf("%d",&x);Mark[x]=1;
       }
       mem(d);mem(q);
       dfs(1,-1);mx=0,v=1;
       for(int i=1;i<=n;i++) {
        if(Mark[i]&&d[i]>mx) {
            mx=d[i];v=i;
        }
       }mem(d);mx=0;
       int sum=0;
       dfs(v,-1);
       for(int i=1;i<=n;i++) {
        if(q[i]) {
            mx=max(mx,d[i]);sum++;
        }
       }
       for(int i=1;i<=n;i++) {
        if(q[i]&&d[i]==mx) {
            v=min(v,i);
        }
       }
       printf("%d
    %d
    ",v,(sum-1)*2-mx);
      return 0;
    }
    代码
  • 相关阅读:
    继承项目第13周项目1基类中成员的访问限定符和派生类的继承方式
    架构业务wait和sleep区别狭义jiavaBean规范,三层架构模式
    文件文本编辑器ASP.net使用CKEditor(html文本编辑器)
    彩信对象android(5)_发彩信操作
    方法接口UML统一建模语言,java中七种设计原则,
    jquery实现jQuery实现图片轮播效果,jQuery实现焦点新闻
    设备文件BSP及嵌入式驱动开发笔记
    Invalidate
    C#集合类使用范例
    判断一段程序是由C 编译程序还是由C++编译程序编译的
  • 原文地址:https://www.cnblogs.com/zxhl/p/4953302.html
Copyright © 2011-2022 走看看