最小支配集--pojPerfect Service
来源:互联网 发布:au是什么软件 编辑:程序博客网 时间:2024/06/03 02:26
Perfect Service
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 1739 Accepted: 844
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 by exactly 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
/* 最小支配集 ,即在一棵树中,选择覆盖一个点,则该点的邻居也会被覆盖 整棵树中,选择最少的点,使得所有的点被覆盖 poj 3398 最小支配集裸题 */#include<iostream>#include<stdio.h>#include<algorithm>#include<string.h>using namespace std;const int MAXN = 20010;struct EdgeNode{ int to; int next;}Edges[MAXN];int Head[MAXN];//前向星建图 int father[MAXN];//记录i的父节点 int Pos[MAXN];//记录深搜时第i个访问到的节点 bool vis[MAXN];//标记节点是否访问过 bool S[MAXN];//true 代表第i个点被覆盖了 bool Set[MAXN];//true 代表第i个点属于支配集 int N,M,cnt;void DFS(int x){ Pos[cnt++] = x; for(int k = Head[x]; k != -1; k = Edges[k].next) { if(!vis[Edges[k].to]) { vis[Edges[k].to] = true; father[Edges[k].to] = x; DFS(Edges[k].to); } }}int Greedy()//贪心求最小支配集{ memset(S,0,sizeof(S)); memset(Set,0,sizeof(Set)); int ans = 0; for(int i = N-1; i >= 0; i--)//反向序列检查 { int t = Pos[i]; if(!S[t])//当前点未被覆盖,也就是当前点既不属于支配集,也不与支配集中的点相连 { if(!Set[father[t]])//当前点的父亲结点不属于支配集, { Set[father[t]] = true; //将父节点加入支配集 ans++; //顶点个数加1 } //执行到这里,t的父节点已经属于支配集了 //所以要讲t,t的父节点,t的父节点的父节点覆盖掉 S[t] = true; S[father[t]] = true; S[father[father[t]]] = true; } } return ans;}int main(){// ios::sync_with_stdio(false);// freopen("in.txt","r",stdin); int u,v,nxt=0; while(1) { if(nxt == -1) break; scanf("%d",&N); //初始化 memset(Edges,0,sizeof(Edges)); memset(Head,-1,sizeof(Head)); memset(father,0,sizeof(father)); memset(vis,false,sizeof(vis)); memset(Pos,0,sizeof(Pos)); int id = 0; for(int i = 0; i < N-1; ++i) { scanf("%d%d",&u,&v); Edges[id].to = v; Edges[id].next = Head[u]; Head[u] = id++; Edges[id].to = u; Edges[id].next = Head[v]; Head[v] = id++; } cnt = 0; vis[1] = true; father[1] = 1; DFS(1); printf("%d\n",Greedy()); cin >> nxt; } return 0;}
- 最小支配集--pojPerfect Service
- POJ 3398 Perfect Service 最小支配集变形
- poj 3398 Perfect Service(最小支配集树形DP)
- POJ 3398 Perfect Service(树形DP,最小支配集)
- POJ 3398 Perfect Service【最小支配集修改 dp】
- Poj 3659 Perfect Service【最小支配集、树型dp】
- POJ 3398 Perfect Service 树形DP 最小支配集变形
- 最小支配集
- 树的最小支配集
- 树的最小支配集
- poj 3398 Perfect Service 树形dp类似求树的点最小支配集
- HDU--3957[Street Fighter] 最小支配集
- poj 3659 树形dp 最小支配集
- POJ 3659 树形DP,最小支配集
- POJ 3659 贪心 最小支配集
- 寒假集训 1004【最小支配集】
- 最小支配集、最小覆盖点、最大独立集
- 最小支配集合板子
- 单页web应用(SPA)的简单介绍
- Smart3D软件基本操作步骤
- 阿里云双11活动撸福利攻略云服务器篇 必买爆款
- Nginx源码剖析--HTTP模块配置信息的merge
- python3的format
- 最小支配集--pojPerfect Service
- WMTS 概述
- Python简单的爬虫(一)
- 如何在CentOS 7上安装Nginx,MySQL,PHP(LEMP)堆栈
- 打造自己的Ubuntu渗透利器--安装巡风扫描器
- Truncated incorrect DOUBLE value: 报错
- 受欢迎的牛bzoj1051(浅谈tarjan——(3))
- Fragment+ViewPage一步步实现底部导航栏。
- 在V4中如何使用局部时钟网络