入门题吧。。。

Perfect Service
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 1220   Accepted: 593

Description

A network is composed of N computers connected by N − 1 communication links such that any two computers can be communicated via a unique route. Two computers are said to be adjacent if there is a communication link between them. The neighbors of a computer is the set of computers which are adjacent to it. In order to quickly access and retrieve large amounts of information, we need to select some computers acting as servers to provide resources to their neighbors. Note that a server can serve all its neighbors. A set of servers in the network forms a perfect service if every client (non-server) is served byexactly one server. The problem is to find a minimum number of servers which forms a perfect service, and we call this number perfect service number.

We assume that N (≤ 10000) is a positive integer and these N computers are numbered from 1 to N. For example, Figure 1 illustrates a network comprised of six computers, where black nodes represent servers and white nodes represent clients. In Figure 1(a), servers 3 and 5 do not form a perfect service because client 4 is adjacent to both servers 3 and 5 and thus it is served by two servers which contradicts the assumption. Conversely, servers 3 and 4 form a perfect service as shown in Figure 1(b). This set also has the minimum cardinality. Therefore, the perfect service number of this example equals two.

Your task is to write a program to compute the perfect service number.

Input

The input consists of a number of test cases. The format of each test case is as follows: The first line contains one positive integer, N, which represents the number of computers in the network. The next N − 1 lines contain all of the communication links and one line for each link. Each line is represented by two positive integers separated by a single space. Finally, a 0 at the (N + 1)th line indicates the end of the first test case.

The next test case starts after the previous ending symbol 0. A −1 indicates the end of the whole inputs.

Output

The output contains one line for each test case. Each line contains a positive integer, which is 
the perfect service number.

Sample Input

6
1 3
2 3
3 4
4 5
4 6
0
2
1 2
-1

Sample Output

2
1

Source

Kaohsiung 2006
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <iostream>
using namespace std;
#define N 20010struct node
{int to,next;
}edge[2*N];int cnt,pre[N];
int n;
int mark[N];
int f[N];
int g[N];
int num;void add_edge(int u,int v)
{edge[cnt].to=v;edge[cnt].next=pre[u];pre[u]=cnt++;
}void DFS(int s)
{mark[s]=1;g[num++]=s;for(int p=pre[s];p!=-1;p=edge[p].next){int v=edge[p].to;if(mark[v]==0){f[v]=s;DFS(v);}}
}int  greedy()
{int set[N];memset(set,0,sizeof(set));memset(mark,0,sizeof(mark));int sum=0;for(int i=num-1;i>=0;i--){int k=g[i];if(mark[k]==0){if(set[f[k]]==0){set[f[k]]=1;sum++;}mark[k]=1;mark[f[k]]=1;mark[f[f[k]]]=1;}}return sum;
}int main()
{while(scanf("%d",&n)&&(n!=-1)){if(n==0) continue;cnt=0;num=0;memset(pre,-1,sizeof(pre));memset(f,0,sizeof(f));for(int i=1;i<n;i++){int  x,y;scanf("%d%d",&x,&y);add_edge(x,y);add_edge(y,x);}f[1]=1;memset(mark,0,sizeof(mark));DFS(1);printf("%d\n",greedy());}return 0;
}

转载于:https://www.cnblogs.com/chenhuan001/archive/2013/03/18/2965858.html

poj 3398 (树上的最小支配集)相关推荐

  1. 贪心法求树的最小支配集,最小点覆盖,最大独立集

    原文地址(转自 Ashly的博客) 定义: 最小支配集:对于图G = (V, E) 来说,最小支配集指的是从 V 中取尽量少的点组成一个集合, 使得 V 中剩余的点都与取出来的点有边相连.也就是说,设 ...

  2. 最小支配集,最小点覆盖,最大独立集(贪心/DP)

    最小支配集(minimal dominating set):对于图G=(V,E)来说,设V'是图G的一个支配集,则对于图中的任意一个顶点u,要么属于集合V',要么与V'中的顶点相连. 在V'中除去任何 ...

  3. 树的最小支配集、最小点覆盖、最大独立集【模板】

    最小支配集:指从所有顶点中取尽量少的点组成一个集合,使得剩下的所有点都与取出来的点有边相连.顶点个数最小的支配集被称为最小支配集.这里用贪心法来求. 1.以1号点深度优先搜索整棵树,求出每个点在DFS ...

  4. 牛客假日团队赛8:H.Cell Phone Network(最小支配集)

    链接:https://ac.nowcoder.com/acm/contest/1069/A 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言6553 ...

  5. 树形DP求树的最小支配集,最小点覆盖,最大独立集

    转自:https://www.cnblogs.com/Ash-ly/p/5783877.html 一:最小支配集 考虑最小支配集,每个点有两种状态,即属于支配集合或者不属于支配集合,其中不属于支配集合 ...

  6. [UVA-1218] Perfect Service(树的最小支配集)

    题目链接:https://vjudge.net/problem/UVA-1218 题目大意:给你一棵无向树,让你求树的最小支配集,但是有一个要求是除最小支配集外剩下的任何一个结点不能同时连接支配集中的 ...

  7. 1579: 【例 5】皇宫看守(最小支配集——贪心求解/树形DP)

    [题目描述] 太平王世子事件后,陆小凤成了皇上特聘的御前一品侍卫. 皇宫以午门为起点,直到后宫嫔妃们的寝宫,呈一棵树的形状,某些宫殿间可以互相望见.大内保卫森严,三步一岗,五步一哨,每个宫殿都要有人全 ...

  8. *【POJ - 3659】Cell Phone Network (树形dp,最小支配集)

    题干: Farmer John has decided to give each of his cows a cell phone in hopes to encourage their social ...

  9. 独立集,覆盖集,支配集,最大团,最大匹配

    独立集: 独立集是指图的顶点集的一个子集,该子集的导出子图不含边.如果一个独立集不是任何一个独立集的子集, 那么称这个独立集是一个极大独立集.一个图中包含顶点数目最多的独立集称为最大独立集.最大独立集 ...

最新文章

  1. Leangoo敏捷工具企业版-企业管理
  2. Golang的单引号、双引号与反引号
  3. k-means均值向量
  4. C++ 学习 之Struct
  5. c语言 冒泡排序算法,排序算法_冒泡排序改进(c语言实现)
  6. 洛谷——P1085 [NOIP2004 普及组] 不高兴的津津
  7. 减速箱箱体尺寸是怎样计算的_皮带输送机减速机漏油、油温高可不能小觑,原因及处理方法教给你...
  8. qq发压缩文件服务器拒绝,如何解决qq文件太大无法发送问题
  9. 前端学习/ Day1/HTTP简单易懂/GET POST/HTTP特性/HTTP与HTTPS/HTTP版本演变/加解密数字签名数字证书
  10. java程序如何安装到手机上_java怎么安装到手机?手机安装java的教程
  11. macOS升级到Big Sur之后Goland提示找不到svn命令
  12. TB6612FNG 驱动学习笔记
  13. python输入生日输出星座代码 过程_用Python来写一个男女相亲小程序|码农的情人节...
  14. 使用Python实现搜索任意电影资源的磁力链接
  15. Bugku -disordered_zip【MISC】
  16. Java使用正则表达式从字符串中提取 IP 地址
  17. AI崛起,阿里的科技孵化力
  18. 一千万人都在等小黄车OFO退押金
  19. 城堡争霸服务器维护,城堡争霸 - 阵营守护神(国际服)无法连接服务器是什么原因...
  20. (一)软件开发背景 - 天津市政府采购中心网

热门文章

  1. AQS独占式同步队列入队与出队
  2. android stub.asinterface是什么意思
  3. learning hdmi edid protocol
  4. 2019年1月29日
  5. 河南省第十届大学生程序设计竞赛 A,B,C,D,F,G,H 题解
  6. Stanford CoreNLP使用需要注意的一点
  7. CodeForces - 707C
  8. CSS3 里添加自定义字体
  9. 添加水印(文字and图片)
  10. 位运算模块mBit.bas