Uva 3902 Network

题目大意:

在非叶子节点上安装最少的服务器使得,每个叶子节点到服务器的距离不超过k。

贪心+图上的dfs。 先从深度最大的叶子节点开始找。找到父节点后再用这个父节点进行扩充。

/* ***********************************************
Author        :guanjun
Created Time  :2016/5/10 23:15:38
File Name     :7.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 10010
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;
int n,s,k;
vector<int>gr[maxn];
vector<int>nod[maxn];
int fa[maxn];
int vis[maxn];
void dfs(int f,int u,int d){
    fa[u]=f;
    int x=gr[u].size();
    if(x==1&&d>k)nod[d].push_back(u);
    for(int i=0;i<x;i++){
        int v=gr[u][i];
        if(v!=f)dfs(u,v,d+1);
    }
}
void dfs1(int f,int u,int d){
    vis[u]=1;
    int x=gr[u].size();
    for(int i=0;i<x;i++){
        int v=gr[u][i];
        if(v!=f&&d<k)dfs1(u,v,d+1);
    }
}
int solve(){
    int ans=0;
    cle(vis);
    for(int d=n-1;d>k;d--){
        for(int j=0;j<nod[d].size();j++){
            int u=nod[d][j];
            if(vis[u])continue;
            int v=u;
            for(int i=0;i<k;i++)v=fa[v];
            ans++;
            dfs1(-1,v,0);
        }
    }
    return ans;
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif
    //freopen("out.txt","w",stdout);
    int t,a,b;
    cin>>t;
    while(t--){
        cin>>n>>s>>k;
        cle(fa);
        for(int i=0;i<=n;i++)gr[i].clear(),nod[i].clear();
        for(int i=1;i<n;i++){
            scanf("%d%d",&a,&b);
            gr[a].push_back(b);
            gr[b].push_back(a);
        }
        dfs(-1,s,0);
        printf("%d\n",solve());
    }
    return 0;
}
时间: 2024-08-23 06:01:20

Uva 3902 Network的相关文章

[2016-03-19][UVALive][3902][Network]

时间:2016-03-19 18:41:09 星期六 题目编号:[2016-03-19][UVALive][3902][Network] 题目大意:给定一个树状的图,一个服务器能覆盖k范围内的人,已知一个服务器的位置,问至少需要多少个服务器才能覆盖所有的叶子节点 分析: 以第一个服务器的点为根,把无根树转换为有根树 要使每个叶子节点都被覆盖,那么叶子节点的k级祖先内必须有一个服务器 要使服务器最少,服务器应该放在最远的位置 方法: dfs1得到每层深度的节点(k层以内的就不用记录),同时记录每个

Live Archive 3902 Network 【持续更新】

3902 - Network Asia - Seoul - 2007/2008 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 ser

uva 1267 Network(DFS)

uva 1267 Network 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 VO

Uva 网络(Network,Seoul 2007,LA 3902)

1 #include<iostream> 2 #include<cstring> 3 #include<vector> 4 using namespace std; 5 6 const int maxn=1000+10; 7 int n,s,k; 8 vector<int> tree[maxn],nodes[maxn]; 9 int fa[maxn]; 10 bool covered[maxn]; 11 12 void dfs(int u,int f,int

uva 315 Network(连通图求割点)

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=251  Network  A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connecting several places numbered by integers

UVA - 315 Network 和 UVA - 10199 (求割顶)

链接 :  http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20837 http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=21278 求割顶的裸题. UVA - 315 #include <algorithm> #include <iostream> #include <sstream> #include <cstrin

UVA 315 Network(无向图求割点)

题目大意 A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connecting several places numbered by integers from 1 to N. No two places have the same number. The lines are bidirectional and always connect together two pl

UVA 318 Network(无向图求割点数)

Description A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connecting several places numbered by integers from 1 to N . No two places have the same number. The lines are bidirectional and always connect togethe

UVA 315 Network

Description: A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connecting several places numbered by integers from 1 to N. No two places have the same number. The lines are bidirectional and always connect togethe