Uva 10859 Placing Lampposts(树状DP)
来源:互联网 发布:易语言数据比较 编辑:程序博客网 时间:2024/05/16 17:45
题意:给一个n个点m条边的无向无环图,在尽量少的节点上放灯,使得所有边都被照亮。每盏灯将照亮以他为一个端点的所有边,在灯的总数最小的前提下,被两盏灯同时照亮的边数尽量大
思路:放置的灯数a尽量少,被两盏灯同时照亮的边数应尽量大。后面那句话等价于恰好被一盏灯照亮的边数c尽量小。最终求 x = a * M + c的最小值。其中M是一个足够大的数,那么x / M就是放置的灯数,x % c就是被一盏灯照亮的灯数。
先将棵树转化为有根树,然后每个节点上只有放灯不放灯两种情况。当前节点放灯的话,子节点可放可不放;若不放灯则子节点必须放灯。
#include<cstdio>#include<cstring>#include<vector>#include<algorithm>const int INF = 1e9;const int maxn = 1e3 + 10;const int M = 5000;using namespace std;vector<int> G[maxn];vector<int> g[maxn];int dp[maxn][2];int vis[maxn];int T, n, m;void init() { for(int i = 0; i < maxn; i++) { G[i].clear(); g[i].clear(); } memset(vis, 0, sizeof vis); memset(dp, 0, sizeof dp);}void build_tree(int u) { vis[u] = 1; for(int i = 0; i < G[u].size(); i++) { int v = G[u][i]; if(vis[v]) continue; g[u].push_back(v); build_tree(v); }}void dfs(int u) { if(!g[u].size()) { dp[u][0] = 0; dp[u][1] = M; return ; } for(int i = 0; i < g[u].size(); i++) { int v = g[u][i]; dfs(g[u][i]); int cc = dp[v][0]; int dd = dp[v][1]; dp[u][0] += dp[v][1] + 1; dp[u][1] += min(cc + 1, dd); } dp[u][1] += M;}int main() { scanf("%d", &T); while(T--) { init(); scanf("%d %d", &n, &m); for(int i = 0; i < m; i++) { int u, v; scanf("%d %d", &u, &v); G[u].push_back(v); G[v].push_back(u); } int ans = 0; for(int i = 0; i < n; i++) { if(vis[i]) continue; build_tree(i); dfs(i); ans += min(dp[i][0], dp[i][1]); } printf("%d %d %d\n", ans / M, m - (ans % M), ans % M); } return 0;}
0 0
- UVA 10859 Placing Lampposts(树状DP)
- Uva 10859 Placing Lampposts(树状DP)
- Uva - 10859 - Placing Lampposts(树形dp)
- uva 10859 - Placing Lampposts(树形dp)
- UVA 10859 Placing Lampposts(树DP)
- UVA 10859 Placing Lampposts(树形DP)
- UVA--10859--Placing Lampposts--DP
- UVA 10859Placing Lampposts(树上的DP)
- *UVA 10859 Placing Lampposts ACM解题报告(dp)
- UVA 题目10859 Placing Lampposts(树形DP)
- Uva 10859 - Placing Lampposts 树形dp
- uva 10859 Placing Lampposts 树形dp
- uva 10859 Placing Lampposts (树形dp)
- UVa 10859 Placing Lampposts / 树形DP
- UVa:10859 Placing Lampposts(树形DP)
- Placing Lampposts - UVa 10859 树形dp
- UVA 10859 Placing Lampposts 树形dp(水
- uva 10859 Placing Lampposts 树形dp
- React+Webpack快速上手指南
- 手把手教大家编译debian源码以及安装系统---基于dragonboard410c
- jquery validation API
- 特别的分解。思考角度:数学中周长一定的矩形,长宽最为接近时面积最大。
- Unity5 多场景 打包Assetbundle 以及 Shader Stripping 导致 LightMap 全部丢失的解决方法
- Uva 10859 Placing Lampposts(树状DP)
- 数据库的关系表:一对多、多对多实例
- c/c++--归并排序
- 减少gradle时间
- h264编码速度质量参数设置
- 软件开发中涉及操作系统知识总结—IO模型
- FileZilla往本地虚拟机传输文件
- Swift项目引入第三方库的方法
- Android-学习-侧栏