Codeforces 618 D Hamiltonian Spanning Tree 贪心+dp

探讨了一个关于Hamiltonian Spanning Tree的最短路径问题,通过分析不同情况下边权值的影响,给出了两种解题思路及对应的AC代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

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,现在给了一颗最小生成树,树上的边的权值为x。现在要从某个点出发访问所有的顶点,问路径上经过的最小权值之和是多少?

【解题方法】思维&&脑洞。题解参考:http://blog.youkuaiyun.com/yp_2013/article/details/50629164

           首先经过每个点恰好一次,那么走的总边数是一定的,即n-1,那么既然边的权值只有两种,那么肯定是算出一种,然后用减法求得另外一种喽!
分为两种情况,如果最小生成树上的边小的话,那么肯定就是要贪心地在树上选最小的边!
一个点可以有入度和出度,那么每个节点就是最多连两条边,所以就在最小生成树上构造这样一条路径就ok了!
另一种情况,如果最小生成树是星形的话,(也就是某个节点的度数为n-1),那么无疑肯定要选择一条最小生成树上的边,其余的话都绝对可以用非树上的边去遍历!

【AC 代码】

#include <bits/stdc++.h>
using namespace std;
const int maxn = 200010;
vector<int>G[maxn];
bool used[maxn];
int degree[maxn];
void dfs(int u,int f,int &num)
{
    int cnt=0;
    for(auto v : G[u]){
        if(v==f) continue;
        dfs(v,u,num);
        cnt += used[v];
    }
    used[u] = cnt<2?1:0;
    num+=min(cnt,2);
}
int main()
{
    int n,x,y,u,v;
    cin>>n>>x>>y;
    for(int i=1; i<=n; i++) G[i].clear();
    memset(degree,0,sizeof(degree));
    for(int i=1; i<=n-1; i++){
        cin>>u>>v;
        G[u].push_back(v);
        G[v].push_back(u);
        degree[u]++;
        degree[v]++;
    }
    if(x<y){
        memset(used,0,sizeof(used));
        int num=0;
        dfs(1,-1,num);
        printf("%lld\n",1LL*((1LL)*num*x+1LL*(n-1-num)*y));
    }
    else{
        bool ok=0;
        for(int i=1; i<=n; i++){
            if(degree[i]==n-1){
                ok=1;
                break;
            }
        }
        if(ok==1){
            printf("%lld\n",(1LL)*x+(1LL)*(n-2)*y);
        }
        else{
            printf("%lld\n",(1LL)*(n-1)*y);
        }
    }
    return 0;
}

【解题方法2 贪心】

            贪心的解法,c表示以这个点为根的下面的有效边为多少,然后dfs表示的是这个点能贡献出来的有限边是多少以及答案可以贡献出多少!

【AC 代码 】

#include <bits/stdc++.h>
using namespace std;
const int maxn = 200010;
vector<int>G[maxn];
int degree[maxn];
int n,x,y,num;
int dfs(int u,int v)
{
    int c=0;
    for(auto it : G[u]){
        if(it==v) continue;
        c += dfs(it,u);
    }
    if(c==0) return 1;
    if(c==1) {num++; return 1;}
    else{
        num+=2;
        return 0;
    }
}
int main()
{
    cin>>n>>x>>y;
    memset(degree,0,sizeof(degree));
    for(int i=1; i<=n; i++) G[i].clear();
    for(int i=1; i<=n-1; i++){
        int u,v;
        cin>>u>>v;
        G[u].push_back(v);
        G[v].push_back(u);
        degree[u]++,degree[v]++;
    }
    if(x>=y)
    {
        bool ok=0;
        for(int i=1; i<=n; i++){
            if(degree[i]==n-1){
                ok=1;
                break;
            }
        }
        if(ok) printf("%lld\n",(1LL)*x+(1LL)*y*(n-2));
        else printf("%lld\n",(1LL)*y*(n-1));
    }else{
        num=0;
        dfs(1,-1);
        printf("%lld\n",(1LL)*num*x+(1LL)*(n-1-num)*y);
    }
    return 0;
}


当前提供的引用内容并未提及关于Codeforces比赛M1的具体时间安排[^1]。然而,通常情况下,Codeforces的比赛时间会在其官方网站上提前公布,并提供基于不同时区的转换工具以便参赛者了解具体开赛时刻。 对于Codeforces上的赛事而言,如果一场名为M1的比赛被计划举行,则它的原始时间一般按照UTC(协调世界时)设定。为了得知该场比赛在UTC+8时区的确切开始时间,可以遵循以下逻辑: - 前往Codeforces官网并定位至对应比赛页面。 - 查看比赛所标注的标准UTC起始时间。 - 将此标准时间加上8小时来获取对应的北京时间(即UTC+8)。 由于目前缺乏具体的官方公告链接或者确切日期作为依据,无法直接给出Codeforces M1比赛于UTC+8下的实际发生时段。建议定期访问Codeforces平台查看最新动态更新以及确认最终版程表信息。 ```python from datetime import timedelta, datetime def convert_utc_to_bj(utc_time_str): utc_format = "%Y-%m-%dT%H:%M:%SZ" bj_offset = timedelta(hours=8) try: # 解析UTC时间为datetime对象 utc_datetime = datetime.strptime(utc_time_str, utc_format) # 转换为北京时区时间 beijing_time = utc_datetime + bj_offset return beijing_time.strftime("%Y-%m-%d %H:%M:%S") except ValueError as e: return f"错误:{e}" # 示例输入假设某场Codeforces比赛定于特定UTC时间 example_utc_start = "2024-12-05T17:35:00Z" converted_time = convert_utc_to_bj(example_utc_start) print(f"Codeforces比赛在北京时间下将是:{converted_time}") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值