10859 - Placing Lampposts(树形dp)

来源:互联网 发布:腾讯视频mac版有吗 编辑:程序博客网 时间:2024/05/21 19:49


Output: Standard Out

Next Generation Contest 1 

Time Limit: 2 seconds

Problem DPlacing Lampposts

As 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

2
4 3
0 1
1 2
2 3

5 4
0 1
0 2
0 3
0 4

Sample Output

2 1 2
1 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;}


1 0