Hamiltonian Spanning Tree CodeForces - 618D (思维)
2018-03-29 23:03
330 查看
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.
Example
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 .
代码
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.
Example
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 .
代码
#include<iostream> #include<cstdio> #include<stack> #include<algorithm> #include<queue> #include<cstring> #include<cmath> using namespace std; int n,x,y; int num=0; vector<int>graph[200005]; bool dfs(int root,int fa) { int d=2; for(int i=0;i<graph[root].size();i++) { int v=graph[root][i]; if(v==fa) continue; if(dfs(v,root)&&d) num++,d--; } return d>0; } int main() { cin>>n>>x>>y; int sign=false; int p1,p2; for(int i=1;i<n;i++) { scanf("%d%d",&p1,&p2); graph[p2].push_back(p1); graph[p1].push_back(p2); if(graph[p1].size()==n-1) sign=true; if(graph[p2].size()==n-1) sign=true; } if(x>y) { if(sign) num++; } else dfs(1,-1); cout<<(long long)num*x+(long long)(n-1-num)*y<<endl; return 0; }
相关文章推荐
- Hamiltonian Spanning Tree CodeForces - 618D
- Minimum spanning tree for each edge CodeForces - 609E(ST算法+树链剖分(或倍增LCA)+最小生成树)
- Minimum spanning tree for each edge CodeForces - 609E (Lca+最小生成树)
- Codeforces 618 D Hamiltonian Spanning Tree
- Codeforces Round #158 (Div. 2)D. Black and White Tree(搜索 + 思维)
- CF# Educational Codeforces Round 3 E. Minimum spanning tree for each edge
- codeforces 618 D. Hamiltonian Spanning Tree(dfs)
- CF618D:Hamiltonian Spanning Tree(贪心 & 树形dp & 最小路径覆盖)
- Codeforces Round #397(Div. 1 + Div. 2 combined)E. Tree Folding【思维+Dfs】dalao们的解法真巧妙系列QAQ
- CodeForces 618 D Hamiltonian Spanning Tree(树形DP)
- codeforces 618D. Hamiltonian Spanning Tree
- 【19.27%】【codeforces 618D】Hamiltonian Spanning Tree
- Educational Codeforces Round 3 E. Minimum spanning tree for each edge MST+树上路径倍增
- Hamiltonian Spanning Tree (思维)
- CodeForces 618 D.Hamiltonian Spanning Tree(dfs)
- Minimum spanning tree for each edge CodeForces - 609E
- Educational Codeforces Round 3 E. Minimum spanning tree for each edge LCA/(树链剖分+数据结构) + MST
- Codeforces Round #430 (Div. 2) Ilya And The Tree 树上因子 思维 + dfs
- Educational Codeforces Round 3 E (609E) Minimum spanning tree for each edge
- Educational Codeforces Round 3 E. Minimum spanning tree for each edge 最小生成树+树链剖分+线段树