UVa1218 Perfect Service

2024-01-28 06:48
文章标签 service perfect uva1218

本文主要是介绍UVa1218 Perfect Service,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

        稍复杂的树型dp。。状态的类型不仅跟当前节点有关,还与当前节点的父节点有关。一共有三种状态,当前节点是服务器,当前节点不是且父节点是,当前节点不是且父节点不是。dp的时候直接把不可能状态的费用置为无穷大,这样求出的解就不会是不可能状态。。


#include <iostream>    
#include <stdio.h>    
#include <cmath>    
#include <algorithm>    
#include <iomanip>    
#include <cstdlib>    
#include <string>    
#include <memory.h>    
#include <vector>    
#include <queue>    
#include <stack>    
#include <map>  
#include <set>  
#include <ctype.h>    
#define INF 10000
#define ll long long
#define max3(a,b,c) max(a,max(b,c))
#define MAXN 100010using namespace std;  vector<int> E[10010];
int dp[10010][3];/** 0都不是 * 1自己是 * 2父亲是 */
int fun(int u,int f,int type){if(dp[u][type]!=-1)return dp[u][type];int re=0;if(type==1){for(int i=0;i<E[u].size();i++){if(E[u][i]==f)continue;re+=min(fun(E[u][i],u,1),fun(E[u][i],u,2));}re++;}if(type==0){int tmp=0;re=INF;for(int i=0;i<E[u].size();i++){if(E[u][i]==f)continue;tmp+=fun(E[u][i],u,0);}//换掉一个 for(int i=0;i<E[u].size();i++){if(E[u][i]==f)continue;re=min(re,tmp-fun(E[u][i],u,0)+fun(E[u][i],u,1));}if(E[u].size()==1&&f!=-1)re=INF;}if(type==2){for(int i=0;i<E[u].size();i++){if(E[u][i]==f)continue;re+=fun(E[u][i],u,0);}}dp[u][type]=re;return re;
}int main(){int N;while(cin>>N){memset(dp,-1,sizeof(dp));for(int i=1;i<=N;i++)E[i].clear();int u,v;for(int i=1;i<N;i++){cin>>u>>v;E[u].push_back(v);E[v].push_back(u);}cout<<min(fun(1,-1,0),fun(1,-1,1))<<endl;int end;cin>>end;if(end==-1)break;}return 0;
}


这篇关于UVa1218 Perfect Service的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/652797

相关文章

Android里面的Service种类以及启动方式

《Android里面的Service种类以及启动方式》Android中的Service分为前台服务和后台服务,前台服务需要亮身份牌并显示通知,后台服务则有启动方式选择,包括startService和b... 目录一句话总结:一、Service 的两种类型:1. 前台服务(必须亮身份牌)2. 后台服务(偷偷干

使用TomCat,service输出台出现乱码的解决

《使用TomCat,service输出台出现乱码的解决》本文介绍了解决Tomcat服务输出台中文乱码问题的两种方法,第一种方法是修改`logging.properties`文件中的`prefix`和`... 目录使用TomCat,service输出台出现乱码问题1解决方案问题2解决方案总结使用TomCat,

解决systemctl reload nginx重启Nginx服务报错:Job for nginx.service invalid问题

《解决systemctlreloadnginx重启Nginx服务报错:Jobfornginx.serviceinvalid问题》文章描述了通过`systemctlstatusnginx.se... 目录systemctl reload nginx重启Nginx服务报错:Job for nginx.javas

UserWarning: mkl-service package failed to import

安装完成anaconda,并设置了两个环境变量  之后再控制台运行python环境,输入import numpy as np,提示错误 D:\InstallFolder\Anaconda3\lib\site-packages\numpy\__init__.py:143: UserWarning: mkl-service package failed to import, therefore

jdbc连接数据库使用sid和service_name的区别 ?

问题描述: ORA-12505, TNS:listener does not currently know of SID given in connect descriptor The Connection descriptor used by the client was: 10.12.162.84:1521:xxxx  oracle数据的tnsnames.ora中配置的是:SERVICE

Spring 注解(@Repository 、@Service 和 @Controller )

Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository、@Service 和 @Controller 。         在目前的 Spring 版本中,这 3 个注释和 @Component 是等效的,但是从注释类的命名上,很容易看出这 3 个注释分别和持久层 、业务层 和控制层 相对应。虽然目前这 3 个注释和 @

Android 接收系统广播,开机启动Service,SMS,Battery

本文内容摘自《疯狂Android讲义 第3版》李刚 著 自动开启的Service: package shortcut.song.com.myapplication;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;im

设计之道:ORM、DAO、Service与三层架构的规范探索

引言: 实际开发中,遵守一定的开发规范,不仅可以提高开发效率,还可以提高项目的后续维护性以及项目的扩展性;了解一下本博客的项目设计规范,对项目开发很有意义 一、ORM思想 ORM(Object-Relational-Mapping)在对象模型和关系型模型之间做一个映射(转换)。 目的是为了解决面向对象编程语言的发展和关系型数据库的发展不匹配的问题 可以理解为: 将Java中的数据结

nacos Spring cloud 报错 URI is not absolute、service not found、502 bad gateway

- 服务没找到,请加入依赖 <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-loadbalancer</artifactId></dependency> - 如果是 "URI is not absolute" , 将URL变成固定的字符串,例如

Service相关汇集(可续)

官译:Service 001、Service是什么 Service 是一个可以在后台执行长时间运行操作而不提供用户界面的应用组件。服务可由其他应用组件启动,而且即使用户切换到其他应用,服务仍将在后台继续运行。 此外,组件可以绑定到服务,以与之进行交互,甚至是执行进程间通信 (IPC)。 例如,服务可以处理网络事务、播放音乐,执行文件 I/O 或与内容提供程序交互,而所有这一切均可在后台进行。