当前位置:网站首页>Luogu p5536 [xr-3] core city solution
Luogu p5536 [xr-3] core city solution
2022-07-03 14:27:00 【q779】
Luogu P5536 【XR-3】 The core city Answer key
Topic link :P5536 【XR-3】 The core city
The question :
X State-owned n n n city , n − 1 n - 1 n−1 The length of the bar is 1 1 1 The path of , Each road connects two cities , And any two cities can reach each other through several roads , obviously , Cities and roads form a tree .
X The king of the Kingdom decided to k k k The city was officially designated X The core city of , this k k k This city needs to meet the following two conditions :
- this k k k The city can pass through roads , Arrive in pairs without passing through other cities .
- Define a non core city and this k k k The distance between the core cities is , This city and k k k The minimum distance between two core cities . So in all non core cities , The city with the greatest distance from the core city , Its distance from the core city is the smallest . You need to find this minimum .
Data range :
- 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, Ensure that the city and the road form a tree .
Don't be fooled by the tree , In fact, it has nothing to do with trees
Let's analyze it sensibly , These core cities must be in the middle
Then the distance between these core cities and all edge nodes is no more than 1 1 1 Of
Then can we eliminate the tree from the outside
Then delete one by one , In the end k k k A little bit , Is the core city
What is it? ? Topological sort, right
Time complexity O ( n ) O(n) O(n)
Code :
#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; // Change the meaning
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;
}
Reprint please explain the source
边栏推荐
- MongoDB索引
- 2021年区域赛ICPC沈阳站J-Luggage Lock(代码简洁)
- Thread. Sleep and timeunit SECONDS. The difference between sleep
- 7-15 calculation of PI
- Add ZABBIX calculation type itemcalculated items
- Onmenusharetimeline custom shared content is invalid, and the title and icon are not displayed
- NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
- simpleParallax. JS (create poor visual effects for website pictures)
- Thread.sleep和TimeUnit.SECONDS.sleep的区别
- GRPC的四种数据流以及案例
猜你喜欢
JS matrix zero
【北大青鸟昌平校区】互联网行业中,哪些岗位越老越吃香?
Leetcode(4)——尋找兩個正序數組的中比特數
7-10 calculate salary
Sub-GHz无线解决方案Z-Wave 800 系列ZG23 soc和ZGM230S模块
Exercise 8-7 string sorting
基因家族特征分析 - 染色体定位分析
Tiantu investment sprint Hong Kong stocks: asset management scale of 24.9 billion, invested in xiaohongshu and Naixue
Exercise 6-6 use a function to output an integer in reverse order
Programming language: the essence of type system
随机推荐
Why is this error reported when modifying records in the database
洛谷P5018 [NOIP2018 普及组] 对称二叉树 题解
6-9 statistics of single digits (15 points)
7-6 mixed type data format input
Mongodb index
7-16 find the set of integers that meet the given conditions
NPM install is stuck with various strange errors of node NPY
The mail function of LNMP environment cannot send mail
LNMP环境mail函数不能发送邮件解决
编程语言:类型系统的本质
7-9 find a small ball with a balance
7-20 print 99 formula table (format output)
fpga阻塞赋值和非阻塞赋值
Niuke: crossing the river
Convert string to decimal integer
数学常数表 by q779
String sort
Raft 协议
FPGA blocking assignment and non blocking assignment
【北大青鸟昌平校区】互联网行业中,哪些岗位越老越吃香?