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. 微软Azure平台 cloud service动态申请证书并绑定证书碰到的坑

    我们有一个saas平台 部分在azure的cloud service 使用lets encrypt来申请证书.每一个商家申请域名之后就需要通过Lets encrypt来得到证书并绑定证书. 主要碰到的 ...

  2. 在Google的GKE上创建支持Internal Load Balancer的Service

    在Google的Kubernetes Engine上发布service,可以采用除On-Promise相同的Cluster IP和NodePort两种方式外,还可以创建LoadBalaner的Serv ...

  3. golang的sync包例子

    package main import ( "fmt" "sync" ) var wg sync.WaitGroup func asyncTestFunc() ...

  4. FPGA能代替CPU架构吗?

    你还没听过FPGA?那你一定是好久没有更新自己在企业级IT领域的知识了.今天笔者就和大家聊聊何为FPGA?FPGA主要应用场景是什么?有人说FPGA是替代传统CPU和GPU的未来,你信吗? FPGA全 ...

  5. C程序花括号嵌套层次统计(新)

    [问题描述] 编写程序,统计给定的C源程序中花括号的最大嵌套层次,并输出花括号嵌套序列,该程序没有语法错误. 注意:1)源程序注释(/* … */)中的花括号应被忽略,不参与统计.2)源程序中的字符串 ...

  6. struts2学习(6)自定义拦截器-登录验证拦截器

    需求:对登录进行验证,用户名cy 密码123456才能登录进去:  登录进去后,将用户存在session中: 其他链接要来访问(除了登录链接),首先验证是否登录,对这个进行拦截: com.cy.mod ...

  7. Appium ios新的定位方式FindsByIosNSPredicate (没有试 先记录在这里) 有个 driver.find_element_by_ios_uiautomation() 研究下 ios的定位

    这个定位方式需要用java-client -5.0.版本,4.x的版本没有这个定位方式 //输入账号和密码 driver.findElementByIosNsPredicate("value ...

  8. java代码------计算器

    总结:我用if()语句写计算功能的代码时,实现不了,与switch_-catch语句不一样.不知到怎么实现 package com.p; import javax.swing.*; import ja ...

  9. 手机页面开发的meta

    <meta name="viewport" content="width=device-width, initial-scale=1"> 手机环境. ...

  10. 【UVa】1374 Power Calculus(IDA*)

    题目 题目     分析 IDA*大法好,抄了lrj代码.     代码 #include <cstdio> #include <cstring> #include <a ...