传送门

Description

给定一个\(n\)个点\(m\)条边的无向无环图,选择尽量少的节点,使得所有边都至少有一个顶点被选择。在这个基础上,要求有两个顶点被选择的边数尽可能大

Input

多组数据。第一行是数据组数\(T\)。

以下\(T\)组,每组包括:

第一行两个整数\(n\),\(m\)。

下面\(m\)行,每行两个整数\(u\),\(v\)。代表一条边。

Output

对于每组数据输出一行,包括三个用空格隔开的整数,分别是:

最小的灯的个数,两个顶点都被选择的边数,一个顶点被选择的边数

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

Hint

\(For~All:\)

\(m~<~n~\leq~1000\)

Solution

考虑无向无环图本质上是个森林,各个树互不影响。于是下面只研究单棵树的情况。

考虑这个题的优化目标有两个,分别是要求点数尽可能少,还有两个顶点被选择的边尽可能多。为了统一取max和min,我们将目标二改为有且仅有一个顶点被选择的边尽可能少。

对于同时最小化两个目标,而且在第一个目标最小的时候需要最小化第二个目标的的时候,可以设第一个目标的值是\(x_1\),第二个目标的值是\(x_2\)。目标为最小化\(ans=x_1~\times~K+x_2\),其中满足\(K~>~max_{x_2}\)。

于是对于本题就可以套这种方法。由于\(n~\leq~1000\),所以不妨设\(K=2000\)。设\(f_{i,0/1}\)为以\(i\)为根的子树合法,且点\(i\)不选/选的答案。

方程显然:

\[f_{i,0}~=~\sum\{f_{to,1}+1\}
\]

\[f_{i,1}~=~\sum~min~\{f_{to,1}~,~f_{to,0}+1\}+k
\]

于是就没了

Code

#include<cstdio>
#include<cstring>
#define rg register
#define ci const int
#define cl const long long int typedef long long int ll; namespace IO {
char buf[90];
} template<typename T>
inline void qr(T &x) {
char ch=getchar(),lst=' ';
while(ch>'9'||ch<'0') lst=ch,ch=getchar();
while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
if(lst=='-') x=-x;
} template<typename T>
inline void write(T x,const char aft,const bool pt) {
if(x<0) x=-x,putchar('-');
int top=0;
do {
IO::buf[++top]=x%10+'0';
x/=10;
} while(x);
while(top) putchar(IO::buf[top--]);
if(pt) putchar(aft);
} template<typename T>
inline T mmax(const T a,const T b) {return a > b ? a : b;}
template<typename T>
inline T mmin(const T a,const T b) {return a < b ? a : b;}
template<typename T>
inline T mabs(const T a) {return a < 0 ? -a : a;} template<typename T>
inline void mswap(T &a,T &b) {
T temp=a;a=b;b=temp;
} const int st = 2000;
const int maxn = 1010;
const int maxm = 2010; struct Edge {
int to,nxt;
};
Edge edge[maxm];int hd[maxn],ecnt;
inline void cont(ci from,ci to) {
Edge &e=edge[++ecnt];
e.to=to;e.nxt=hd[from];hd[from]=ecnt;
} int n,m;
int frog[maxn][2];
bool vis[maxn]; void clear();
void reading();
void dfs(ci,ci); int main() {
rg int t=0;qr(t);
while(t--) {
clear();
qr(n);qr(m);
rg int _ans=0;
reading();
for(rg int i=1;i<=n;++i) if(!vis[i]) {
dfs(i,0);_ans+=mmin(frog[i][0],frog[i][1]);
}
rg int tk=_ans%st;
write(_ans/st,' ',true);write(m-tk,' ',true);write(tk,'\n',true);
}
return 0;
} void clear() {
n=m=ecnt=0;
memset(hd,0,sizeof hd);
memset(vis,0,sizeof vis);
memset(edge,0,sizeof edge);
memset(frog,0,sizeof frog);
} void reading() {
rg int a,b;
for(rg int i=1;i<=m;++i) {
a=b=0;qr(a);qr(b);++a,++b;
cont(a,b);cont(b,a);
}
} void dfs(ci u,ci fa) {
vis[u]=true;
frog[u][1]=st;
for(rg int i=hd[u];i;i=edge[i].nxt) {
int &to=edge[i].to;
if(to == fa) continue;
dfs(to,u);
frog[u][0]+=frog[to][1]+1;
frog[u][1]+=mmin(frog[to][1],frog[to][0]+1);
}
}

Summary

对于同时最小化两个目标,而且在第一个目标最小的时候需要最小化第二个目标的的时候,可以设第一个目标的值是\(x_1\),第二个目标的值是\(x_2\)。目标为最小化\(ans=x_1~\times~K+x_2\),其中满足\(K~>~max_{x_2}\)。

【树形DP】【UVA10859】 Placing Lampposts的更多相关文章

  1. UVA10859 Placing Lampposts

    我是题面 这道题使我知道了一种很神奇的方法,一定要认真看哦 如果没有被两盏灯同时照亮的边数应尽量大这个限制的话,这就是一道很经典的树形DP题--没有上司的舞会 很可惜,这个限制就在那里,它使得我辛苦写 ...

  2. uva10859 Placing Lampposts (树形dp+求两者最小值方法)

    题目链接:点击打开链接 题意:给你一个n个点m条边的无向无环图,在尽量少的节点上放灯,使得所有边都被照亮,每盏灯将照亮以它为一个端点的所有边.在灯的总数最小的前提下,被两盏灯同时照亮的边数应尽量大. ...

  3. 再谈树形dp

    上次说了说树形dp的入门 那么这次该来一点有难度的题目了: UVA10859 Placing Lampposts 给定一个n个点m条边的无向无环图,在尽量少的节点上放灯,使得所有边都与灯相邻(被灯照亮 ...

  4. UVA 10859 - Placing Lampposts 树形DP、取双优值

                              Placing Lampposts As a part of the mission ‘Beautification of Dhaka City’, ...

  5. UVa 10859 - Placing Lampposts 树形DP 难度: 2

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  6. UVA 10859 Placing Lamppost 树形DP+二目标最优解的求解方案

    题意:给定一个无向,无环,无多重边,要求找出最少的若干点,使得,每条边之中至少有一个点上有街灯.在满足上述条件的时候将还需要满足让两个点被选择的边的数量尽量多. 题解: 对于如何求解最小的节点数目这点 ...

  7. 「算法笔记」树形 DP

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

  8. UVA - 10859 Placing Lampposts 放置街灯

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

  9. 树形DP入门学习

    这里是学习韦神的6道入门树形dp进行入门,本来应放在day12&&13里,但感觉这个应该单独放出来好点. 这里大部分题目都是参考的韦神的思想. A - Anniversary part ...

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

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

随机推荐

  1. 初学Direct X(5)

    初学Direct X(5) 前面学习了使用表面绘制屏幕,但这种方法与另一种比较起来,有着绘图速度颇慢以及缺乏对任何透明类型的支持,这就是前面的篮框以及炸弹会有黑色背景的原因,这种方法就是纹理.他可以绘 ...

  2. 流畅的python(笔记)

    流畅的python中有很多奇技淫巧,整本书都在强调如何最大限度地利用Python 标准库.介绍了很多python的不常用的数据类型.操作.库等,对于入门python后想要提升对python的认识应该有 ...

  3. 167. Add Two Numbers【LintCode by java】

    Description You have two numbers represented by a linked list, where each node contains a single dig ...

  4. loadrunner_遇到cookie接口_3种应对方法

    方法一:是调用登录接口,在调用登录后的接口 方法二:手动储存cookie,写死cookie 方法一:提前登录收集cookie,写成参数化文件 方法一,案例(就是先登录,再写登录后的接口): 注:use ...

  5. 文本分类-TextCNN

    简介 TextCNN模型是由 Yoon Kim提出的Convolutional Naural Networks for Sentence Classification一文中提出的使用卷积神经网络来处理 ...

  6. 接口_GET请求_基于python

    1.GET请求(不带参数) # coding:utf-8 import requests r=requests.get("https://www.baidu.com") #r即为r ...

  7. POJ 2986 A Triangle and a Circle(三角形和圆形求交)

    Description Given one triangle and one circle in the plane. Your task is to calculate the common are ...

  8. POJ 2987 Firing(最大流最小割の最大权闭合图)

    Description You’ve finally got mad at “the world’s most stupid” employees of yours and decided to do ...

  9. Python中package的导入语法

    在Python中,一个目录被称为一个package.import和from语法除了导入module文件之外,还可以导入package,语法如下: # import语法 import dir1.dir2 ...

  10. JavaScript中childNodes和children的区别

    我在学习JavaScript对DOM操作的过程中,发现了使用childNodes属性,得不到我想要的结果,因此我就从JavaScript高级程序设计中了解了childNodes和children的区别 ...