算法竞赛入门经典训练指南 例题1-15 网络 Network UVALive 3902

题目:https://vjudge.net/problem/UVALive-3902

思路:贪心 + 无根树转有根树 + 搜索(我用的BFS)
以初始服务器为根节点算出各个叶节点的深度排好序,从深度最大的节点开始装服务,服务放在叶节点的k级祖先处,这样每个服务都能最大限度的覆盖叶节点,不断染色至所有叶节点都被染色。

具体实现上我先用邻接表存的图,然后遍历节点度数为1的节点记为叶节点。然后从初始服务器BFS建树将每个节点父节点记下,顺便利用BFS逐层扫描的特点将叶节点根据深度排好序,并记下叶节点数量。
先对初始服务器周围深度不超过k的叶节点预处理染色,然后从深度最大且没被染色过的叶节点找到k级祖先进行染色,直至全部叶节点都被染色,这一步我也是用BFS写的。

代码:c++

#include <cmath>
#include <map>
#include <set>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;

const int maxn = 10000 + 10;

int n, s, k;

map<int, set<int> > pic;

int fa[maxn];
map<int, vector<int> > leaves;
bool cover[maxn];
bool isleaf[maxn];
int leafnum;

void findleaf()
{
    leafnum = 0;
    for (int i = 1; i <= n; i++)
    {
        if (pic[i].size() == 1)
        {
            isleaf[i] = true;
            leafnum++;
        }
    }
}

void build()
{
    bool vis[maxn] = { 0 };
    queue<pair<int, int> > q;//index, step
    q.push(make_pair(s, 0));
    vis[s] = true;
    while (!q.empty())
    {
        int u = q.front().first;
        int step = q.front().second;
        q.pop();
        if (isleaf[u])
        {
            leaves[step].push_back(u);
        }
        else
        {
            for (auto t : pic[u])
            {
                if (!vis[t])
                {
                    vis[t] = true;
                    fa[t] = u;
                    q.push(make_pair(t, step + 1));
                }
            }
        }
    }
}

void coverleaf(int u)
{
    bool vis[maxn] = { 0 };
    queue<pair<int, int> > q;
    vis[u] = true;
    q.push(make_pair(u, 0));
    while (!q.empty())
    {
        int cur = q.front().first;
        int step = q.front().second;
        q.pop();
        if (step > k)
        {
            continue;
        }
        if (isleaf[cur] && !cover[cur])
        {
            cover[cur] = true;
            leafnum--;
        }
        if (!isleaf[cur])
        {
            for (auto t : pic[cur])
            {
                if (!vis[t])
                {
                    q.push(make_pair(t, step + 1));
                    vis[t] = true;
                }
            }
        }
    }
}

int findkfather(int u)
{
    for (int i = 0; i < k; i++)
    {
        u = fa[u];
    }
    return u;
}

int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        scanf("%d%d%d", &n, &s, &k);
        pic.clear();
        memset(cover, false, sizeof(cover));
        leaves.clear();
        memset(fa, 0, sizeof(fa));
        fa[s] = s;
        memset(isleaf, false, sizeof(isleaf));
        for (int i = 1; i <= n - 1; i++)
        {
            int a, b;
            scanf("%d%d", &a, &b);
            pic[a].insert(b);
            pic[b].insert(a);
        }
        findleaf();
        build();
        coverleaf(s);
        int ans = 0;
        for (map<int, vector<int> >::reverse_iterator it = leaves.rbegin(); it != leaves.rend(); it++)
        {
            for (auto t : it->second)
            {
                if (leafnum == 0)
                {
                    goto FINISH;
                }
                if (!cover[t])
                {
                    coverleaf(findkfather(t));
                    ans++;
                }
            }
        }
    FINISH:
        printf("%d\n", ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Rewriter_huanying/article/details/76690681