当前位置:网站首页>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
边栏推荐
- Tiantu investment sprint Hong Kong stocks: asset management scale of 24.9 billion, invested in xiaohongshu and Naixue
- 7-8 overspeed judgment
- Selective sorting
- retrofit
- NPM install is stuck with various strange errors of node NPY
- 7-2 and then what time (15 minutes)
- puzzle(016.4)多米诺效应
- Exercise 8-8 moving letters
- Raft agreement
- String sort
猜你喜欢
Frequently asked questions: PHP LDAP_ add(): Add: Undefined attribute type in
[clean up the extraordinary image of Disk C]
retrofit
NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
Accelerating strategy learning using parallel differentiable simulation
论文分享:Generating Playful Palettes from Images
Doris学习笔记之数据表的创建
7-18 finding the single root of polynomial by dichotomy
【北大青鸟昌平校区】互联网行业中,哪些岗位越老越吃香?
Exercise 6-2 using functions to sum special A-string sequences
随机推荐
洛谷P3065 [USACO12DEC]First! G 题解
US stock listing of polar: how can the delivery of 55000 units support the valuation of more than 20billion US dollars
npm install卡住与node-npy的各种奇怪报错
MongoDB数据库入门的常用命令
7-11 calculation of residential water charges by sections
必贝特医药冲刺科创板:年营收97万亏损1.37亿 拟募资20亿
Canvas utility library fabric JS user manual
7-19 check denomination (solve binary linear equation)
J-luggage lock of ICPC Shenyang station in 2021 regional games (simple code)
使用并行可微模拟加速策略学习
[clean up the extraordinary image of Disk C]
Niuke: crossing the river
Mysql多表查询 #子查询
剑指 Offer 28. 对称的二叉树
LNMP环境mail函数不能发送邮件解决
MySQL multi table query subquery
7-20 print 99 formula table (format output)
Raft 协议
ConstraintLayout 的使用
7-28 monkeys choose King (Joseph problem)