当前位置:网站首页>洛谷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-24 reduction of the simplest fraction (rolling Division)
- js 2023. String pair equal to the target string after connection
- 常见问题之PHP——ldap_add(): Add: Undefined attribute type in
- 中国PETG市场预测及战略研究报告(2022版)
- 好看、好用、强大的手写笔记软件综合评测:Notability、GoodNotes、MarginNote、随手写、Notes Writers、CollaNote、CollaNote、Prodrafts、Noteshelf、FlowUs、OneNote、苹果备忘录
- 全文检索引擎Solr系列—–全文检索基本原理
- Exercise 6-2 using functions to sum special A-string sequences
- Raft 协议
- JS matrix zero
- protobuf与grpc
猜你喜欢

Eight sorts

Redis: commandes d'action pour les données de type chaîne

7-15 calculation of PI

Print. JS -- web page file printing

中感微冲刺科创板:年营收2.4亿净亏1782万 拟募资6亿

x86汇编语言-从实模式到保护模式 笔记

JVM runtime data area

JS matrix zero

allegro,orcad, net alias,port,off-page connector之间的异同点和如何选取

Redis: operation command of string type data
随机推荐
allegro,orcad, net alias,port,off-page connector之间的异同点和如何选取
Raft agreement
Exercise 9-1 time conversion
7-6 mixed type data format input
常见问题之PHP——ldap_add(): Add: Undefined attribute type in
Programmable logic device software testing
Interface for querying IP home
Exercise 10-2 recursive factorial sum
Learn to punch in today
J-luggage lock of ICPC Shenyang station in 2021 regional games (simple code)
Leetcode(4)——尋找兩個正序數組的中比特數
Exercise 10-3 recursive implementation of exponential functions
Concat and concat_ Ws() differences and groups_ Use of concat() and repeat() functions
超简单手机地图开发
全文检索引擎Solr系列—–全文检索基本原理
Reflection -- basic usage
Similarities and differences between Allegro, OrCAD, net alias, port, off page connector and how to select them
ZABBIX saves the page blank after adding calculated items
TS code automatically generates JS
JVM garbage collector