UvaLive 3902 Network(贪心+DFS)

℡╲_俬逩灬. 提交于 2019-12-24 10:56:08

UvaLive 3902 Network

Description

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 defined 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 , find 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 {1, 6, 7, 8, 9, 10, 11, 13}, the set of servers is {2, 3, 4, 5, 12, 14}, 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 {6, 7, 8, 9, 10} 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 {12, 4} 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 first line of the input. The first 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 - 1 lines, each line contains a pair of nodes which represent an edge of the tree network.

Output

Your program is to write to standard output. Print exactly one line for each test case. The line should contain an integer that is the minimum number of the needed replicas.

Sample Input

2 14 
12 2 
1 2 
2 3 
3 4 
4 5 
5 6 
7 5 
8 5 
4 9 
10 3 
2 12 
12 14 
13 14 
14 11 
14 
3 4 
1 2 
2 3 
3 4 
4 5 
5 6 
7 5 
8 5 
4 9 
10 3 
2 12 
12 14 
13 14 
14 11

Sample Output

1 
0

题解

题意

给出一颗无根树,和服务器覆盖范围k,问在非叶子节点部署服务器,要求覆盖所有叶子节点。求所需服务器的最小值。

思路

首先,将题中给出的服务器节点作为树的根节点,以建立有根树。然后每次取当前深度最大而没有覆盖的节点。通过贪心的想法,每次尽可能选择距离当前点较远的点放置服务器。DFS标记能够覆盖到的叶子节点,循环直到所有叶子节点被覆盖。

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;

#define REP(i,n) for(int i=0;i<(n);i++)

const int MAXN = 1e3+10;

struct Point{
    int fa;
    int ra=INF;
    int cov;
}tree[MAXN];

vector<int> gra[MAXN],node[MAXN];
int visit[MAXN];
int n,s,k;
int maxr;
int ans;

void init(int s){
    //printf("s:%d,rank:%d\n",s,tree[s].ra);
    for(int i=0;i<gra[s].size();i++){
        if(tree[gra[s][i]].ra>tree[s].ra){
            tree[gra[s][i]].fa =s;
            tree[gra[s][i]].ra =tree[s].ra+1;
            tree[gra[s][i]].cov =0;
            if(gra[gra[s][i]].size()==1){
                //printf("gra:%d\n",gra[s][i]);
                node[tree[s].ra+1].push_back(gra[s][i]);
            }
        }
        maxr = max(maxr,tree[s].ra+1);
    }
    for(int i=0;i<gra[s].size();i++){   
        if(visit[gra[s][i]]==0){
            visit[gra[s][i]]=1; 
            init(gra[s][i]);    
        }
    }
    return ;
}

void dfs(int now,int d){
    tree[now].cov = 1;
    if(d>=1){
        if(now!=s) dfs(tree[now].fa,d-1);
        for(int i=0;i<gra[now].size();i++){
            dfs(gra[now][i],d-1);
        }
    }
    
    return ;
}

void solve(){
    ans = 0;
    dfs(s,k);
    for(int i=maxr;i>0;i--){
        for(int j=0;j<node[i].size();j++){
            if(tree[node[i][j]].cov!=1){
                int cur = node[i][j];
                for(int p = 0;p<k&&cur!=-1;p++){
                    cur = tree[cur].fa;
                }
                ans++;
                dfs(cur,k);
            }
        }
    }
    return ;
}

int main(){
    int T = 0;
    scanf("%d",&T);
    while(T--){
        maxr = 0;
        memset(gra,0,sizeof(gra));
        memset(node,0,sizeof(node));
        memset(visit,0,sizeof(visit));
        for(int i=0;i<MAXN;i++){
            tree[i].ra=INF;
            tree[i].cov =0;
            tree[i].fa =-1;
        }
        scanf("%d %d %d",&n,&s,&k);
        int a,b;
        for(int i=0;i<n-1;i++){
            scanf("%d %d",&a,&b);
            gra[a].push_back(b);
            gra[b].push_back(a);
        }
        tree[s].ra=0;
        node[0].push_back(s);
        visit[s]=1;
        init(s);
        solve();
        printf("%d\n",ans);
    }
    
    return 0;
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!