CodeForces 618D Hamiltonian Spanning Tree
题意:要把所有的节点都访问一次,并且不能重复访问,有两种方式访问,一种是根据树上的路径
走和当前节点连接的下一个节点cost x, 或者可以不走树上边,直接跳到不与当前节点连接的节点,cost y
分析:
别被树吓着!
一定会走n-1条路,那么就是有一些走树上的边,有一些不走。
如果树上的路径cost更大(x >= y),那么尽可能的不走树上的路径,那么根据尝试可以找到规律
如果有一个节点是所有节点的父节点,也就是说这个节点的度为n-1,那么只会走一个x其他都是y
如果没有这个节点,一定可以全部走y
另一种情况如果(x < y),那么也就是说要尽可能的多走树上的边,我们知道一个节点只能访问一次,也就是说
一个节点最多只能连两条边出去,然后dfs搜索,找到最多可以走多少条,每个节点的度数如果不被剪完就可以继续连,
剩下的只能走y。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
#include <stack>
#include <set>
#include <map>
#include <math.h>
#define pb push_back
#define CLR(a) memset(a, 0, sizeof(a));
#define MEM(a, b) memset(a, b, sizeof(a));
#define fi first
#define se second using namespace std; typedef long long ll; const int MAXN = ;
const int MAXV = ;
const int MAXE = ;
const int INF = 0x3f3f3f3f;
ll x, y, n;
struct Edge
{
int to, next;
Edge () {}
Edge(int to, int next) : to(to), next(next) {}
}edge[MAXN << ];
int num;
int head[MAXN];
void Add(int from, int to)
{
edge[num] = Edge(to, head[from]);
head[from] = num++;
}
int deg[MAXN];
ll ans = ;
ll len = ;
int cnt = ;
bool dfs(int crt, int fa)
{
int rem = ;
for (int t = head[crt]; t != -; t = edge[t].next)
{
Edge e = edge[t];
int v = e.to;
if (v == fa) continue;
if (dfs(v, crt) && rem > )
{
len++; rem--;
}
}
return rem > ;
} int main()
{
//freopen("in.txt", "r", stdin);
while (~scanf("%lld%lld%lld", &n, &x, &y))
{
MEM(head, -);
MEM(edge, -);
CLR(deg);
num = ;
len = ;
for (int i = ; i < n-; i++)
{
int u, v;
scanf("%d%d", &u, &v);
Add(u, v);
Add(v, u);
deg[u]++;
deg[v]++;
}
bool done = false;
if (x >= y)
{
for (int i = ; i <= n; i++)
{
if (deg[i] == n-)
{
ans = y*(n-)+x;
printf("%lld\n", ans);
done = true;
break;
}
}
if (done) continue;
ans = (n-)*y;
printf("%lld\n", ans);
continue;
}
dfs(, ); ans = len*x + (n--len)*y;
printf("%lld\n", ans);
}
return ;
}
CodeForces 618D Hamiltonian Spanning Tree的更多相关文章
- Codeforces 618D Hamiltonian Spanning Tree(树的最小路径覆盖)
题意:给出一张完全图,所有的边的边权都是 y,现在给出图的一个生成树,将生成树上的边的边权改为 x,求一条距离最短的哈密顿路径. 先考虑x>=y的情况,那么应该尽量不走生成树上的边,如果生成树上 ...
- 【19.27%】【codeforces 618D】Hamiltonian Spanning Tree
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- [Educational Round 3][Codeforces 609E. Minimum spanning tree for each edge]
这题本来是想放在educational round 3的题解里的,但觉得很有意思就单独拿出来写了 题目链接:609E - Minimum spanning tree for each edge 题目大 ...
- codeforces 609E Minimum spanning tree for each edge
E. Minimum spanning tree for each edge time limit per test 2 seconds memory limit per test 256 megab ...
- codeforces 609E. Minimum spanning tree for each edge 树链剖分
题目链接 给一个n个节点m条边的树, 每条边有权值, 输出m个数, 每个数代表包含这条边的最小生成树的值. 先将最小生成树求出来, 把树边都标记. 然后对标记的边的两个端点, 我们add(u, v), ...
- Codeforces 1133 F2. Spanning Tree with One Fixed Degree 并查集+生成树
好久没更新博客了,一直懒得动,这次更新一下. 题意大概是:给出一个图,求它的一个一号节点的度数恰好为D的生成树的方案. 一开始随便水了个乱搞贪心,不出意外并没有过. 仔细思考之后,对于这个问题我们可以 ...
- Codeforces Educational Codeforces Round 3 E. Minimum spanning tree for each edge LCA链上最大值
E. Minimum spanning tree for each edge 题目连接: http://www.codeforces.com/contest/609/problem/E Descrip ...
- Codeforces Educational Codeforces Round 3 E. Minimum spanning tree for each edge 树上倍增
E. Minimum spanning tree for each edge 题目连接: http://www.codeforces.com/contest/609/problem/E Descrip ...
- Educational Codeforces Round 3 E. Minimum spanning tree for each edge LCA/(树链剖分+数据结构) + MST
E. Minimum spanning tree for each edge Connected undirected weighted graph without self-loops and ...
随机推荐
- How to restrict root user to access or modify a file and directory in Linux
Now in this article I will show you steps to prevent or restrict access of root user to access certa ...
- wine卸载
Wine手动卸载,出现殘留,导致安装其他软件也不成功. 错误如下: 正在读取软件包列表... 完成正在分析软件包的依赖关系树 正在读取状态信息... 完成 您也许需要运行“ap ...
- Oracle 函数使用记录
持续更新…… 参考:https://www.cnblogs.com/bbliutao/archive/2017/11/08/7804263.html 1. ADD_MONTHS 语法: ADD_MON ...
- 学习JavaScript你必须掌握的8大知识点!
大知识点! 一.JavaScript思维导图之<变量>的学习 二. JavaScript思维导图之<函数基础> 三.JavaScript思维导图之<基本dom操作 ...
- 02Qt信号与槽(1)
信号与槽 1.概述 信号和槽机制是 Qt 的核心机制,信号和槽是一种高级接口,应用于对象之间的通信,它是 Qt 的核心特性,也是 Qt 区别于其他工具包的重要地方.信号和槽是 Qt 自行定义的一种 ...
- Python3 pymsyql 连接读取数据
import pymysql conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='root', db='t ...
- 七周成为数据分析师06_MySQL
关于 MySQL 的知识,主要也是一些实操和练习. 因为个人之前已经专门练习过 MySQL 操作,这里就不做笔记,之后另写一篇博文记录 MySQL 知识. 同时附上本课程对应的文字教程: 如何七周成为 ...
- ACM-ICPC 2018 徐州赛区网络预赛 B. BE, GE or NE
In a world where ordinary people cannot reach, a boy named "Koutarou" and a girl named &qu ...
- Python之code对象与pyc文件(一)
Python程序的执行过程 我们都知道,C语言在执行之前需要将源代码编译成可执行的二进制文件,也就是将源代码翻译成机器代码,这种二进制文件一旦生成,即可用于执行.但是,Python是否一样呢?或许很多 ...
- 文件上传下载,命令之wget / curl / which / sort / uniq / cut / wc /tr /sed
目录 命令 1.文件的上传下载 2.从外网下载文件wget 3.curl文件下载 4.查找命令which 5.字符处理命令-排序sort 6.字符处理-去重uniq 7.字符处理-截取cut 8.字符 ...