Codeforces 618 D Hamiltonian Spanning Tree

A group of n cities is connected by a network of roads. There is an undirected road between every pair of cities, so there are roads in total. It takes exactly y seconds to traverse any single road.

A spanning tree is a set of roads containing exactly n - 1 roads such that it’s possible to travel between any two cities using only these roads.Some spanning tree of the initial network was chosen. For every road in this tree the time one needs to traverse this road was changed from y to x seconds. Note that it’s not guaranteed that x is smaller than y.

You would like to travel through all the cities using the shortest path possible. Given n, x, y and a description of the spanning tree that was chosen, find the cost of the shortest path that starts in any city, ends in any city and visits all cities exactly once.

Input

The first line of the input contains three integers n, x and y (2 ≤ n ≤ 200 000, 1 ≤ x, y ≤ 109).

Each of the next n - 1 lines contains a description of a road in the spanning tree. The i-th of these lines contains two integers ui and vi(1 ≤ ui, vi ≤ n) — indices of the cities connected by the i-th road. It is guaranteed that these roads form a spanning tree.

Output

Print a single integer — the minimum number of seconds one needs to spend in order to visit all the cities exactly once.

Sample test(s)

input

1
2
3
4
5
5 2 3
1 2
1 3
3 4
5 3

output

1
9

input

1
2
3
4
5
5 3 2
1 2
1 3
3 4
5 3

output

1
8

Note

In the first sample, roads of the spanning tree have cost 2, while other roads have cost 3. One example of an optimal path is .

In the second sample, we have the same spanning tree, but roads in the spanning tree cost 3, while other roads cost 2. One example of an optimal path is .

Solution

分x>y和x<y两种情况考虑。

菊花图特判。

树上DP。

方程:

1
2
3
4
5
long long t1 = max(dp[u][0], dp[u][1]);  
long long t2 = max(t1, dp[u][2]);
dp[now][2] = max(dp[now][2]+t2, dp[now][1]+t1+1);  
dp[now][1] = max(dp[now][1]+t2, dp[now][0]+t1+1);  
dp[now][0] += t2;

含义:dp[u][x]从节点u发出x条黑线所能形成(及其子树)最多的不重复经过一个点的路径覆盖条数。

Code

 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
#include <cstdio>
#include <iostream>
using namespace std;
int getint() {
	int r = 0; char c = getchar();
	for (; '0' > c || c > '9'; c = getchar());
	for (; '0' <= c && c <= '9'; c = getchar()) r = r * 10 - '0' + c;
	return r;
}
const int maxn = 200005;
int n;
long long x, y;
int h[maxn];
struct edge_type {
	int to, next;
}edge[maxn<<1]; int cnte = 0;
int dst[maxn], ptr = 0;
void ins(int u, int v) {
	edge[++cnte].to = v;
	edge[cnte].next = h[u];
	h[u] = cnte;
}
long long dp[maxn][3];
int du[maxn];
void dfs(int now, int father) {
	for (int i = h[now]; i; i = edge[i].next) if (father != edge[i].to){
		long long u = edge[i].to;
		dfs(u, now);
        long long t1 = max(dp[u][0], dp[u][1]);  
        long long t2 = max(t1, dp[u][2]);
        dp[now][2] = max(dp[now][2]+t2, dp[now][1]+t1+1);  
        dp[now][1] = max(dp[now][1]+t2, dp[now][0]+t1+1);  
        dp[now][0] += t2;
	}
}
int main() {
	n = getint(); x = getint(); y = getint();
	if (x > y) {
		int dumax = 0;
		int u, v;
		for (int i = 1; i < n; ++i) {
			u = getint(); v = getint();
			++du[u], ++du[v];
			dumax = max(dumax, max(du[u], du[v]));
		}
		if (dumax != n-1)
			printf("%I64d", y * (n-1));
		else 
			printf("%I64d", y * (n-2)+x);
		return 0;
	}
	int u, v;
	for (int i = 1; i < n; ++i) {
		u = getint(); v = getint();
		ins(u, v); ins(v, u);
	}
	dfs(1, 0);
	int ans = max(max(dp[1][0], dp[1][1]), dp[1][2]);
	printf("%I64d", x * ans + (n-1-ans)*y);
}