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

走和当前节点连接的下一个节点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. 面向对象OONo.3单元总结

    一,JML语言 1)JML理论基础:JML是一类语言,用来描述一个方法或一个类的功能.以及这个类在实现这个功能时需要的条件.可能改变的全局变量.以及由于条件问题不能实现功能时这个方法或类的行为,具有明 ...

  2. “CTL_CODE”未定义

    C4013 “CTL_CODE”未定义:假设外部返回 int 要加入 #include<winioctl.h> 并且要放在#include<windows.h>的后面

  3. 学习笔记(二):使用 TensorFlow 的起始步骤(First Steps with TensorFlow)

    目录 1.工具包 TensorFlow 张量 (Tensor) 图 (graph) TensorBoard 2.tf.estimator API Estimator 预创建的 Estimator (p ...

  4. Linux网络配置指令

    版权声明:本文为博主原创文章,未经博主允许不得转载. 原文地址: https://www.cnblogs.com/poterliu/p/6686799.html 重启网卡service network ...

  5. Python基础——异常

    捕捉所有异常 for i in range(10): try: input_number=input('write a number') if input_number=='q': break res ...

  6. python numpy模块

    目录 numpy模块 一维数组 二维数组(用的最多的) 获取多维数组的行和列 多维数组的索引 高级功能 多维数组的元素的替换 通过函数方法创建多维数组 矩阵的运算 点乘和转置(了解) 点乘必须 m*n ...

  7. STM32CUBEMX入门学习笔记1:软件的简单介绍

    STM32CUBEMX是ST公司设计的一款免费软件,软件可以通过其官网下载.现在已经下载到.通过STM32CUBEMX可以完成从单片机选型,程序初始化,中断配置……工作.并生成对应的"HAL ...

  8. POJ:1086-Parencodings

    Parencodings Time Limit: 1000MS Memory Limit: 10000K Description Let S = s1 s2-s2n be a well-formed ...

  9. BZOJ 2721: [Violet 5]樱花

    (X-N)(Y-N)=N^2 #include<cstdio> using namespace std; const int mod=1e9+7; int n,cnt,isprime[10 ...

  10. CodeForces 392C Yet Another Number Sequence 矩阵快速幂

    题意: \(F_n\)为斐波那契数列,\(F_1=1,F_2=2\). 给定一个\(k\),定义数列\(A_i=F_i \cdot i^k\). 求\(A_1+A_2+ \cdots + A_n\). ...