time limit per test2 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard output

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.

Examples

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 .

【题解】



给你一个完全图n*(n-1)/2的图;

这个图的所有边边权都为y;

然后再对这个图的某个生成树上的边进行修改;这个生成树上的边边权从y改成x;

(这个生成树就是给你的n-1条边组成的树);

然后让你求遍历所有的边的最小边权和(每个点只能走一次);

如果x小于y;

则我们最后的路径应该在那个所给的生成树上的边尽可能地多;

怎样保证呢?

从任意一个点进行dfs(假如从1点开始);

每个点可以连接点边数肯定是2(大于3就不能保证每个点只走一次);



上图是样例输入的情形。

最后dfs能形成两条路径

1->2

和5->3->4

然后从这两个子图里面比如引一条2指向5的y边权边过去;就能遍历整张图了;

这个dfs的依据就是每个点最多只能联通两条边;

在dfs的时候记录这个点还能连几条边;

然后判断一下目标点能连几条边;如果都能连(都大于0)则连一条边;

如果y<=x

让非生成树边最多;

其实只有没有一个点和其余n-1个点相连的情况都能保证遍历的时候全是非生成树边;因为你总可以和那个不能通过生成树边到达的点通过非生成树边到达;

则特判一下就好;

如果有一个点能和其他n-1个点相连答案就是x+(n-2)*y否则就都是(n-1)*y;

#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <string>
#define lson L,m,rt<<1
#define rson m+1,R,rt<<1|1
#define LL long long using namespace std; const int MAXN = 2e5+10; const int dx[5] = {0,1,-1,0,0};
const int dy[5] = {0,0,0,-1,1};
const double pi = acos(-1.0); int n;
LL x,y,in=0;
vector <int> a[MAXN]; void input_LL(LL &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)) t = getchar();
LL sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
} void input_int(int &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)) t = getchar();
int sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
} bool dfs(int x,int fa)
{
int rest = 2;
int len = a[x].size();
for (int i =0 ;i <= len-1;i++)
{
int y = a[x][i];
if (y==fa)
continue;
bool j = dfs(y,x);
if (rest&&j)
{
rest--;
in++;
}
}
return rest>0;
} int main()
{
//freopen("F:\\rush.txt", "r", stdin);
input_int(n);input_LL(x);input_LL(y);
for (int i = 1;i <= n-1;i++)
{
int x0,y0;
input_int(x0);input_int(y0);
a[x0].push_back(y0);
a[y0].push_back(x0);
}
if (x<y)
{
dfs(1,-1);
LL ans = in*x + (n-1-in)*y;
cout << ans << endl;
}
else
{
for (int i = 1;i <= n;i++)
{
int len = a[i].size();
if (len == n-1)
{
cout << x+(n-2)*y<<endl;
return 0;
}
}
cout << (n-1)*y<<endl;
}
return 0;
}

【19.27%】【codeforces 618D】Hamiltonian Spanning Tree的更多相关文章

  1. 【 BowWow and the Timetable CodeForces - 1204A 】【思维】

    题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...

  2. Codeforces 618D Hamiltonian Spanning Tree(树的最小路径覆盖)

    题意:给出一张完全图,所有的边的边权都是 y,现在给出图的一个生成树,将生成树上的边的边权改为 x,求一条距离最短的哈密顿路径. 先考虑x>=y的情况,那么应该尽量不走生成树上的边,如果生成树上 ...

  3. CodeForces 618D Hamiltonian Spanning Tree

    题意:要把所有的节点都访问一次,并且不能重复访问,有两种方式访问,一种是根据树上的路径 走和当前节点连接的下一个节点cost x, 或者可以不走树上边,直接跳到不与当前节点连接的节点,cost y 分 ...

  4. Codeforces Edu3 E. Minimum spanning tree for each edge

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  5. Codeforces 1682 D Circular Spanning Tree

    题意 1-n排列,构成一个圆:1-n每个点有个值0或者1,0代表点的度为偶数,1代表点的度为计数:询问能否构成一棵树,树的连边在圆内不会相交,在圆边上可以相交,可以则输出方案. 提示 1. 首先考虑什 ...

  6. 【19.77%】【codeforces 570D】Tree Requests

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  7. 【27.91%】【codeforces 734E】Anton and Tree

    time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  8. 【51.27%】【codeforces 604A】Uncowed Forces

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  9. 【27.85%】【codeforces 743D】Chloe and pleasant prizes

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

随机推荐

  1. 微服务实践(七):从单体式架构迁移到微服务架构 - DockOne.io

    原文:微服务实践(七):从单体式架构迁移到微服务架构 - DockOne.io [编者的话]这是用微服务开发应用系列博客的第七篇也是最后一篇.第一篇中介绍了微服务架构模式,并且讨论了微服架构的优缺点: ...

  2. 汉字转换成对应ASCII

    private static byte[] hanziToHexByte(string hanzistring)        {            byte[] bytes = Encoding ...

  3. C语言深度剖析-----数组参数和指针参数分析

    数组退化的意义 当向函数传递数组时, 二维数组参数 等价关系 注意事项 只能去一维数组 无法向一个函数传递一个任意的多维数组,注释地方出错 传递与访问二维数组的方式 动态地算出二维数组的列

  4. OC学习篇之---协议的概念和用法

    这一篇文章我们在来看一下OC中协议的概念以及用法,协议也是OC中的一个重点,Foundation框架以及我们后面在写代码都会用到. OC中的协议就是相当于Java中的接口(抽象类),只不过OC中的名字 ...

  5. AE 获取地图上当前选中的要素

    樱木 原文 AE开发----获取地图上当前选中的要素 Code1 int selCount = axMapControl1.Map.SelectionCount; IEnumFeature pEnum ...

  6. Java反射学习总结四(动态代理使用实例和内部原理解析)

    通过上一篇文章介绍的静态代理Java反射学习总结三(静态代理)中,大家可以发现在静态代理中每一个代理类只能为一个接口服务,这样一来必然会产生过多的代理,而且对于每个实例,如果需要添加不同代理就要去添加 ...

  7. 【t041】距离之和

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 在一条数轴上有N头牛在不同的位置上,每头牛都计算到其它各头牛的距离.求这n*(n-1)个距离的总和. ...

  8. jQuery 中 is() 函数常见使用方法

    依据选择器.DOM元素或 jQuery 对象来检測匹配元素集合.假设当中至少有一个元素符合这个给定的表达式就返回true. 假设没有元素符合,或者表达式无效.都返回'false'. '''注意:''' ...

  9. Helloworld之Spring依赖注入/控制反转(DI/IoC)版

    Helloworld之Spring依赖注入/控制反转(DI/IoC)版 作者:雨水, 日期:2014-10-29 摘要:本文主要用于培训刚開始学习的人理解Spring中的依赖注入的基本概念. 先介绍依 ...

  10. GDB(十)--调试正在运行的进程

    我编写了一个循环: long i;    for (i = 0; i < 999999; i++) {        mt.a += 1;        sleep(1);    }把它编译成a ...