UVA

来源:互联网 发布:网络语苏是什么意思 编辑:程序博客网 时间:2024/06/05 06:57

原题:

A network is comp osed ofNcomputers connected byN1 communication links such that any two
computers can be communicated via a unique route. Two computers are said to beadjacentif there is
a communication link between them. Theneighborsof 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 serversto provide resources to their neighbors. Note that a server can serve
all its neighbors. A set of servers in the network forms aperfect serviceif every client (non-server) is
served by exactly oneserver. The problem is to find a minimum number of servers which forms a
perfect service, and we call this numberperfect service number.
We assume that N(10000) is a positive integer and theseNcomputers 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
02
1 2
-1
Sample Output
21


题意:

       n台机器组成树形结构,选择其中一些为服务器,要求每台机器恰好和一台服务器相连,求服务器的最小数量。
思路:

       取dp[i][0]为i为服务器时最小数量。dp[i][1]为i不是服务器,但i的父亲为服务器,则i的所有子节点都不是服务器。

dp[i][2]为i和它的父亲都不是服务器,则i的子节点中有且只有一个服务器。

       dp[i][0]=sum{min(dp[v][0],dp[v][1]}+1;

       dp[i][1]=sum(dp[v][2]);

       dp[i][2]=min(dp[i][1]-dp[v][2]+dp[v][0]);

       注意:建树的时候要双向,以保证是完整的树


#include <iostream>#include <iomanip>#include <algorithm>#include <cstdio>#include <cstring>#include <queue>#include <deque>#include <string>#include <cmath>#include <vector>#include <utility>#include <set>#include <map>#include <sstream>#include <climits>//#pragma comment(linker, "/STACK:1024000000,1024000000")#define pi acos(-1.0)#define INF 2147483647using namespace std;typedef long long ll;typedef pair <int,int > P;int N;vector<int > son[10015];int dp[10015][5];int vis[10015];void dfs(int n,int f){    if(vis[n])        return ;    vis[n]=1;    int num=son[n].size();    if(num==0)    {        dp[n][0]=1;        dp[n][1]=0;        dp[n][2]=0;        return ;    }    dp[n][0]=1;    for(int i=0; i<num; i++)    {        dfs(son[n][i],n);        if(son[n][i]!=f)        {            dp[n][0]+=min(dp[son[n][i]][0],dp[son[n][i]][1]);            dp[n][1]+=dp[son[n][i]][2];        }    }    dp[n][2]=N;    for(int i=0; i<num; i++)    {        if(son[n][i]!=f)            dp[n][2]=min(dp[n][2],dp[n][1]-dp[son[n][i]][2]+dp[son[n][i]][0]);    }}int main(){    while(scanf("%d",&N))    {        memset(vis,0,sizeof(vis));        memset(dp,0,sizeof(dp));        for(int i=0; i<10015; i++)            son[i].clear();        for(int i=0; i<N-1; i++)        {            int a,b;            scanf("%d%d",&a,&b);            son[a].push_back(b);            son[b].push_back(a);        }        int flag;        scanf("%d",&flag);        dfs(1,-1);        int res=INF;        res=min(dp[1][0],dp[1][2]);        printf("%d\n",res);        if(flag==-1)            break;    }    return 0;}


原创粉丝点击