Perfect Service UVA - 1218(树形dp)

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
题意:有n(n<=10000)台机器形成树状结构,要求在其中一些机器上安装服务器,要求每台不是服务器的机器恰好和一台服务器计算机相邻。求服务器的最少数量。
思路:对于这一点来说,有三种状态:
①当前点就是服务器。
②当前点不是服务器,但是父节点是服务器。
③当前点不是服务器,但是父节点不是服务器,儿子节点是服务器。
只有这三种情况,其他的情况要么和自己没关系,要么就不符合题意。
我们令dp[u][0]代表第一种情况,dp[u][1]代表第二种情况,dp[u][2]代表第三种情况。
对于第一种情况,当前点是服务器,那么儿子节点是不是服务器就没有关系了,选择一个最小值加上就行了。dp[u][0]+=min(dp[v][0],dp[v][1])
对于第二种情况,当前点不是服务器,但是父亲节点是服务器,那么儿子节点一定不是服务器。那么加上儿子节点不是服务器,儿子的儿子是服务器的情况就可以了。dp[u][1]+=dp[v][2].
对于第三种情况,是比较复杂的。子节点肯定只有一个才能是服务器。也就是说dp[u][2]=min(dp[u][2],dp[v1][2]+dp[v2][2]…+dp[v][0])。dp[u][2]和dp[u][1]的区别在于dp[u][2]有且只有一个子结点是服务器,所以这里可以简化为dp[u][2]=min(dp[u][2],dp[u][1]-dp[v][2]+dp[v][0])。
如图所示图片来源

在这里插入图片描述
代码如下:

#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;

const int maxx=1e4+100;
int dp[maxx][3];
struct edge{
	int to,next;
}e[maxx<<1];
int head[maxx<<1];
int n,tot;
//dp[u][0]代表的是u这个结点是服务点
//dp[u][1]代表的是u这个结点不是服务点,父亲节点是服务点
//dp[u][2]代表的是u和u的父亲节点不是服务点,儿子节点是服务点 
inline void init()
{
	memset(head,-1,sizeof(head));
	tot=0;
}
inline void add(int u,int v)
{
	e[tot].next=head[u],e[tot].to=v,head[u]=tot++;
}
inline void dfs(int u,int f)
{
	dp[u][0]=1;
	dp[u][1]=0;
	dp[u][2]=inf;
	for(int i=head[u];i!=-1;i=e[i].next)
	{
		int to=e[i].to;
		if(to==f) continue;
		dfs(to,u);
		dp[u][0]+=min(dp[to][1],dp[to][0]);
		dp[u][1]+=dp[to][2];
		dp[u][0]=min(dp[u][0],inf);
		dp[u][1]=min(dp[u][1],inf);
		dp[u][2]=min(dp[u][2],dp[u][1]-dp[to][2]+dp[to][0]);
	}
}
int main()
{
	while(1)
	{
		scanf("%d",&n);
		int x,y;
		init();
		for(int i=1;i<n;i++)
		{
			scanf("%d%d",&x,&y);
			add(x,y);
			add(y,x);
		}
		dfs(1,0);
		printf("%d\n",min(dp[1][0],dp[1][2]));
		scanf("%d",&x);
		if(x==-1) break;
	}
	return 0;
}

努力加油a啊,(o)/~

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值