与树中点的距离相关的两题 1.中间的连续点离边界距离最小 2.寻找有标记的最远的两个点

comej oj:添加链接描述
问题: 一共有n个城市构成一棵树,其中有k个核心城市,这k个核心城市是连在一起的,要让非核心城市离核心城市的最大距离最小。
思路:从最外层 一层一层 向内搜,知道搜完 n-k 个点(i.e 搜完非核心城市)就退出, 因为用的是bfs,所以非核心城市与核心城市的距离一定是最小的。

#include <bits/stdc++.h>
#define ms(x, n) memset(x,n,sizeof(x));
#define rep(i, n) for(int i = 0; i < (int)n; ++i)
#define rep1(i, n) for(int i = 1; i <= (int)n; ++i)
#define pb push_back
#define PI acos(-1.0)
#define LONG_LONG_MAX 1LL<<62
#define LONG_LONG_MIN -LONG_LONG_MIN - 1
typedef long long int ll;
typedef unsigned long long int ull;
using namespace std;

const int maxn = 1e5 + 10;
int n, k, cnt[maxn], a, b;
vector<int> v[maxn];

int main() {
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    ms(cnt, 0);
    cin >> n >> k;
    rep(i, n-1) {
        cin >> a >> b;
        v[a].pb(b);
        v[b].pb(a);
        ++cnt[b];
        ++cnt[a];
    }
    if (k == n) {
        cout << 0 << endl;
        return 0;
    }
    if (k == n-1) {
        cout << 1 << endl;
        return 0;
    }
    queue<pair<int, int>> q;
    for (int i = 1; i <= n; ++i) {
        if (cnt[i] == 1) {
            q.push(make_pair(i, 1));
        }
    }
    k = n - k;
    int ans;
    while (true) {
        int cur1 = q.front().first;
        int cur2 = q.front().second;
        q.pop();
        --k;
        if (k == 0) {
            ans = cur2;
            break;
        }
        for (auto next : v[cur1]) {
            --cnt[next];
            if (cnt[next] == 1) { //这层的点都搜完了,往下一层开始搜
                q.push(make_pair(next, cur2+1));
            }
        }
    }
    cout << ans << endl;
    return 0;
}

牛客:添加链接描述
题目:一共n个点构成一棵树,有k个人分布在k个点上(不一定要在连续的点上),他们要聚在一起,问花费的最小时间是多少。(一个人走到另一个相邻的点所耗时间为1)
思路:标记人在的位置,通过两次bfs求出最远的两个人的直径,最后将直径除以2,再向上取整即为答案。

#include <bits/stdc++.h>
#define ms(x, n) memset(x,n,sizeof(x));
#define rep(i, n) for(int i = 0; i < (int)n; ++i)
#define rep1(i, n) for(int i = 1; i <= (int)n; ++i)
#define pb push_back
#define PI acos(-1.0)
#define LONG_LONG_MAX 1LL<<62
#define LONG_LONG_MIN -LONG_LONG_MIN - 1
typedef long long int ll;
typedef unsigned long long int ull;
using namespace std;

const int maxn = 1e5 + 10;
int n, k, a, b, sgn[maxn], vis[maxn], pos, max_dis;
vector<int> v[maxn];

void bfs(int x) {
    ms(vis, 0);
    int cur1, cur2, next;
    queue<pair<int, int>> q;
    q.push(make_pair(x, 0));
    vis[x] = 1;
    while (!q.empty()) {
        cur1 = q.front().first;
        cur2 = q.front().second;
        q.pop();
        for (auto next : v[cur1]) {
            if (!vis[next]) {
                vis[next] = 1;
                if (sgn[next] == 1) {
                    if (cur2 + 1 > max_dis) {
                        max_dis = cur2 + 1;
                        pos = next;
                    }
                }
                q.push(make_pair(next, cur2+1));
            }
        }
    }
}

int main() {
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin >> n >> k;
    ms(sgn, 0);
    rep(i, n-1) {
        cin >> a >> b;
        v[a].pb(b);
        v[b].pb(a);
    }
    rep(i, k) {
        cin >> a;
        sgn[a] = 1;
    }
    max_dis = 0;
    bfs(a);
    max_dis = 0;
    int temp_pos = pos;
    bfs(pos);
    cout << (max_dis + 1) / 2 << endl;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值