# Comet OJ - Contest #9 & X Round 3 【XR-3】核心城市(树的直径，树的中心)

## 题目链接

#pragma comment(linker, "/STACK:102400000,102400000")

#include <iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<cmath>
#include<stack>
#include<string>

const int mod = 998244353;
const int maxn = 2e5 + 5;
const int inf = 1e9;
const long long onf = 1e18;
#define me(a, b) memset(a,b,sizeof(a))
#define lowbit(x) x&(-x)
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI 3.14159265358979323846
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
int dis[maxn], father[maxn];
int st, ed, tempmin;
bool vis[maxn];
struct node {
int v, next;
} tree[maxn];

struct Node {
int pos, dis;

bool friend operator<(Node a, Node b) {
return a.dis < b.dis;
}
};

void add_edge(int u, int v) {
}

void init() {
tot = 0;
}

void dfs(int u, int fa) {
if (dis[u] > tempmin)
tempmin = dis[u], ed = u;
for (int i = head[u]; i != -1; i = tree[i].next) {
int v = tree[i].v;
if (fa == v)
continue;
father[v] = u;
dis[v] = dis[u] + 1;
dfs(v, u);
}
}

int dfslen(int u, int fa) {
int ans = 0;
for (int i = head[u]; i != -1; i = tree[i].next) {
int v = tree[i].v;
if (fa == v)
continue;
int temp = dfslen(v, u) + 1;
ans = max(ans, temp);
dis[v] = temp;
}
return ans;
}

int get_root() {
st = 1, dis[1] = 0, tempmin = 0;
dfs(st, -1);
dis[ed] = 0, tempmin = 0, st = ed, father[ed] = -1;
dfs(st, -1);
int root, nowpos = ed, temp = inf;
while (nowpos != -1) {
//   cout<<dis[nowpos]<<" "<<dis[ed]-dis[nowpos]<<endl;
int Max = max(dis[nowpos], dis[ed] - dis[nowpos]);
if (Max < temp)
root = nowpos, temp = Max;
nowpos = father[nowpos];
}
return root;
}

int main() {
// freopen("1.in", "r", stdin);
//  freopen("1.out", "w", stdout);
int n, k;
cin >> n >> k;
init();
for (int i = 1; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
}
int root = get_root();///找树的中心
dfslen(root, -1);///处理出各点的子节点到该点的父节点的距离
priority_queue<Node> q;
q.push(Node{root, dis[root]});
vis[root] = true;
for (int i = 1; i <= k; i++) {
Node now = q.top();
q.pop();
for (int j = head[now.pos]; j != -1; j = tree[j].next) {
int v = tree[j].v;
if (!vis[v]) {
vis[v] = true;
q.push(Node{v, dis[v]});
}
}
}
if (q.empty())
puts("0");
else
printf("%d\n", q.top().dis);
return 0;
}

09-01 58

04-30 91

11-07 68

04-17 271

08-31 109

08-27 625

07-08 1098

03-29 106

04-16 1002

03-02 24

#### 洛谷P3761[TJOI2017]城市（树的直径、半径）

©️2020 CSDN 皮肤主题: 创作都市 设计师: CSDN官方博客

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