题面

  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=Graph={V,EV,E},|V|=n|V|=n,|E|=m|E|=m,满足m<nm<n
  在一个节点打上标记可以覆盖与该节点相邻的边,记覆盖所有边需要标记的点数为xx,边两端点都被标记的条数记为AA(记为双端点覆盖数),求出最小的xx,如果xx有相等的情况,求出最大的AA

解法

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

fk,0=∑x=sonkmin(fx,0,fx,1)

f_{k,0}=\sum_{x=son_k} min(f_{x,0},f_{x,1})

fk,1=∑x=sonkfx,0

f_{k,1}=\sum_{x=son_k} f_{x,0}
  与此同理,我们设ci,0c_{i,0}为ii节点子树内部全部被覆盖,并且ii号节点打了标记的双端点覆盖数,ci,1c_{i,1}为ii节点……并且ii节点没有打标记……,那么有:

ck,1=∑x=sonkcx,0

c_{k,1}=\sum_{x=son_k} c_{x,0}

ck,0+=cx,0+1,fx,0<fx,1

c_{k,0}+=c_{x,0}+1,f_{x,0}

ck,0+=cx,1,fx,0>fx,1

c_{k,0}+=c_{x,1},f_{x,0}>f_{x,1}

ck,0+=max(cx,0+1,cx,1),fx,0=fx,1

c_{k,0}+=max(c_{x,0}+1,c_{x,1}),f_{x,0}=f_{x,1}

复杂度

O(T∗n2T*n^2)

代码

#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;
}

【UVA10859】Placing Lampposts相关推荐

  1. UVA10859 放置街灯 Placing Lampposts(树状DP)

    UVA10859 放置街灯 Placing Lampposts(树状DP) 这道题有两种解决方法,因为原图保证无重边无环无自环, 所以原图一定是一颗树(或森林).,都是树状DP,但是实现的过程大同小异 ...

  2. Placing Lampposts UVA - 10859 放置街灯 树形dp

    As a part of the mission 'Beautification of Dhaka City', the government has decided to replace all t ...

  3. UVA - 10859 Placing Lampposts 放置街灯

    Placing Lampposts 传送门:https://vjudge.net/problem/UVA-10859 题目大意:给你一片森林,要求你在一些节点上放上灯,一个点放灯能照亮与之相连的所有的 ...

  4. Nodejs【单机】多进程模式集群

    Nodejs[单机]多进程模式集群实例: 1.安装:npm install -s cluster 2.服务代码: var debug = require('debug'); var express = ...

  5. 【sql】leetcode习题 (共 42 题)

    [175]Combine Two Tables (2018年11月23日,开始集中review基础) Table: Person +-------------+---------+ | Column ...

  6. oracle软件静默安装程序,【oracle】静默安装 oracle 11gr2

    [序言] oracle 提供了静默安装方法在不适用图形界面的情况下安装 oracle 软件 ,创建db,配置netca,快速完成oracle 的部署. 在以下情形中可以使用静默安装 a OUI 的 G ...

  7. 【英语学习】【WOTD】ecstatic 释义/词源/示例

    文章目录 Podcast ecstatic *adj.* [ek-STAT-ik] Definition Did You Know? Examples Podcast ecstatic podcast ...

  8. UVA665 LA5658 False coin【暴力】

    The "Gold Bar" bank received information from reliable sources that in their last group of ...

  9. 【译】Vertical-Align: All You Need To Know

    原文地址:Vertical-Align: All You Need To Know Often I need to vertically align elements side by side. 我经 ...

最新文章

  1. linux shell ls 输出存进数组变量
  2. 记一些茅塞顿开的事情
  3. CF650E-Clockwork Bomb【并查集】
  4. [USACO 2017 Feb Gold] Tutorial
  5. 每年一波FPGA系列新品,这次Achronix专为AI/ML应用打造……
  6. python包和目录有什么不同_python模块和包的区别
  7. 将一个16进制数转化为10进制数
  8. Spring: Spring 从xml获取bean
  9. mysql 在线语法检查工具_「mysql 管理工具」五大开源MySQL管理工具! - seo实验室
  10. 各国语言缩写以及国际域名缩写
  11. smss,lsass, http://laji.xrlyy.com病毒处理
  12. 不常用SQL语句整理
  13. java产品经理_产品经理必懂的技术那点事儿:成为全栈产品经理
  14. 这些响应式网页测试工具确保你的设计万无一失
  15. MATLAB中的CVX包使用中的错误:Cannot perform the operation: {convex} .* {convex}
  16. 似然函数与贝叶斯公式
  17. linux完整备份nand,arm-linux东东之nand之4:nand_command
  18. 网络兼职圈套你知道多少?拓商提醒您谨防被骗
  19. Macy‘s Thanksgiving Day Parade
  20. 记录一次京东面试吧,面试的是虚拟平台的经验.

热门文章

  1. Wxpython pannel切换
  2. 群同态和群同构的区别_同构和同态有什么区别,它们可以用在哪些方面?
  3. 【数据仓库】数据仓库的介绍
  4. 跟未名学Office - PPT操作:高效
  5. 学生结构体,学生有姓名 学号 三门成绩 班级人数为五人 。实现 1:第一门成绩的平均分;2:找出两门以上不及格的学生,输出他们的姓名学号及三门成绩; 3:找出平均分在90分以上或者全部成绩在85分以上
  6. Leet_Code_1
  7. nacos配置中心[NACOS HTTP-GET] The maximum number of tolerable server reconnection errors has been reache
  8. 密码学的安全性浅析-3
  9. Auto CAD2004完全笔记
  10. 顶级程序员书单系列二:《编码-隐匿在计算机软硬件背后的语言》