codeforces 618 D. Hamiltonian Spanning Tree(dfs)
2018-02-26 00:53
771 查看
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
相关文章推荐
- CodeForces 618 D.Hamiltonian Spanning Tree(dfs)
- CodeForces 618 D Hamiltonian Spanning Tree(树形DP)
- Codeforces 618 D Hamiltonian Spanning Tree
- 【19.27%】【codeforces 618D】Hamiltonian Spanning Tree
- Codeforces 618 D Hamiltonian Spanning Tree 贪心+dp
- codeforces 618D. Hamiltonian Spanning Tree
- CF618D:Hamiltonian Spanning Tree(贪心 & 树形dp & 最小路径覆盖)
- 【Wunder Fund Round 2016 (Div 1 + Div 2 combined)D】【贪心 dfs】Hamiltonian Spanning Tree 树的最小链划分
- Hamiltonian Spanning Tree CodeForces - 618D (思维)
- HDU 4582 DFS spanning tree 解题报告(贪心 & 树形DP)
- CodeForces 682C - Alyona and the Tree(dfs)
- CodeForces 723F st-Spanning Tree
- codeforces 763A Timofey and a tree(DFS)
- Codeforces 682C C. Alyona and the Tree (DFS)
- Codeforces 842C Ilya And The Tree【Dfs】
- Wunder Fund Round 2016 D. Hamiltonian Spanning Tree(贪心+dp)
- Codeforces 29D Ant on the Tree 树的遍历 dfs序
- hdu 4582 DFS spanning tree
- HDOJ 4582: DFS spanning tree
- Codeforces 383C . Propagating tree【树阵,dfs】