首页 > 代码库 > Placing Lampposts - UVa 10859 树形dp

Placing Lampposts - UVa 10859 树形dp

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 integers a 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

 

题意:有n个点和m条街,每个点上可以装一个路灯,每条街都必须在两端至少有一个灯,问至少需要多少个灯,如果灯数相同的情况下,最多有多少条街可以在两端都有路灯。

思路:简单的树形dp,就是本题可能不止是一棵树。dp[u][0 1][k]表示在u节点不安装/安装路灯的情况,dp[u][k][0 1]表示u节点及其以下的街都满足要求是最少的路灯数和最大的两端都有灯的街数。

AC代码如下:

 

[cpp] view plaincopy技术分享技术分享
    1. #include<cstdio>  
    2. #include<cstring>  
    3. #include<vector>  
    4. using namespace std;  
    5. int n,m,dp[1010][2][2],vis[1010];  
    6. vector<int> vc[1010];  
    7. void dfs(int u,int f)  
    8. {  
    9.     vis[u]=1;  
    10.     dp[u][1][0]=1;  
    11.     int i,j,k,len=vc[u].size(),v;  
    12.     for(i=0;i<len;i++)  
    13.     {  
    14.         v=vc[u][i];  
    15.         if(v==f)  
    16.           continue;  
    17.         dfs(v,u);  
    18.         dp[u][0][0]+=dp[v][1][0];  
    19.         dp[u][0][1]+=dp[v][1][1];  
    20.   
    21.         if(dp[v][0][0]<dp[v][1][0])  
    22.         {  
    23.             dp[u][1][0]+=dp[v][0][0];  
    24.             dp[u][1][1]+=dp[v][0][1];  
    25.         }  
    26.         else if(dp[v][0][0]>dp[v][1][0])  
    27.         {  
    28.             dp[u][1][0]+=dp[v][1][0];  
    29.             dp[u][1][1]+=dp[v][1][1]+1;  
    30.         }  
    31.         else  
    32.         {  
    33.             dp[u][1][0]+=dp[v][0][0];  
    34.             dp[u][1][1]+=max(dp[v][0][1],dp[v][1][1]+1);  
    35.         }  
    36.     }  
    37. }  
    38. int main()  
    39. {  
    40.     int T,t,i,j,k,u,v,a,b,c;  
    41.     scanf("%d",&T);  
    42.     for(t=1;t<=T;t++)  
    43.     {  
    44.         scanf("%d%d",&n,&m);  
    45.         for(i=0;i<n;i++)  
    46.            vc[i].clear();  
    47.         for(i=1;i<=m;i++)  
    48.         {  
    49.             scanf("%d%d",&u,&v);  
    50.             vc[u].push_back(v);  
    51.             vc[v].push_back(u);  
    52.         }  
    53.         memset(dp,0,sizeof(dp));  
    54.         memset(vis,0,sizeof(vis));  
    55.         a=0;b=0;c=0;  
    56.         for(i=0;i<n;i++)  
    57.            if(vis[i]==0)  
    58.            {  
    59.                dfs(i,-1);  
    60.                if(dp[i][0][0]<dp[i][1][0])  
    61.                  a+=dp[i][0][0],b+=dp[i][0][1];  
    62.                else if(dp[i][0][0]>dp[i][1][0])  
    63.                  a+=dp[i][1][0],b+=dp[i][1][1];  
    64.                else  
    65.                  a+=dp[i][0][0],b+=max(dp[i][0][1],dp[i][1][1]);  
    66.             }  
    67.         printf("%d %d %d\n",a,b,m-b);  
    68.     }  

Placing Lampposts - UVa 10859 树形dp