10859 - Placing Lampposts(树形dp)
来源:互联网 发布:腾讯视频mac版有吗 编辑:程序博客网 时间:2024/05/21 19:49
Output: Standard Out
Time Limit: 2 seconds
Problem DPlacing LamppostsAs a part of the mission �Beautification of Dhaka City�, the government has decided to replace all the old lampposts with new expensive ones. Since the new ones are quite expensive and the budget is not up to the requirement, the government has decided to buy the minimum number of lampposts required to light the whole city.
Dhaka city can be modeled as an undirected graph with no cycles, multi-edges or loops. There are several roads and junctions. A lamppost can only be placed on junctions. These lampposts can emit light in all the directions, and that means a lamppost that is placed in a junction will light all the roads leading away from it.
The �Dhaka City Corporation� has given you the road map of Dhaka city. You are hired to find the minimum number of lampposts that will be required to light the whole city. These lampposts can then be placed on the required junctions to provide the service. There could be many combinations of placing these lampposts that will cover all the roads. In that case, you have to place them in such a way that the number of roads receiving light from two lampposts is maximized.
Input
There will be several cases in the input file. The first line of input will contain an integer T(T<=30) that will determine the number of test cases. Each case will start with two integers N(N<=1000) and M( M<N) that will indicate the number of junctions and roads respectively. The junctions are numbered from 0 to N-1. Each of the next M lines will contain two integersa and b, which implies there is a road from junction a to b,
( 0<= a,b < N ) and a != b. There is a blank line separating two consecutive input sets.
Output
For each line of input, there will be one line of output. Each output line will contain 3 integers, with one space separating two consecutive numbers. The first of these integers will indicate the minimum number of lampposts required to light the whole city. The second integer will be the number of roads that are receiving lights from two lampposts and the third integer will be the number of roads that are receiving light from only one lamppost.
Sample Input
24 3
0 1
1 2
2 3
5 4
0 1
0 2
0 3
0 4
Sample Output
2 1 21 0 4题意:一个无向图上,要去放灯,要求每条边都被照亮的最少灯数,并且1边被两盏灯照亮的边数要尽量多,输出灯数,两盏照亮的边数,一盏照亮的边数。
思路:树形dp,dp[u][0]代表点u不放灯,dp[u][1]代表点u放灯,那么如果不放灯那前一个结点必然要放灯,不然之间的边将不能照亮dp[u][0] = dp[v][1];
如果放灯,那么就看之前放灯和不放灯那个更小dp[u][1] = min(dp[v][0], dp[v][1]);然后把所有子树都加起来即可。两盏灯的转移方法类似,如果u和v都放,那么多+1即可,要注意相等的情况。
代码:
#include <stdio.h>#include <string.h>#include <vector>#define max(a,b) ((a)>(b)?(a):(b))#define min(a,b) ((a)<(b)?(a):(b))using namespace std;const int N = 1005;int t, n, m, dp[N][2], cnt[N][2], vis[N];vector<int> g[N];void dfs(int u) {dp[u][0] = 0;dp[u][1] = 1;cnt[u][0] = 0;cnt[u][1] = 0;vis[u] = 1;for (int i = 0; i < g[u].size(); i++) {int v = g[u][i];if (vis[v]) continue;dfs(v);dp[u][0] += dp[v][1];cnt[u][0] += cnt[v][1];if (dp[v][0] > dp[v][1]) {dp[u][1] += dp[v][1];cnt[u][1] += cnt[v][1] + 1;}else {dp[u][1] += dp[v][0];if (dp[v][0] < dp[v][1]) {cnt[u][1] += cnt[v][0];}else {cnt[u][1] += max(cnt[v][0], cnt[v][1] + 1);}}}}int main() {scanf("%d", &t);while (t--) {memset(vis, 0, sizeof(vis));memset(g, 0, sizeof(g));scanf("%d%d", &n, &m);int i, u, v;for (i = 0; i < m; i++) {scanf("%d%d", &u, &v);g[u].push_back(v);g[v].push_back(u);}int ans1 = 0, ans2 = 0;for (i = 0; i < n; i++) {if (vis[i]) continue;dfs(i);if (dp[i][0] > dp[i][1]) {ans1 += dp[i][1];ans2 += cnt[i][1];}else {ans1 += dp[i][0];if (dp[i][0] < dp[i][1]) {ans2 += cnt[i][0];}else {ans2 += max(cnt[i][0], cnt[i][1]);}}}printf("%d %d %d\n", ans1, ans2, m - ans2);}return 0;}
- 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:10859 Placing Lampposts(树形DP)
- Placing Lampposts - UVa 10859 树形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)
- 【动态规划】【树形DP】[UVa 10859]Placing Lampposts
- UVA 题目10859 Placing Lampposts(树形DP)
- Light oj 1230 - Placing Lampposts(树形dp)
- 使用投影计算两条直线交点
- SQL盲注攻击技术综述
- 寒假训练的小总结
- 雅芳涉嫌贿赂中国官员 或面临1.32亿美元罚款
- 微软面试100题系列-设计包含 min 函数的栈
- 10859 - Placing Lampposts(树形dp)
- ios项目设置arc和非arc
- 微软面试100题系列-数组连续和最大
- SRM 606 DIV1 450
- Permutation Sequence
- Linux的回环接口lo
- NSString属性声明中的copy和retain区别
- 理解autorelease
- 锻造模拟:接触+弹塑性分析