当前位置:网站首页>洛谷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;
}
转载请说明出处
边栏推荐
- Why don't I have a rookie medal
- x86汇编语言-从实模式到保护模式 笔记
- Exercise 9-3 plane vector addition
- Scroll detection of the navigation bar enables the navigation bar to slide and fix with no content
- String substitution
- Comprehensive evaluation of good-looking, easy-to-use and powerful handwriting note taking software: notability, goodnotes, marginnote, handwriting, notes writers, collanote, collanote, prodrafts, not
- Thread. Sleep and timeunit SECONDS. The difference between sleep
- How Facebook moves instagram from AWS to its own server
- Exercise 6-6 use a function to output an integer in reverse order
- Leetcode(4)——寻找两个正序数组的中位数
猜你喜欢
Global event bus
Leetcode(4)——寻找两个正序数组的中位数
concat和concat_ws()区别及group_concat()和repeat()函数的使用
Programming language: the essence of type system
玖逸云黑免费无加密版本源码
Leetcode (4) -- find the median of two positively ordered arrays
Doris学习笔记之数据表的创建
Thinking about the arrangement problem in the backtracking problem (leetcode questions 46 and 47)
Print. JS -- web page file printing
7-18 finding the single root of polynomial by dichotomy
随机推荐
Similarities and differences between Allegro, OrCAD, net alias, port, off page connector and how to select them
别再问自己适不适合做软件测试了
Find the sum of the elements of each row of the matrix
7-15 calculation of PI
全文检索引擎Solr系列—–全文检索基本原理
concat和concat_ws()区别及group_concat()和repeat()函数的使用
Etcd cluster permission management and account password usage
Exercise 10-6 recursively find Fabonacci sequence
Common mixins
JVM garbage collector
适用于XP的DDK
Exercise 9-3 plane vector addition
Exercise 6-6 use a function to output an integer in reverse order
Sendmail无法发送邮件及发送过慢解决
Onmenusharetimeline custom shared content is invalid, and the title and icon are not displayed
Exercise 6-2 using functions to sum special A-string sequences
7-14 sum integer segments (C language)
Print. JS -- web page file printing
Solr series of full-text search engines - basic principles of full-text search
Exercise 8-7 string sorting