牛客多校__meeting

链接:https://ac.nowcoder.com/acm/contest/884/A
来源:牛客网
 

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 524288K,其他语言1048576K
64bit IO Format: %lld

题目描述

A new city has just been built. There're nnn interesting places numbered by positive numbers from 111 to nnn.

In order to save resources, only exactly n−1n-1n−1 roads are built to connect these nnn interesting places. Each road connects two places and it takes 1 second to travel between the endpoints of any road.

There is one person in each of the places numbered x1,x2…xkx_1,x_2 \ldots x_kx1​,x2​…xk​ and they've decided to meet at one place to have a meal. They wonder what's the minimal time needed for them to meet in such a place. (The time required is the maximum time for each person to get to that place.)

输入描述:

 

First line two positive integers, n,kn,kn,k - the number of places and persons.

For each the following n−1n-1n−1 lines, there're two integers a,ba,ba,b that stand for a road connecting place aaa and bbb. It's guaranteed that these roads connected all nnn places.

On the following line there're kkk different positive integers x1,x2…xkx_1,x_2 \ldots x_kx1​,x2​…xk​ separated by spaces. These are the numbers of places the persons are at.

输出描述:

A non-negative integer - the minimal time for persons to meet together.

示例1

输入

复制

4 2
1 2
3 1
3 4
2 4

输出

复制

2

说明

They can meet at place 1 or 3.

题意:在一棵树上有若干点,找出一点,使这若干点到这个点的最长时间最小,输出这个时间

题解:相当于找出相距最远的两个点,时间即为这两点的距离除以2向上取整

证明:我们取两人路径中和一头距离为d/2上取整的一个点,让所有人在这相聚。如 果有一个人在d/2时间内到不了,那么它和路径两头中与它远的那一头的距离大于d,与 最远的假设矛盾

找到这样最远的一对点类似找树的直径。可以直接dp,也可以采用两遍bfs:从任意一个关 键点开始,找到离它最远的关键点x,再从x开始bfs,找到的新的最远点和x形成的就是直径

#include 

using namespace std;

const int maxn =1e5+9;
const int INF = 0x3f3f3f3f;

vectorV;

struct rt{
    int v,next;
}edge[maxn*10];

int tot;
int head[maxn];

void add_edge(int u,int v){
    edge[tot].v=v;
    edge[tot].next=head[u];
    head[u]=tot++;
}

int dis[maxn];

void bfs(int s){
    memset(dis,0x3f,sizeof(dis));
    dis[s]=0;
    queueque;
    que.push(s);
    while(!que.empty()){
        int u=que.front(); que.pop();
        for(int i=head[u];i!=-1;i=edge[i].next){
            int v=edge[i].v;
            if(dis[v]==INF){
                dis[v]=dis[u]+1;
                que.push(v);
            }
        }
    }
    int D=0,id=s;
    for(int i=0;i

ps:dp写法,大佬队友写的

#include 
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
const int N = 100005;
 
int Head[N];
int Next[2*N];
int V[2*N];
int tot;
int p[N];
int s[N];
int ans;
 
struct Node
{
    int id;
    int num;
};
Node M2[N][2];
 
void add(int u, int v)
{
    V[tot] = v;
    Next[tot] = Head[u];
    Head[u] = tot++;
}
 
void dfs(int u, int fa)
{
    for(int i = Head[u]; i != -1; i = Next[i])
    {
        int v = V[i];
        if(v != fa)
        {
            dfs(v, u);
            int x = 0;
            if((s[v] > 0) || p[v]) x = s[v]+1;
            if(x > M2[u][0].num)
            {
                M2[u][1] = M2[u][0];
                M2[u][0] = (Node){v, x};
            }
            else if(x > M2[u][1].num) M2[u][1] = (Node){v, x};
            s[u] = max(s[u], x);
        }
    }
}
 
void dfs2(int u, int fa, int k)
{
    ans = min(ans, max(s[u], k));
    if(k != 0) k++;
    for(int i = Head[u]; i != -1; i = Next[i])
    {
        int v = V[i];
        if(v != fa)
        {
            int a = 0;
            if(M2[u][0].id != v && M2[u][0].num != 0) a = M2[u][0].num+1;
            else if(M2[u][1].id != v && M2[u][1].num != 0) a = M2[u][1].num+1;
            if(a == 0 && p[u]) a = 1;
            a = max(a, k);
            dfs2(v, u, a);
        }
    }
}
 
int main()
{
    int n, m;
    while(~scanf("%d%d", &n, &m))
    {
        ans = INF;
        tot = 0;
        memset(Head, -1, sizeof(Head));
        memset(p, 0, sizeof(p));
        memset(M2, 0, sizeof(M2));
        memset(s, 0, sizeof(s));
        for(int i = 1; i < n; i++)
        {
            int u, v;
            scanf("%d%d", &u, &v);
            add(u, v);
            add(v, u);
        }
        for(int i = 0; i < m; i++)
        {
            int x;
            scanf("%d", &x);
            p[x] = 1;
        }
        dfs(1, 0);
        dfs2(1, 0, 0);
        printf("%d\n", ans);
    }
    return 0;
}

 

你可能感兴趣的:(牛客网,宽搜bfs)