uva1267 Network

来源:互联网 发布:php 站点根目录 编辑:程序博客网 时间:2024/06/11 16:49

Consider a tree network with n nodes where the internal nodes
correspond to servers and the terminal nodes correspond to clients.
The nodes are numbered from 1 to n . Among the servers, there is an
original server S which provides VOD (Video On Demand) service. To
ensure the quality of service for the clients, the distance from each
client to the VOD server S should not exceed a certain value k . The
distance from a node u to a node v in the tree is de ned to be the
number of edges on the path from u to v . If there is a nonempty
subset C of clients such that the distance from each u in C to S is
greater than k , then replicas of the VOD system have to be placed in
some servers so that the distance from each client to the nearest VOD
server (the original VOD system or its replica) is k or less. Given a
tree network, a server S which has VOD system, and a positive integer
k , nd the minimum number of replicas necessary so that each client
is within distance k from the nearest server which has the original
VOD system or its replica. For example, consider the following tree
network. In the above tree, the set of clients is f 1, 6, 7, 8, 9, 10,
11, 13 g , the set of servers is f 2, 3, 4, 5, 12, 14 g , and the
original VOD server is located at node 12. For k
= 2, the quality of service is not guaranteed with one VOD server at node 12 because the clients in f 6, 7, 8, 9, 10 g are away from VOD
server at distance

k . Therefore, we need one or more replicas. When one replica is placed at node 4, the distance from each client to the nearest server
of f 12, 4 g is less than or equal to 2. The minimum number of the
needed replicas is one for this example. Input Your program is to read
the input from standard input. The input consists of T test cases. The
number of test cases ( T ) is given in the rst line of the input. The
rst line of each test case contains an integer n (3  n  1 ; 000)
which is the number of nodes of the tree network. The next line
contains two integers s (1  s  n ) and k ( k  1) where s is the VOD
server and k is the distance value for ensuring the quality of
service. In the following n

以原有节点为根转为有根树,然后对于当前最深的没有覆盖的节点,当然选择它的第k个祖先进行覆盖才能在满足覆盖到他的前提下覆盖到最多的节点,然后从这个点开始dfs一遍覆盖别的点【注意不只是它的子树,还有他的兄弟以及兄弟的子树】。重复此操作直到所有叶子都被覆盖。
复杂度好像是O(n^2)的,但是一般来说并不用覆盖n次,每次也不用遍历整棵树进行覆盖,所以实际上优很多。

#include<cstdio>#include<cstring>#include<vector>#include<algorithm>using namespace std;int n,k,r,fa[1010];vector<int> to[1010],lvl[1010];bool ok[1010];void init(){    int i,x,y;    scanf("%d%d%d",&n,&r,&k);    for (i=1;i<=n;i++)      to[i].clear(),lvl[i].clear();    for (i=1;i<n;i++)    {        scanf("%d%d",&x,&y);        to[x].push_back(y);        to[y].push_back(x);    }}void dfs(int u,int f,int d){    int i,v;    if (to[u].size()==1) lvl[d].push_back(u);    fa[u]=f;    for (i=0;i<to[u].size();i++)      if ((v=to[u][i])!=f)        dfs(v,u,d+1);}void dfs2(int u,int now,int f){    int i,v;    if (now>k) return;    if (to[u].size()==1) ok[u]=1;    for (i=0;i<to[u].size();i++)      if ((v=to[u][i])!=f)        dfs2(v,now+1,u);}int solve(){    int i,j,x,u,v,ans=0;    memset(ok,0,sizeof(ok));    for (i=n;i>k+1;i--)      for (j=0;j<lvl[i].size();j++)        if (!ok[u=lvl[i][j]])        {            ans++;            for (x=1;x<=k;x++)              u=fa[u];            dfs2(u,0,-1);        }    return ans;}int main(){    int T;    scanf("%d",&T);    while (T--)    {        init();        dfs(r,0,1);        printf("%d\n",solve());    }}
0 0
原创粉丝点击