• CodeForces


    Discription

    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 yseconds 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 .

        题目要求就是先给你一个边权都是y的完全图,然后再把一棵树的边都改成x,之后问你图中权值最小的一个包含n个点的链的权值是多少。

        分类讨论一下:

            1.如果y<=x的话,我们尽量保留原图中的边就好了。 可以发现只要给出的树 不是菊花图的话,总能找出一条包含n个点的链,使得链上的边权都是y。所以这种情况特判一下是不是菊花图就好了。

            2.如果x<y的话,我们是希望尽量保留给出的树中的边的,这部分其实就是求树的最小路径覆盖,也就是选出尽量多的边,使得它们构成的联通块在树中都是链,这样用y把链首尾相连就可以了(两个链相接的地方在树中一定没有边,不然答案还能+1,于最优性不符)。  直接一个树上dp就ojbk了。

    #include<bits/stdc++.h>
    #define ll long long
    using namespace std;
    const int maxn=200005;
    int to[maxn*2],ne[maxn*2],num;
    int hd[maxn],n,m,X,Y,D[maxn],A,f[maxn][3];
    inline void add(int x,int y){ to[++num]=y,ne[num]=hd[x],hd[x]=num;}
    
    void dfs(int x,int fa){
    	for(int i=hd[x];i;i=ne[i]) if(to[i]!=fa){
    		dfs(to[i],x);
    		f[x][2]=max(f[x][2]+f[to[i]][2],f[x][1]+f[to[i]][1]+1);
    		f[x][1]=max(f[x][1]+f[to[i]][2],f[x][0]+f[to[i]][1]+1);
    		f[x][0]+=f[to[i]][2];
        }
    	
    	f[x][1]=max(f[x][1],f[x][0]);
    	f[x][2]=max(f[x][2],f[x][1]);
    }
    
    int main(){
    	int uu,vv;
    	scanf("%d%d%d",&n,&X,&Y);
    	for(int i=1;i<n;i++) scanf("%d%d",&uu,&vv),add(uu,vv),add(vv,uu),D[uu]++,D[vv]++;
    	if(Y<=X){
    		for(int i=1;i<=n;i++) if(D[i]==n-1){
    			printf("%lld
    ",(n-2)*(ll)Y+(ll)X);
    			return 0;
    		}
    		printf("%lld
    ",(n-1)*(ll)Y);
    		return 0;
    	}
    	
    	dfs(1,-1),A=f[1][2];
    	
    	printf("%lld
    ",X*(ll)A+(n-A-1)*(ll)Y);
    	return 0;
    }
    

      

  • 相关阅读:
    ReadOnly TextEdit输入问题
    关于正太分布单侧区间上下限的定义
    关于Devexpress richEditControl
    CentOS7 升级 cmake
    极客时间实战目录
    kafka安装
    查找连续相同值的算法,并给出连续相同值的个数以及位置
    解决若依linux启动ERROR
    supervisor配置进程
    python做上位机
  • 原文地址:https://www.cnblogs.com/JYYHH/p/8927041.html
Copyright © 2020-2023  润新知