Codeforces 618D Hamiltonian Spanning Tree【思维+Dfs】

224 篇文章 2 订阅

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,现在要求从一个点出发,遍历所有的点,并且需要保证每个点只能经过一次,问路径的最小值。


思路:


①如果x>=y,如果其不是菊花图,结果就一定是(n-1)*y,否则需要一条树边来连通其他部分,那么结果就一定是x+(n-2)*y;


②如果x<y,那么对于每个点都有一个2的限制,那么我们过程Dfs贪心一下就行了。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
#define ll __int64
ll cnt;
vector<ll>mp[250000];
ll degree[250000];
int Dfs(int u,int from)
{
    int son=2;
    for(int i=0;i<mp[u].size();i++)
    {
        int v=mp[u][i];
        if(v==from)continue;
        if(Dfs(v,u)&&son>0)
        {
            son--;
            cnt++;
        }
    }
    return son>0;
}
int main()
{
    ll n,x,y;
    while(~scanf("%I64d%I64d%I64d",&n,&x,&y))
    {
        memset(degree,0,sizeof(degree));
        for(ll i=1;i<=n;i++)mp[i].clear();
        for(ll i=0;i<n-1;i++)
        {
            ll x,y;scanf("%I64d%I64d",&x,&y);
            mp[x].push_back(y);
            mp[y].push_back(x);
            degree[x]++;degree[y]++;
        }
        if(x>=y)
        {
            ll flag=0;
            for(ll i=1;i<=n;i++)
            {
                if(degree[i]==n-1)flag=1;
            }
            if(flag==0)
            {
                printf("%I64d\n",(n-1)*y);
            }
            else printf("%I64d\n",x+(n-2)*y);
        }
        else
        {
            cnt=0;
            Dfs(1,-1);
            printf("%I64d\n",cnt*x+(n-1-cnt)*y);
        }
    }
}










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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值