UVa 12677 - Join two kingdoms

contents

  1. 1. Problem
  2. 2. Sample Input
  3. 3. Sample Output
  4. 4. Solution

Problem

給兩棵樹,請問將兩個樹用一條邊連起來,則獲得新的一棵樹的最長路徑期望值為何。

Sample Input

1
2
3
4
5
6
7
8
9
10
11
12
13
14
4 5
1 2
2 3
4 2
2 3
3 4
4 1
4 5
1 5
1 2
2 3
3 4
4 5

Sample Output

1
2
5.350
4.400

Solution

利用 dp 方法,找到通過點 v 的 (以 v 為路徑端點)最長路徑為何,以及兩棵樹分別原有的最長路徑為何。

接著考量是否加入新一條會成為最長路徑,也就是說會通過新增加的邊。分別對每個樹的節點構成的最長路徑做排序,接著窮舉連接的點,用二分搜索找到另一個樹的節點,能夠使之成為更長的最長路徑。

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <string.h>
#define oo 0xfffffff
using namespace std;
vector<int> g[65536];
int dp_down[65536][2], dp_up[65536];
int used[65536];
void dfs(int u) {
used[u] = 1;
dp_down[u][0] = dp_down[u][1] = 0;
for(int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if(used[v] == 0) {
dfs(v);
if(dp_down[v][0]+1 > dp_down[u][1])
dp_down[u][1] = dp_down[v][0] + 1;
if(dp_down[u][1] > dp_down[u][0])
swap(dp_down[u][0], dp_down[u][1]);
}
}
}
void dfs2(int u, int dep) {
dp_up[u] = dep, used[u] = 1;
for(int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if(used[v] == 0) {
int hv;
if(dp_down[v][0] + 1 != dp_down[u][0])
hv = dp_down[u][0];
else
hv = dp_down[u][1];
hv = max(hv, dp_up[u]);
dfs2(v, hv+1);
}
}
}
int main() {
int n, m;
int x, y;
while (scanf("%d %d", &n, &m) == 2) {
int mxpath[2][65636];
int a_diameter = 0, b_diameter = 0;
for (int i = 0; i <= n; i++)
g[i].clear();
for (int i = 1; i < n; i++) {
scanf("%d %d", &x, &y);
g[x].push_back(y);
g[y].push_back(x);
}
memset(used, 0, sizeof(used));
dfs(1);
memset(used, 0, sizeof(used));
dfs2(1, 0);
for (int i = 1; i <= n; i++) {
mxpath[0][i] = max(max(dp_down[i][0], dp_down[i][1]), dp_up[i]);
a_diameter = max(a_diameter, dp_down[i][0] + dp_down[i][1]);
a_diameter = max(a_diameter, dp_down[i][0] + dp_up[i]);
}
for (int i = 0; i <= m; i++)
g[i].clear();
for (int i = 1; i < m; i++) {
scanf("%d %d", &x, &y);
g[x].push_back(y);
g[y].push_back(x);
}
memset(used, 0, sizeof(used));
dfs(1);
memset(used, 0, sizeof(used));
dfs2(1, 0);
for (int i = 1; i <= m; i++) {
mxpath[1][i] = max(max(dp_down[i][0], dp_down[i][1]), dp_up[i]);
b_diameter = max(b_diameter, dp_down[i][0] + dp_down[i][1]);
b_diameter = max(b_diameter, dp_down[i][0] + dp_up[i]);
}
double ret = 0, suffix[65536] = {};
sort(mxpath[0]+1, mxpath[0]+1+n);
sort(mxpath[1]+1, mxpath[1]+1+m);
for (int i = m; i >= 0; i--)
suffix[i] = suffix[i+1] + mxpath[1][i];
int mx_diameter = max(a_diameter, b_diameter);
for (int i = 1, j = m; i <= n; i++) {
while (j > 0 && mxpath[0][i] + mxpath[1][j] >= mx_diameter)
j--;
ret += j * mx_diameter + (suffix[j + 1] + mxpath[0][i] * (m - j) + m - j);
}
printf("%.3lf\n", ret / n / m);
}
return 0;
}
/*
*/