您的位置:首页 > Web前端

POJ 3398 Perfect Service【最小支配集修改 dp】

2016-05-18 21:36 387 查看
Perfect Service

Time Limit: 2000MSMemory Limit: 65536K
Total Submissions: 1497Accepted: 724
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

题意是说n个计算机,其中一些要作为服务器,服务器可以与其他机子相连,问最小的服务器的个数,但是一个普通机子不能与两台及以上服务器相连,这里对应的最小支配集dp递推方程不太一样。

dp[u][0]表示当前节点属于支配集并且以u为根的子树都被覆盖的情况下支配集所含最少点的个数;

dp[u][1]表示当前点不属于支配集并且以u为根的子树都被覆盖且u被至少一个子节点覆盖的情况下支配集所含最少点的个数;

dp[u][2]表示当前点不属于支配集并且以u为根的子树都被覆盖且u没被子节点覆盖的情况下支配集所含最少点的个数。

对于第一种状态,值等于每个儿子节点的3种状态的最小值之和加1,因为当前点属于支配集,其儿子的状态就不重要了。

第二种状态,若当前点没有子节点令其为INF 否则,则需要以其儿子节点为根节点的子树全被覆盖,因此与其儿子节点的前两种状态有关,对于其儿子的第三种状态,因为其儿子不在支配集又不被覆盖而当前点也不在支配集如此其儿子将永远不被覆盖所以和第三种状态无关;而对于其儿子节点的前两种状态,若对于所有的儿子节点的前两种状态的最小值都落在第二种状态上则必须强制其中的一个儿子节点为第一种状态,否则会出现其所有儿子都不在支配集而当前点也不在支配集则当前点永远不会被覆盖的情况;因此在计算过程中应标记存不存在一个儿子节点为第一种状态同时计算更新所有儿子节点的第二种状态和第一种状态的差的最小值。

第三种状态,当前点不在支配集且不被儿子节点覆盖,那么所有的儿子节点都是被覆盖且不在支配集,也就是说此状态只与儿子节点的第二种状态有关加和即可。

对于此题,由于一个普通机子不能和两台及以上服务器相连,因此第一种状态需要修改一下。当前点属于支配集,那么其为普通机子的儿子的儿子不能属于支配集,即其儿子要么属于支配集,要么不能被覆盖,因此第一种状态为其儿子节点的第一种状态和第三种状态的最小值之和。

#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define maxn 10010
#define inf 0x3f3f3f3f
using namespace std;
int dp[maxn][3];
int cnt,head[maxn];
struct node
{
int f,t,next;
}edge[maxn];
void init()
{
cnt=0;
memset(head,-1,sizeof(head));
memset(dp,0,sizeof(dp));
}
void add(int f,int t)
{
edge[cnt].f=f;
edge[cnt].t=t;
edge[cnt].next=head[f];
head[f]=cnt++;
}
void DP(int u,int p)
{
dp[u][2]=0;
dp[u][0]=1;
bool sign=false;
int sum=0,inc=inf;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].t;
if(v==p)
continue;
DP(v,u);
dp[u][0]+=min(dp[v][0],dp[v][2]);
//dp[u][0]=min(dp[v][0],min(dp[v][1],dp[v][2]))最小支配集模板中
if(dp[v][0]<=dp[v][1])
{
sum+=dp[v][0];
sign=true;
}
else
{
sum+=dp[v][1];
inc=min(inc,dp[v][0]-dp[v][1]);
}
if(dp[v][1]!=inf&&dp[u][2]!=inf)
dp[u][2]+=dp[v][1];
else
dp[u][2]=inf;
}
if(inc==inf&&!sign)
dp[u][1]=inf;
else
{
dp[u][1]=sum;
if(!sign)
dp[u][1]+=inc;
}
}
int main()
{
int n,a,b;
while(scanf("%d",&n)&&(n!=-1))
{
init();
for(int i=1;i<n;++i)
{
scanf("%d%d",&a,&b);
add(a,b);
add(b,a);
}
DP(1,n);
int ans=min(dp[1][0],dp[1][1]);
printf("%d\n",ans);
scanf("%d",&a);
if(a==0)
continue;
else if(a==-1)
return 0;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: