UVA1267 - Network

题意:n台及其连成一个树状网络,其中叶结点是客户端,其他结点是服务器。已经有一台服务器提供VOD服务,要求出最少在几个记得点放置服务器,使得所有客户端到服务器的距离都不超过k。

思路:先将无根树状化为有根树。之后就利用贪心,找到深度最大的结点u,每次放置服务器的地方都要放在u的k级祖先,这样是最划算的。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>

using namespace std;

const int MAXN = 1005;
vector<int> gr[MAXN], nodes[MAXN];
int n, s, k, fa[MAXN];
bool vis[MAXN];

void dfs(int u, int f, int d) {
    fa[u] = f;
    int nc = gr[u].size();
    if (nc == 1 && d > k)
        nodes[d].push_back(u);  //利用nodes表可以省去对于叶子结点深度的搜索
    for (int i = 0; i < nc; i++)  {
        int v = gr[u][i];
        if (v != f)
            dfs(v, u, d + 1);
    }
}

void dfs_cover(int u, int f, int d) {
    vis[u] = true;
    int nc = gr[u].size();
    for (int i = 0; i < nc; i++) {
        int v = gr[u][i];
        if (v != f && d < k)
            dfs_cover(v, u, d + 1);
    }
}

int solve() {
    int ans = 0;
    memset(vis, 0, sizeof(vis));
    for (int d = n - 1; d > k; d--)
        for (int i = 0; i < nodes[d].size(); i++) {
            int u = nodes[d][i];
            if (vis[u])
                continue;
            int v = u;
            for (int j = 0; j < k; j++)
                v = fa[v];
            dfs_cover(v, -1, 0);
            ans++;
        }
    return ans;
}

int main() {
    int cas;
    scanf("%d", &cas);
    while (cas--) {
        scanf("%d%d%d", &n, &s, &k);
        for (int i = 1; i <= n; i++) {
            gr[i].clear();
            nodes[i].clear();
        }
        for (int i = 0; i < n - 1; i++) {
            int a, b;
            scanf("%d%d", &a, &b);
            gr[a].push_back(b);
            gr[b].push_back(a);
        }
        dfs(s, -1, 0);
        int ans = solve();
        printf("%d\n", ans);
    }
    return 0;
}

UVA1267 - Network

时间: 2024-08-01 07:14:27

UVA1267 - Network的相关文章

ubuntu开机出现waiting for network configuration

ubuntu启动时,出现waiting for network configuration,waiting up to 60 more seconds for network configuration等,进入桌面后网络图标也不见了 解决方法,首先在 /etc/network/interfaces 文件里面无关的都删去,留下lo这个 然后再到/etc/init/failsafe.conf文件里将sleep59改成5或10,sleep50意思是等待59秒

Linux 性能监控 : CPU 、Memory 、 IO 、Network

一.CPU 1.良好状态指标 CPU利用率:User Time <= 70%,System Time <= 35%,User Time + System Time <= 70% 上下文切换:与CPU利用率相关联,如果CPU利用率状态良好,大量的上下文切换也是可以接受的 可运行队列:每个处理器的可运行队列<=3个线程 2.监控工具 vmstat $ vmstat 1 procs -----------memory---------- ---swap-- -----io---- --s

深度学习方法(十):卷积神经网络结构变化——Maxout Networks,Network In Network,Global Average Pooling

技术交流QQ群:433250724,欢迎对算法.技术感兴趣的同学加入. 最近接下来几篇博文会回到神经网络结构的讨论上来,前面我在"深度学习方法(五):卷积神经网络CNN经典模型整理Lenet,Alexnet,Googlenet,VGG,Deep Residual Learning"一文中介绍了经典的CNN网络结构模型,这些可以说已经是家喻户晓的网络结构,在那一文结尾,我提到"是时候动一动卷积计算的形式了",原因是很多工作证明了,在基本的CNN卷积计算模式之外,很多简

qt.network.ssl: QSslSocket: cannot call unresolved function SSLv23_client_method

最近在做一个网络音乐播放器时,由于出现qt.network.ssl: QSslSocket: cannot call unresolved function SSLv23_client_method, 而不能播放网络歌曲. 上网搜了半天,都说要在电脑那里安装openssl,然后把C:\OpenSSL-Win64\bin下的libeay32.dll和ssleay32.dll拷贝到D:\Qt\Qt5.4.2\5.4\mingw491_32\bin, 然而并没什么卵用! 我的解决办法是: Qt的这个目

bzoj1834: [ZJOI2010]network 网络扩容

努力看了很久样例一直过不了...然后各种输出中间过程啊巴拉巴拉弄了1h,没办法了...然后突然想到啊原来的边可以用啊为什么不用...于是A了...感人肺腑 #include<cstdio> #include<cstring> #include<queue> #include<iostream> #include<algorithm> using namespace std; #define rep(i,n) for(int i=1;i<=n

linux 6.x network device not active

[[email protected] Desktop]# service networkrestart Shutting down interface eth0: Error: Device\'eth0\' (/org/freedesktop/NetworkManager/Devices/0)disconnecting failed: This device is not active [FAILED] Shutting down loopback interface: [ OK ] Bring

centos linux中怎么查看和修改计算机名/etc/sysconfig/network

centos linux中怎么查看和修改计算机名 查看计算机名:在终端输入hostname 修改的话 hostname +计算机名(重启后失效)要永久修改的话要修改配置文件/etc/sysconfig/network修改hostname=你要改的名字

Computer Network学习笔记_4

3-2_Retransmissions 主讲ARQ,主要用在错误普遍存在而且必须改正的情况,WiFi.TCP都用.ARQ的规则就是recerver收到正确帧要自动回发ACK,sender如果没有在timeout内收到ACK就重发Frame.ARQ有两个问题,一个是timeout定多长,一个是帧重复.Timeout定的时间不能太长太短,解决帧重复就是在Frames和ACKs中加sequence numbers.帧重复的问题是如果sender发的数据receiver收到然后回发的ACK超时,那么se

Qt 5.4错误"qt.network.ssl: QSslSocket: cannot call"

今天研读QQStars(http://www.qtcn.org/bbs/read.php?tid-57944.html)时,发现这个软件登陆不上,总是提示: qt.network.ssl: QSslSocket: cannot call unresolved function ERR_get_error qt.network.ssl: QSslSocket: cannot call unresolved function TLSv1_client_method qt.network.ssl: Q