codeforces 618 D. Hamiltonian Spanning Tree(dfs)

28 篇文章 0 订阅
24 篇文章 0 订阅

Description

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

题目大意

n个点,完全图,初始状态每条边的权值是y;
输入的n-1条边的权值由y变成x,x与y的大小关系不确定,
且这n-1条边一定是图的一个生成树。
寻找一条价值最小的路径,通过每一个点且只经过一次。

解题思路

当x> y时,尽量选取不在生成树上的边,当有一个点的度为n-1 时,必定选取一条x 的边通往此点,其余情况下均为 (n-1)*y;
当x< y时,尽量选取生成树上的边,则根据每个点的度最大为2 的策略进行dfs,得出最大可选取边数。

代码实现

#include <bits/stdc++.h>
#define IO ios::sync_with_stdio(false);\
    cin.tie(0);\
    cout.tie(0);
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
const int maxn=2e5+7;
int d[maxn];
ll num=0;
struct node
{
    int to;
    int next;
} edge[maxn*2];
int head[maxn],tot;
void addedge(int u,int v)
{
    edge[tot].to = v;
    edge[tot].next = head[u];
    head[u] = tot++;
}
int dfs(int u,int v)
{
    int unused=2;
    for(int i=head[u]; i!=-1; i=edge[i].next)
    {
        int to=edge[i].to;
        if(to==v) continue;
        if(dfs(to,u)&&unused>0)
        {
            num++;
            unused--;
        }
    }
    return unused>0;
}
int main()
{
    IO;
    ll n,x,y;
    cin>>n>>x>>y;
    int u,v;
    memset(head,-1,sizeof head);
    for(int i=0;i<n-1;i++)
    {
        cin>>u>>v;
        d[u]++;
        d[v]++;
        addedge(u,v);
        addedge(v,u);
    }
    if(x>=y)
    {
        bool flag=true;
        for(int i=1;i<=n;i++)
            if(d[i]==n-1)
        {
            flag=false;
            break;
        }
        if(flag)
            cout<<(ll)(y*(n-1))<<endl;
        else
            cout<<(ll)(y*(n-2)+x)<<endl;
    }
    else
    {
        dfs(6,0);
        cout<<(ll)(num*x+y*(n-1-num))<<endl;
    }
    return 0;
}

Ps
一开始对于x< y的情况一直以为选取的最大边数为(度为1 的点-2),即生成树的直径
附反例数据

6 2 3
1 2
1 3
1 4
4 5
4 6
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值