您的位置:首页 > 其它

Hamiltonian Spanning Tree CodeForces - 618D (思维)

2018-03-29 23:03 330 查看
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.

Example

Input

5 2 3

1 2

1 3

3 4

5 3

Output

9

Input

5 3 2

1 2

1 3

3 4

5 3

Output

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 .

代码

#include<iostream>
#include<cstdio>
#include<stack>
#include<algorithm>
#include<queue>
#include<cstring>
#include<cmath>

using namespace std;
int n,x,y;
int num=0;
vector<int>graph[200005];
bool dfs(int root,int fa)
{
int d=2;
for(int i=0;i<graph[root].size();i++)
{
int v=graph[root][i];
if(v==fa)
continue;
if(dfs(v,root)&&d)
num++,d--;
}
return d>0;
}
int main()
{
cin>>n>>x>>y;
int sign=false;
int p1,p2;
for(int i=1;i<n;i++)
{
scanf("%d%d",&p1,&p2);
graph[p2].push_back(p1);
graph[p1].push_back(p2);
if(graph[p1].size()==n-1)
sign=true;
if(graph[p2].size()==n-1)
sign=true;
}
if(x>y)
{
if(sign)
num++;
}
else
dfs(1,-1);
cout<<(long long)num*x+(long long)(n-1-num)*y<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: