Placing 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 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


题目大意:给定一个无向无环图,要求在点上放灯,如果某一点上放了等,则可以照亮与它相通的边,现在要求放尽量少得等,使得所有边都被照亮,并且输出灯数,被照亮两次的边数(即边的两个端点均放置灯),被照亮一次的边。 如果等数一样的话,按照被照亮一次边越大的方案。

解题思路:

优化目标有两个,使得灯数a尽量少,使得被两个灯的边数b尽量多

在这里可以转化一下,就是恰被一盏灯照亮的边数c尽量少

<IMPORTANT>当我们同时需要优化两个变量a,c,要求首先满足a最小的情况下,使得c尽量小

我们可以引入一个值x=Ma+c,在这里的M是一个很大的值,是一个比c的理论最大值和a的最小理论值之差还要大的值

为什么?因为如果两个a不同的,无论c相差多少,仍然是a起决定性的作用

则我们的目标就是使x的值尽量小,那么x/M的整数部分就是灯数,x%M就是只被一个灯照亮的,m-x%M就是被两个灯照亮的

我们令dp[i][j]表示在i节点的x的最小值(j=1表示父亲节点放灯,0则反之)

那么对i来说,有两个决策

如果在i放灯

那么dp[i][j] = sum(dp[k][1])+M,k是i的子节点。如果j=0且i不是根,还要+1,因为i和他的父亲节点这条边只被一个灯照亮

如果在i不放灯,必须j=1(不然边就无法照亮)

那么dp[i][j] = sum(dp[k][0]),如果i不是根,还要加1,因为i和他的父亲节点这条边只被一个灯照亮

通过边DFS边DP的思路就可以把结果求出来

另外图可能有多个连通分量

代码如下:

#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#define Size 1005
#define M 2000
using namespace std; int d[Size][];
bool vis[Size][];
vector<int> edge[Size];
int n,m; int dp(int i,int j,int f){
if(vis[i][j]==true)return d[i][j];
vis[i][j]=true; int k=edge[i].size(); //yes
int& ans=d[i][j];
ans=M;
for(int ch=;ch<k;ch++){
if(edge[i][ch]!=f){
ans+=dp(edge[i][ch],,i);
}
}
if(j==&&f!=-)ans++; //no
if(j== || f==-){
int ans2=;
for(int ch=;ch<k;ch++){
if(edge[i][ch]!=f){
ans2+=dp(edge[i][ch],,i);
}
}
if(f!=-)ans2++; ans=min(ans,ans2);
} return ans;
} int main(){
freopen("30.in","r",stdin); int T; cin>>T;
while(T--){
for(int i=;i<=n;i++)edge[i].clear();
memset(vis,false,sizeof(vis)); cin>>n>>m;
int a,b;
for(int i=;i<m;i++){
cin>>a>>b;
edge[a].push_back(b);
edge[b].push_back(a);
} int ans=;
for(int i=;i<n;i++){
if(vis[i][]==false){
ans+=dp(i,,-);//-1表示无父节点 即 i为根节点
}
} cout<<ans/M<<' ' <<m-ans%M<<' '
<<ans%M<<endl;
} fclose(stdin);
return ;
}

UVa10859 放置街灯的更多相关文章

  1. 10_放置街灯(Placing Lampposts,UVa 10859)

    问题来源:刘汝佳<算法竞赛入门经典--训练指南> P70 例题30: 问题描述:有给你一个n个点m条边(m<n<=1000)的无向无环图,在尽量少的节点上放灯,使得所有边都被照 ...

  2. UVA - 10859 Placing Lampposts 放置街灯

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

  3. 「算法笔记」树形 DP

    一.树形 DP 基础 又是一篇鸽了好久的文章--以下面这道题为例,介绍一下树形 DP 的一般过程. POJ 2342 Anniversary party 题目大意:有一家公司要举行一个聚会,一共有 \ ...

  4. UVa10895 Placing Lampposts

    UVa10895 Placing Lampposts 链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34290 [思路] ...

  5. Spring环境搭建之:导入jar包、配置文件名称及放置位置

    Spring环境搭建之:导入jar包.配置文件名称及放置位置 现在项目开发中spring框架应用的还是比较多的,自己用的还不太熟练,每次用的时候总配置半天,总有些配置弄错,就找个时间总结以下,方便以后 ...

  6. ArcGIS ElementLayer上放置Windows控件

    ElementLayer是ArcGIS API for Silverlight/WPF中的一种图层类型,主要用来承载Silverlight/WPF中的UIElement对象(UIElement),使用 ...

  7. JavaScript放置位置区别

    JavaScript放置位置区别 页面中的脚本会在页面载入浏览器后立即执行.我们并不总希望这样.有时,我们希望当页面载入时执行脚本,而另外的时候,我们则希望当用户触发事件时才执行脚本. 位于 head ...

  8. cadence学习之——原理图库的添加及器件的放置

    画原理图,库是必不可少的,库有cadence自带的,也可以自己建自己的库,然后在画原理图工程时, 这些库都需要被添加进原理图工程才能使用. 1.库的添加 打开Place Part属性框,操作如下: ( ...

  9. 使用Fragment应用放置后台很久,被系统回收,出现crash

    使用Fragment应用放置后台很久,被系统回收,出现crash:原因:系统做了源码FragmentActivity调用onSaveInstanceState保存Fragment对象,这时候系统恢复保 ...

随机推荐

  1. jeecg中service中注入jdbc的注解

    @Resource private JdbcTemplate jdbcTemplate;

  2. (转)Inno Setup入门(十五)——Inno Setup类参考(1)

    本文转载自:http://blog.csdn.net/yushanddddfenghailin/article/details/17250955 nno setup脚本能够支持许多的类,这些类使得安装 ...

  3. idea 注册码 地址:

    http://idea.lanyus.com IntelliJ IDEA 注册码 *.lanyus.com及*.qinxi1992.cn下的全部授权服务器已遭JetBrains封杀 请搭建自己的Int ...

  4. SpringMVC之五:自定义DispatcherServlet配置及配置额外的 servlets 和 filters

    相关文章 <Servlet3.0之四:动态注册和Servlet容器初始化> <SpringBoot中通过SpringBootServletInitializer如何实现组件加载> ...

  5. Go - 基础知识

    经历了五一小假期,前后差不多一周多没有坚持学习了,所以在归来的第一时间继续 Go 的学习之旅. Go 程序的基本结构 首先先贴出一段简单的代码:HelloGo.go // HelloGo packag ...

  6. Go - coding之前的准备

    Go tool 的使用 Go的tool要求我们对于code有一定的结构化组织和管理,下面我们就来一介绍他们: --GoPath environment variable:  顾名思义,环境变量,指定了 ...

  7. length length()

    数组长度 length String 长度 length()

  8. java 等额本金与等额本息

    等额本金与等额本息 等本等息的意思是,每月的本金相等,利息也相等. 等额本息的意思是,每月的本金+利息之和相等(其实每个月本金和利息都有变化,并不相等) 等本等息的意思是,每月的本金相等,利息不等. ...

  9. centos7.3安装python3.6.5

    最近在玩django,想部署个网站试试,结果发现线上默认的centos用得居然是python2.7.5,那么先升级下吧,到python3.6.5 yum安装时python2.7.5 那么编译安装吧 那 ...

  10. rabbitMQ消息队列1

    rabbitmq 消息队列 解耦 异步 优点:解决排队问题 缺点: 不能保证任务被及时的执行 应用场景:去哪儿, 同步 优点:保证任务被及时的执行 缺点:排队问题 大并发 Web nginx 1000 ...