当前位置:网站首页>洛谷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;
}
转载请说明出处
边栏推荐
- Current situation, analysis and prediction of information and innovation industry
- x86汇编语言-从实模式到保护模式 笔记
- Recent learning summary
- Understand the application scenario and implementation mechanism of differential segment
- 7-28 monkeys choose King (Joseph problem)
- 泰凌冲刺科创板:拟募资13亿 国家大基金与小米长江是股东
- Exercise 8-7 string sorting
- JVM垃圾回收机
- concat和concat_ws()区别及group_concat()和repeat()函数的使用
- Exercise 9-3 plane vector addition
猜你喜欢

泰凌冲刺科创板:拟募资13亿 国家大基金与小米长江是股东

Eight sorts

Exercise 10-2 recursive factorial sum

Why are grass-roots colleges and universities with "soil and poverty" called "Northeast small Tsinghua"?

Global event bus

7-8 overspeed judgment

MySQL multi table query subquery

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

Exercise 8-7 string sorting

Generate directories from web content
随机推荐
添加Zabbix计算类型项目Calculated items
Interface for querying IP home
Too many files with unapproved license
Raft 协议
Mysql多表查询 #子查询
JS get DPI, PX to cm, cm to PX
Doris学习笔记之数据表的创建
超简单手机地图开发
Find the sum of the elements of each row of the matrix
Article content typesetting and code highlighting
7-2 and then what time (15 minutes)
JS matrix zero
Recent learning summary
7-6 mixed type data format input
fpga阻塞赋值和非阻塞赋值
LNMP环境mail函数不能发送邮件解决
别再问自己适不适合做软件测试了
String substitution
中国PETG市场预测及战略研究报告(2022版)
7-24 reduction of the simplest fraction (rolling Division)