题意:要把所有的节点都访问一次,并且不能重复访问,有两种方式访问,一种是根据树上的路径

走和当前节点连接的下一个节点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的更多相关文章

  1. Codeforces 618D Hamiltonian Spanning Tree(树的最小路径覆盖)

    题意:给出一张完全图,所有的边的边权都是 y,现在给出图的一个生成树,将生成树上的边的边权改为 x,求一条距离最短的哈密顿路径. 先考虑x>=y的情况,那么应该尽量不走生成树上的边,如果生成树上 ...

  2. 【19.27%】【codeforces 618D】Hamiltonian Spanning Tree

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  3. [Educational Round 3][Codeforces 609E. Minimum spanning tree for each edge]

    这题本来是想放在educational round 3的题解里的,但觉得很有意思就单独拿出来写了 题目链接:609E - Minimum spanning tree for each edge 题目大 ...

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

  5. codeforces 609E. Minimum spanning tree for each edge 树链剖分

    题目链接 给一个n个节点m条边的树, 每条边有权值, 输出m个数, 每个数代表包含这条边的最小生成树的值. 先将最小生成树求出来, 把树边都标记. 然后对标记的边的两个端点, 我们add(u, v), ...

  6. Codeforces 1133 F2. Spanning Tree with One Fixed Degree 并查集+生成树

    好久没更新博客了,一直懒得动,这次更新一下. 题意大概是:给出一个图,求它的一个一号节点的度数恰好为D的生成树的方案. 一开始随便水了个乱搞贪心,不出意外并没有过. 仔细思考之后,对于这个问题我们可以 ...

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

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

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

随机推荐

  1. docker安装gitlab-ce

    pull and run docker pull docker.io/gitlab/gitlab-ce docker run -itd --name gitlab -p 10080:80 gitlab ...

  2. mysql中添加数据时,报错(incorrect string value:'\xf0\x9f ) 字符转换不正确

    这个问题,原因是UTF-8编码有可能是两个.三个.四个字节.Emoji表情或者某些特殊字符是4个字节,而Mysql的utf8编码最多3个字节,所以数据插不进去. 在网上搜了一下解决问题的方案,我选了一 ...

  3. Python——函数基础

    函数是什么 它相当于一个独立的代码块,可以被重复使用,如果需要增加一个功能或者修改一个功能,只需要,增加或者修改函数即可. 函数分类 内置函数 python解释器已经为我们定义好的参数,比如:len( ...

  4. Bzoj 近期题目一句话题解

    目录 Bzoj 近期题目题解 1000: A+B Problem (模拟) 1008: [HNOI2008]越狱 (容斥) 1012: [JSOI2008]最大数maxnumber (线段树) 103 ...

  5. Linux菜鸟起飞之路【九】系统启动流程

    Linux系统启动流程 BIOS -> MBR  -> BootLoader -> Kernel -> init 1.打开电源后,计算机从主板的BIOS中读取其中存储的程序.这 ...

  6. Kubernetes的主要功能

    Kubernetes的主要功能 1.数据卷   Pod中容器之间共享数据,可以使用数据卷. 2.应用程序健康检查   容器内服务可能进程堵塞无法处理请求,可以设置监控检查策略保证应用健壮性. 3.复制 ...

  7. Primer C++第五版 读书笔记(一)

    Primer C++第五版 读书笔记(一) (如有侵权请通知本人,将第一时间删文) 1.1-2.2 章节 关于C++变量初始化: 初始化不是赋值,初始化的含义是创建变量时赋予其一个初始值,而赋值的含义 ...

  8. AOP面向切面编程笔记

    1.AOP概念:Aspect Oriented Programming 面向切面编程 2.作用:本质上来说是一种简化代码的方式 继承机制 封装方法 动态代理 …… 3.情景举例 ①数学计算器接口[Ma ...

  9. Divisibility by 25 CodeForces - 988E

    You are given an integer nn from 11 to 10181018 without leading zeroes. In one move you can swap any ...

  10. 做ios工程时,把UI从xib移动到代码中遇到的问题

    由于四期要做多语言版本,带xib页面的工程做多语言版本比较麻烦,再加上现在已经习惯了代码中的viewdidload函数中初始化控件,所以就把两个页面从xib移到代码中去了. 在修改后加载页面会遇到ba ...