Codefroces 618D Hamiltonian Spanning Tree 树型动规
来源:互联网 发布:宾馆软件破解版 编辑:程序博客网 时间:2024/06/09 19:53
链接
Codefroces 618D Hamiltonian Spanning Tree
题意
权值均为y的完全图中部分边被权值为x的生成树取代, 求一条权值最小的哈密顿通路.
思路
嗯, 看了官方题解
很明显分两种情况考虑, 当 x>=y 时, 就尽量走y边, 而只有一种情况才使得需要走x边, 就是生成树的某一个节点的度数是 n-1, 这里的答案是比较好求的.
当 x<y 时, 就尽量走树上的边, 这里用dp来记录树上能够走的最多的路径, 最后添加不足的y边就可以了. 在树上记录路径要注意使得空出来的边补上 y 边后能够形成哈密顿通路, 所以对于每一个节点, 与子节点的连边情况只能是0条, 1条, 2条, 而且如果是连2条的话该节点就不能与父节点连边了. 这样的话用dp[i][j]表示节点i与他的子节点连j条边后该子树得到的最多边数. 方程很简单, 见代码中间. 全树上的边数就是 max(dp[1][j]), 答案就容易求得了.
代码
#define LL long long#define INF 0x3f3f3f3f#define MAXN 200005#include <bits/stdc++.h>using namespace std;vector<int> G[MAXN];bool vis[MAXN];int dp[MAXN][5];//dp[i][j]表示 i 节点到他的子节点连 j 条边后能得到最大的边数.int temp1, temp2;void dfs(int x, int father){ if (vis[x]) return; vis[x] = true; for (int i = 0; i < G[x].size(); ++i){ int son = G[x][i]; if (son == father) continue; dfs(son, x); temp1 = max(dp[son][0], dp[son][1]); temp2 = max(temp1, dp[son][2]); dp[x][2] = max(dp[x][1] + temp1 + 1, dp[x][2] + temp2); dp[x][1] = max(dp[x][1] + temp2, dp[x][0] + temp1 + 1); dp[x][0] += temp2; }}int main(){#ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout);#endif // ONLIE_JUDGE LL n, x, y; int p, q; scanf("%I64d%I64d%I64d", &n, &x, &y); for (int i = 1; i < n; ++i){ scanf("%d%d", &p, &q); G[p].push_back(q); G[q].push_back(p); } memset(vis, 0, sizeof(vis)); memset(dp, 0, sizeof(dp)); LL ans; if (x >= y){ bool flag = false; for (int i = 1; i <= n; ++i){ if (G[i].size() == n - 1){ flag = true; } } if (flag){ ans = ((n - 2) * y + x); } else{ ans = ((n - 1) * y); } } else{ dfs(1, 0); LL e = max(dp[1][0], max(dp[1][1], dp[1][2])); ans = (x * e + (n - 1 - e) * y); } printf("%I64d\n", ans);}
查看原文:http://www.macinchang.com/2016/01/30/548/
0 0
- Codefroces 618D Hamiltonian Spanning Tree 树型动规
- Codeforces 618 D Hamiltonian Spanning Tree
- codeforces 618D. Hamiltonian Spanning Tree
- 【19.27%】【codeforces 618D】Hamiltonian Spanning Tree
- CodeForces 618 D Hamiltonian Spanning Tree(树形DP)
- codeforces 618D Hamiltonian Spanning Tree(dfs)
- Codeforces 618 D Hamiltonian Spanning Tree 贪心+dp
- CodeForces 618 D.Hamiltonian Spanning Tree(dfs)
- Codeforces 618D Hamiltonian Spanning Tree【思维+Dfs】
- CF618D - Hamiltonian Spanning Tree
- Wunder Fund Round 2016 D. Hamiltonian Spanning Tree
- Wunder Fund Round 2016 D. Hamiltonian Spanning Tree(贪心+dp)
- Wunder Fund Round 2016 D. Hamiltonian Spanning Tree(贪心+dp)★ ★ ★
- 【Wunder Fund Round 2016 (Div 1 + Div 2 combined)D】【贪心 dfs】Hamiltonian Spanning Tree 树的最小链划分
- Spanning tree
- Spanning Tree
- spanning-tree
- sicilyOJ 11中大预选赛 D Minimum Labeled Spanning Tree
- 【Android】【笔记】《Android 入门经典》part 13 网络通信技术
- iOS swift sqlite使用,上传及定时更新
- C内存管理
- SACK信息
- 【Android】【笔记】《Android 入门经典》part 14 Service
- Codefroces 618D Hamiltonian Spanning Tree 树型动规
- TCP显示冲突通告(ECN)
- 集训队专题(2)1007 Evolution
- 【NYOJ】[463]九九乘法表
- JFinal+WebUploader实现图片的异步上传
- 第一讲、整数划分(回溯实现)
- 【JS】【笔记】JavaScript入门经典(第5版)第1章 概述
- [c++]TRACE 宏的利用
- 求一份 电力猫 QCA6410 原理图等