Time Limit: 3000MS     64bit IO Format: %lld & %llu
 /*by SilverN*/
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#include<queue>
using namespace std;
const int mxn=;
vector<int>e[mxn];
int f[mxn][];
int vis[mxn];
int n;
void dp(int u){
int i,j;
vis[u]=;
f[u][]=;f[u][]=;f[u][]=mxn;
queue<int>q;
for(i=;i<e[u].size();i++){
int v=e[u][i];
if(!vis[v]){
dp(v);
q.push(v);
f[u][]+=min(f[v][],f[v][]);
f[u][]+=f[v][];
}
}
while(!q.empty()){
f[u][]=min(f[u][],f[u][]-f[q.front()][]+f[q.front()][]);
q.pop();
}
return;
}
int main(){
int i,j;
while(scanf("%d",&n) && n!=-){
if(!n)continue;
memset(f,,sizeof f);
memset(vis,,sizeof vis);
for(i=;i<=n;i++) e[i].clear();
int x,y;
for(i=;i<n;i++){
scanf("%d%d",&x,&y);
e[x].push_back(y);
e[y].push_back(x);
}
dp();
printf("%d\n",min(f[][],f[][]));
}
return ;
}

UVA1218 Perfect Service的更多相关文章

  1. 【题解】UVA1218 Perfect Service

    UVA1218:https://www.luogu.org/problemnew/show/UVA1218 刷紫书DP题ing 思路 参考lrj紫书 不喜勿喷 d(u,0):u是服务器,孩子是不是服务 ...

  2. POJ 3398 Perfect Service(树型动态规划,最小支配集)

    POJ 3398 Perfect Service(树型动态规划,最小支配集) Description A network is composed of N computers connected by ...

  3. Perfect Service [POJ 3398]

    Perfect Service 描述 网络由N个通过N-1个通信链路连接的计算机组成,使得任何两台计算机可以通过独特的路由进行通信.如果两台计算机之间存在通信链路,则称这两台计算机是相邻的.计算机的邻 ...

  4. UVA - 1218 Perfect Service(树形dp)

    题目链接:id=36043">UVA - 1218 Perfect Service 题意 有n台电脑.互相以无根树的方式连接,现要将当中一部分电脑作为server,且要求每台电脑必须连 ...

  5. UVa 1218 - Perfect Service

    /*---UVa 1218 - Perfect Service ---首先对状态进行划分: ---dp[u][0]:u是服务器,则u的子节点可以是也可以不是服务器 ---dp[u][1]:u不是服务器 ...

  6. Perfect service(树形dp)

    Perfect service(树形dp) 有n台机器形成树状结构,要求在其中一些机器上安装服务器,使得每台不是服务器的计算机恰好和一台服务器计算机相邻.求服务器的最小数量.n<=10000. ...

  7. POJ 3398 Perfect Service --最小支配集

    题目链接:http://poj.org/problem?id=3398 这题可以用两种上述讲的两种算法解:http://www.cnblogs.com/whatbeg/p/3776612.html 第 ...

  8. 【POJ】3398 Perfect Service

    1. 题目描述某树形网络由$n, n \in [1, 10^4]$台计算机组成.现从中选择一些计算机作为服务器,使得每当普通计算机恰好与一台服务器连接(并且不超过一台).求需要指定服务器的最少数量 2 ...

  9. Perfect Service

    题意: n个节点树,在一个节点放上一台服务器可以给相邻的其他各点提供服务且一个节点只能接受一台服务器,求使n个节点都被服务放的服务器的最小数量. 分析: 不算太难,状态想的差不多,但是考虑不全面状态方 ...

随机推荐

  1. 井字游戏 人机对战 java实现

    package com.ecnu.Main; /** * 主函数触发游戏 */public class MainApplication { public static void main(String ...

  2. 洛谷 3567/BZOJ 3524 Couriers

    3524: [Poi2014]Couriers Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 2895  Solved: 1189[Submit][S ...

  3. JS - OOP-继承的最佳实现方式

    如上图,使用第三种方式实现继承最好,也就是加了下划线的. 但是Object.create方法是ES6才支持的,所以,右边就写了一个实现其同样功能的函数.

  4. 一次完整的HTTP请求需要的7个步骤

    HTTP通信机制是在一次完整的HTTP通信过程中,Web浏览器与Web服务器之间将完成下列7个步骤: 1:建立TCP连接 在HTTP工作开始之前,Web浏览器首先要通过网络与Web服务器建立连接,该连 ...

  5. [译]The Python Tutorial#2. Using the Python Interpreter

    [译]The Python Tutorial#Using the Python Interpreter 2.1 Invoking the Interpreter Python解释器通常安装在目标机器的 ...

  6. python3 练习题100例 (十四)

    今天逛贴吧,看到有人求助,做了一下.请大家指正! #!/usr/bin/env python3 # -*- coding: utf-8 -*- __author__ = 'Fan Lijun' imp ...

  7. 笔记-python-standard library-9.6 random

    笔记-python-standard library-9.6 random 1.      random source code:Lib/random.py 1.1.    functions for ...

  8. 《鸟哥的Linux私房菜》学习笔记(0)——磁盘与文件系统管理

    一.Linux的登陆流程 login: 用户名:每个用户名都有一个用户ID(用户标识符),计算机处理的就是用户ID(数字)而不是用户名(字符),. 认证机制:Authentication,比如密码或者 ...

  9. OpenCV学习笔记(一) 环境配置

    Visual Studio 2010 VS2010对应的OpenCV的lib文件(build\x86\vc10\lib)分为debug模式和release模式两种:debug模式牺牲速度,但能提供更多 ...

  10. 堆STL和重载运算符

    大根堆: 1.priority_queue<int> q;[默认 2. priority_queue< node,vector<node>,less<node> ...