【UVA10859】Placing Lampposts

来源:互联网 发布:我想学软件编程 编辑:程序博客网 时间:2024/06/05 13:22

题面

  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.

题意

  Graph={VE},|V|=n|E|=m,满足mn
  在一个节点打上标记可以覆盖与该节点相邻的边,记覆盖所有边需要标记的点数为x,边两端点都被标记的条数记为A(记为双端点覆盖数),求出最小的x,如果x有相等的情况,求出最大的A

解法

树型DP
  这个问题大家应该很熟悉……求树的最小点覆盖,本题只是在此基础上加了一小问,我们可以利用同样的方法解决
  求树的最小点覆盖的时候,我们设fi0i节点的子树内部全部被覆盖,并且i号结点打了标记的最小标记数,fi1i节点子树内……并且i号结点没有打标记的最小标记数,那么有:

fk0=x=sonkminfx0fx1

fk1=x=sonkfx0

  与此同理,我们设ci0i节点子树内部全部被覆盖,并且i号节点打了标记的双端点覆盖数,ci1i节点……并且i节点没有打标记……,那么有:
ck1=x=sonkcx0

ck0+=cx0+1fx0<fx1

ck0+=cx1fx0>fx1

ck0+=maxcx0+1cx1fx0=fx1

复杂度

O(Tn2

代码

#include<iostream>#include<cstdlib>#include<cstring>#include<cstdio>#define Lint long long intusing namespace std;const int MAXN=1010;struct node{    int next,to;}t[MAXN*2];bool vis[MAXN];int head[MAXN],num;int f[MAXN][2],c[MAXN][2];int T,n,m,ans;int A1,A2;void add(int u,int v){    t[++num]=(node){ head[u],v };    head[u]=num;}void dfs(int k,int fa){    vis[k]=1;    f[k][0]=1,f[k][1]=0;    c[k][0]=0,c[k][1]=0;    for(int i=head[k],x; i ;i=t[i].next)    {        x=t[i].to;        if( x==fa)   continue ;        dfs( x,k );        if( f[x][0]<f[x][1] )   f[k][0]+=f[x][0],c[k][0]+=c[x][0]+1;        else        {            f[k][0]+=f[x][1];            if( f[x][0]>f[x][1] )   c[k][0]+=c[x][1];            else   c[k][0]+=max( c[x][1],c[x][0]+1 );        }        f[k][1]+=f[x][0],c[k][1]+=c[x][0];    }}int main(){    int u,v;    scanf("%d",&T);    while( T-- )    {        A1=A2=ans=num=0;        memset( c,0x0,sizeof c );        memset( f,0x0,sizeof f );        memset( vis,0x0,sizeof vis );        memset( head,0x0,sizeof head );        scanf("%d%d",&n,&m);        for(int i=1;i<=m;i++)        {            scanf("%d%d",&u,&v);            u++,v++;            add( u,v ),add( v,u );        }        for(int i=1;i<=n;i++)            if( !vis[i] )            {                dfs( i,0 );                ans+=min( f[i][0],f[i][1] );                if( f[i][0]<f[i][1] )   A1+=c[i][0];                else                    if( f[i][0]>f[i][1] )   A1+=c[i][1];                    else   A1+=max( c[i][0],c[i][1] );            }        A2=m-A1;        printf("%d %d %d\n",ans,A1,A2);    }    return 0;}
原创粉丝点击