题面
??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={ V,E},|V|=n,|E|=m,满足m<n
??在一个节点打上标记可以覆盖与该节点相邻的边,记覆盖所有边需要标记的点数为x,边两端点都被标记的条数记为A (记为双端点覆盖数),求出最小的x,如果x 有相等的情况,求出最大的A
解法
树型
DP :
??这个问题大家应该很熟悉……求树的最小点覆盖,本题只是在此基础上加了一小问,我们可以利用同样的方法解决
??求树的最小点覆盖的时候,我们设fi,0为i节点的子树内部全部被覆盖,并且i 号结点打了标记的最小标记数,fi,1为i节点子树内……并且i 号结点没有打标记的最小标记数,那么有:
fk,0=∑x=sonkmin(fx,0,fx,1)
fk,1=∑x=sonkfx,0
??与此同理,我们设 ci,0 为 i 节点子树内部全部被覆盖,并且i 号节点打了标记的双端点覆盖数, ci,1 为 i 节点……并且i 节点没有打标记……,那么有:
ck,1=∑x=sonkcx,0
ck,0+=cx,0+1,fx,0<fx,1
ck,0+=cx,1,fx,0>fx,1
ck,0+=max(cx,0+1,cx,1),fx,0=fx,1
复杂度
O(T?n2)
代码
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#define Lint long long int
using 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];elseif( 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;
}