当前位置:网站首页>洛谷P5536 【XR-3】核心城市 题解
洛谷P5536 【XR-3】核心城市 题解
2022-07-03 14:15:00 【q779】
洛谷P5536 【XR-3】核心城市 题解
题目链接:P5536 【XR-3】核心城市
题意:
X 国有 n n n 座城市, n − 1 n - 1 n−1 条长度为 1 1 1 的道路,每条道路连接两座城市,且任意两座城市都能通过若干条道路相互到达,显然,城市和道路形成了一棵树。
X 国国王决定将 k k k 座城市钦定为 X 国的核心城市,这 k k k 座城市需满足以下两个条件:
- 这 k k k 座城市可以通过道路,在不经过其他城市的情况下两两相互到达。
- 定义某个非核心城市与这 k k k 座核心城市的距离为,这座城市与 k k k 座核心城市的距离的最小值。那么所有非核心城市中,与核心城市的距离最大的城市,其与核心城市的距离最小。你需要求出这个最小值。
数据范围:
- 1 ≤ k < n ≤ 1 0 5 1 \le k < n \le 10 ^ 5 1≤k<n≤105。
- 1 ≤ u , v ≤ n , u ≠ v 1 \le u,v \le n, u \ne v 1≤u,v≤n,u=v,保证城市与道路形成一棵树。
不要被树这个东西坑了,其实和树关系不大
我们感性地分析一下,这些核心城市一定是在比较中间的位置
然后这些核心城市到所有边缘结点的距离是相差不超过 1 1 1 的
那么我们是不是可以把这个树从外面一圈开始消
然后一个个点的删,最后剩下 k k k 个点,就是核心城市
这是什么?拓扑排序对吧
时间复杂度 O ( n ) O(n) O(n)
代码:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iomanip>
#include <queue>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
#define N (int)(1e5+15)
queue<int> q;
struct Edge{
int u,v,next;}e[N<<1];
int n,k,in[N],head[N],pos=1,ans=0,dep[N],vis[N];
void addEdge(int u,int v)
{
e[++pos]={
u,v,head[u]};
head[u]=pos;++in[v];
}
void topo()
{
while(!q.empty())
{
int u=q.front();q.pop();
for(int i=head[u]; i; i=e[i].next)
{
int v=e[i].v;
if(--in[v]!=1)continue;
if(!vis[v])
{
vis[v]=1;
dep[v]=dep[u]+1;
ans=max(ans,dep[v]);
q.push(v);--k;
if(k<1)return;
}
}
}
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
// freopen("check.in","r",stdin);
// freopen("check.out","w",stdout);
cin >> n >> k; k=n-k; // 转化一下意义
for(int i=1,u,v; i<n; i++)
{
cin >> u >> v;
addEdge(u,v);addEdge(v,u);
}
for(int i=1; i<=n; i++)
if(in[i]==1&&k>=1)
{
q.push(i);vis[i]=1;
--k;ans=1;dep[i]=1;
}
if(k)topo();
cout << ans << '\n';
return 0;
}
转载请说明出处
边栏推荐
- 7-20 print 99 formula table (format output)
- 7-14 sum integer segments (C language)
- Redis: redis data structure and key operation commands
- [Jilin University] information sharing of postgraduate entrance examination and re examination
- 超简单手机地图开发
- Leetcode (4) - - trouver la médiane de deux tableaux ordonnés positifs
- 常见问题之PHP——ldap_add(): Add: Undefined attribute type in
- JS Part III
- Exercise 10-3 recursive implementation of exponential functions
- Strategy, tactics (and OKR)
猜你喜欢

Print. JS -- web page file printing

The small project (servlet+jsp+mysql+el+jstl) completes a servlet with login function, with the operation of adding, deleting, modifying and querying. Realize login authentication, prevent illegal log

7-8 overspeed judgment

Scroll detection of the navigation bar enables the navigation bar to slide and fix with no content

TS code automatically generates JS

7-9 find a small ball with a balance

GRPC的四种数据流以及案例

Doris学习笔记之数据表的创建

Comprehensive evaluation of good-looking, easy-to-use and powerful handwriting note taking software: notability, goodnotes, marginnote, handwriting, notes writers, collanote, collanote, prodrafts, not

Leetcode (4) -- find the median of two positively ordered arrays
随机推荐
Raft agreement
牛客网:过河卒
7-22 tortoise and rabbit race (result oriented)
Common mixins
适用于XP的DDK
String substitution
7-15 calculation of PI
Sendmail can't send mail and it's too slow to send. Solve it
Similarities and differences between Allegro, OrCAD, net alias, port, off page connector and how to select them
Exercise 10-1 judge the three digits that meet the conditions
虽然不一定最优秀,但一定是最努力的!
Etcd cluster permission management and account password usage
Exercise 9-3 plane vector addition
Invalid Z-index problem
Exercise 7-6 count capital consonants
Understand the application scenario and implementation mechanism of differential segment
JVM runtime data area
愉悦资本新双币基金近40亿元完成首次关账
Thread. Sleep and timeunit SECONDS. The difference between sleep
SSH访问控制,多次失败登录即封掉IP,防止暴力破解