关闭

UVALive - 3902 Network

73人阅读 评论(0) 收藏 举报
分类:

大白书的34页就是这道题。
附上链接:
http://acm.hust.edu.cn/vjudge/problem/16451

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.

题解:
这道题要求覆盖的是叶子节点,只需要把叶子节点全部覆盖就可以了,而且数据规模是1000,也就是说O(n^2)就可以了,我们可以采取贪心原则,我们把这个服务器选定为未覆盖的叶子节点的k级祖先是最优的。
题目是只需要覆盖叶子节点,我一开始是求覆盖了所有节点的(尴尬)。

#include<bits/stdc++.h>
using namespace std;

const int maxn=1e3+5;
vector<int> vec[maxn],dep[maxn];
int fa[maxn];
int n,s,k,maxdep;
bool cover[maxn];

void dfs(int now,int f,int d) {
    fa[now]=f;
    int len=(int)vec[now].size();
   if(d>k && len==1) dep[d].push_back(now);
    maxdep=max(maxdep,d);
    for(int i=0; i<len; ++i) {
        if(vec[now][i]^f)
            dfs(vec[now][i],now,d+1);
    }
}

void dfs1(int now,int f,int d) {
    if(d<=k) {
        cover[now]=true;
        int len=(int)vec[now].size();
        for(int i=0; i<len; ++i) {
            if(f^vec[now][i])
                dfs1(vec[now][i],now,d+1);
        }
    }
}

void work(){
    int ans=0;
    for(int i=maxdep;i>k;--i){
        int len=(int)dep[i].size();
        for(int j=0;j<len;++j){
            int to=dep[i][j];
            if(!cover[to]){
                ++ans;
//                cout<<"to="<<to<<endl;
                int getf=to;
                for(int z=0;z<k;++z){
                    getf=fa[getf];
//                    cout<<"getf1="<<getf<<endl;
                }
//                cout<<"getf="<<getf<<endl;
                dfs1(getf,-1,0);
            }
        }
    }
    printf("%d\n",ans);
}

int main() {
    int T,u,v;
    scanf("%d",&T);
    while(T--) {
        scanf("%d%d%d",&n,&s,&k);
        for(int i=0; i<=n; ++i) {
            vec[i].clear();
            dep[i].clear();
        }
        for(int i=0; i<n-1; ++i) {
            scanf("%d%d",&u,&v);
            vec[u].push_back(v);
            vec[v].push_back(u);
        }
        maxdep=0;
        dfs(s,-1,0);
        memset(cover,false,sizeof(cover));
        dfs1(s,-1,0);
        work();
    }
    return 0;
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:69748次
    • 积分:4310
    • 等级:
    • 排名:第6988名
    • 原创:357篇
    • 转载:6篇
    • 译文:0篇
    • 评论:30条
    博客专栏
    最新评论