2019牛客暑期多校训练营(第四场)A meeting(dp)

链接: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.

备注:

1≤n≤1051 \leq n \leq 10^51≤n≤105

             树形dp,把定点1当成树根,进行两次dp,第一次获得这个点下面的点中的人到这个点的最长路径,第二次获得这个点上面点中的人到这个点最长路径就好了。

#include<cstdio>
#include<iostream>
#include<queue>
#include<utility>
#include<cstring>
using namespace std;
int n, m;
const int maxn = 100005;
vector<int>G[100005];
int dp[maxn]; int pos[maxn];
int ans[maxn]; int real_ans = 0x3f3f3f3f;
void dfs1(int u, int fa) {
	int res = dp[u];
	for (auto v : G[u]) {
		if (v == fa)continue;
		dfs1(v, u);
		res = max(res, pos[v] + 1);
	}
	pos[u] = res;
}
void dfs2(int u, int fa,int sum) {
	ans[u] = max(sum, pos[u]);
//	cout << u << " " << ans[u] << "\n";
	real_ans = min(real_ans, ans[u]);
	int tmax = -0x3f3f3f3f, tnum;
	int tmax2 = -0x3f3f3f3f;
	int tsum = 0;
	for (auto v : G[u]) {
		if (v == fa)continue;
		tsum++;
		if (tmax < pos[v]) {
			tmax = pos[v];
			tnum = v;
		}
	}
	for (auto v : G[u]) {
		if (v == fa)continue;
		if (v == tnum)continue;
		if (tmax2 < pos[v]) {
			tmax2 = pos[v];
		}
	}
	for (auto v : G[u]) {
		if (v == fa)continue;
		if (v == tnum)continue;
		dfs2(v, u, max(tmax + 2, sum + 1));
	}
	if (tsum != 0)
		dfs2(tnum, u, max(tmax2 + 2, sum + 1));
}
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	while (cin >> n >> m) {
		for (int i = 1; i <= n; i++) {
			dp[i] = -0x3f3f3f;
		}
		for (int i = 1; i < n; i++) {
			int a, b; cin >> a >> b;
			G[a].push_back(b);
			G[b].push_back(a);
		}
		for (int i = 0; i < m; i++) {
			int a; cin >> a;
			dp[a] = 0;
		}
		dfs1(1, 1);
		dfs2(1, 1, -0x3f3f3f);
		cout << real_ans << "\n";
	}
	return 0;
}
/*
6 6
1 2 
2 3
2 4
2 5
5 6
1 2 3 4 5 6
*/

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值