Codeforces 701E Connecting Universities 贪心

链接

Codeforces 701E Connecting Universities

题意

n个点的树,给你2*K个点,分成K对,使得两两之间的距离和最大

思路

贪心,思路挺巧妙的。首先dfs一遍记录每个点的子树中(包括自己)有多少点是这K个中间的,第二遍dfs时对于每一条边,取两端包含较少的值,这样就保证树中间的点不会被取到,留下的就是相隔更远的点了。方法确实想不到啊。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <cstdio>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>
#include <cstring>
#include <string>

#define LL long long
#define INF 0x3f3f3f3f
#define eps 1e-8

using namespace std;
struct edge{
int v, next;
}edges[400005];
int n, k;
int head[200005], tot = 0, vis[200005];
int sz[200005];
void Add(int u, int v){
edges[tot].v = v;
edges[tot].next = head[u];
head[u] = tot++;
}
void dfs(int u, int fa){
sz[u] = vis[u];
for (int i = head[u]; i != -1; i = edges[i].next){
int v = edges[i].v;
if (v == fa) continue;
dfs(v, u);
sz[u] += sz[v];
}
}
LL ans = 0;
void dfs2(int u, int fa){
for (int i = head[u]; i != -1; i = edges[i].next){
int v = edges[i].v;
if (v == fa) continue;
dfs2(v, u);
ans += min(sz[v], 2 * k - sz[v]);
}
}
int main(){
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif // ONLINE_JUDGE
memset(head, -1, sizeof(head));
scanf("%d%d", &n, &k);
for (int i = 1; i <= 2 * k; i++){
int x;
scanf("%d", &x);
vis[x] = 1;
}
for (int i = 1; i < n; i++){
int u, v;
scanf("%d%d", &u, &v);
Add(u, v);
Add(v, u);
}
dfs(1, 0);
dfs2(1, 0);
printf("%I64d\n", ans);
return 0;
}