codeforces 618D. Hamiltonian Spanning Tree

D. Hamiltonian Spanning Tree
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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.

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 nxy 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 nx 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,x,y。有一个n个点完全图,起始每条边边权为y,现在给了一个图中的生成树,这棵树上每条边的边权变为x。在新图中寻找一颗生成树,每条边边权最小且每个点只能经过一次。


分两种情况:


x > y,找新的生成树的时候就尽量少的选择给的树的边。但是因为这是个完全图,每个点有n-条边,正常情况下一定能找到一条完全不经过给定树的树边的路径(ans = y*(n-1))。只有一种特殊情况就是星形图(星形图就是有一个点度为n-1,就是给的生成树是一个点连接其他所有点),因为每个点必然要经过一次,但是这个星形图中间这个点所有边都被变为x了,所以必然有一条边权为x的被经过,其他的点连成一个圈就行了(ans = y*(n-1)+x)。


x < y,找新的生成树就尽量多的选这个树的边,我开始想的找这棵树的直径,但是这样就是错的,比如(1-2,1-3,4-5,4-6,1-4)这个图,直径只有3,而实际上可以选4条边。利用题目给的,每个点只能被经过一次,那么意思就是每个点的度最多为2,dfs的时候进行贪心计算最多能使用最多的边的数量就行了。
还有x==y的情况就归到x<y里面就行了。

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5+10;
struct node{
    int v,next;
    node(){}
    node(int v,int next):
        v(v),next(next){}
}E[N*5];
LL n,x,y,top;
int du[N];
int head[N];
LL max_cnt;

void add(int u,int v)
{
    E[top] = node(v,head[u]);
    head[u] = top++;
}

bool dfs(int u,int fa)
{
    int son = 2;
    for(int i = head[u];i != -1;i = E[i].next){
        int v = E[i].v;
        if(v == fa) continue;
        if(dfs(v,u) && son > 0){ ///每个点最多能连接2个点
            max_cnt++;
            son--;
        }
    }
    return son > 0;
}

int main(void)
{
    memset(head,-1,sizeof head);
    scanf("%I64d%I64d%I64d",&n,&x,&y);
    for(int i = 1;i < n;i++){
        int u,v;
        scanf("%d%d",&u,&v);
        du[u]++;
        du[v]++;
        add(u,v);
        add(v,u);
    }
    if(x <= y){
        dfs(1,-1);
        LL ans = (max_cnt)*x+(n-max_cnt-1)*y;
        printf("%I64d",ans);
    }
    else{
        bool flag = false;
        for(int i = 1;i <= n;i++){
            if(du[i] == n-1){ ///是否是星形图
                flag = true;
            }
        }
        LL ans = y*(n-1);
        if(flag) ans = ans-y+x;
        printf("%I64d",ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值