当前位置:网站首页>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
边栏推荐
猜你喜欢
![洛谷P5018 [NOIP2018 普及组] 对称二叉树 题解](/img/89/da1a3a38e02671628f385de0f30369.png)
洛谷P5018 [NOIP2018 普及组] 对称二叉树 题解

论文分享:Generating Playful Palettes from Images

Thinking about the arrangement problem in the backtracking problem (leetcode questions 46 and 47)

Zhonggan micro sprint technology innovation board: annual revenue of 240million, net loss of 17.82 million, proposed to raise 600million

剑指 Offer 28. 对称的二叉树

Solution to failure or slow downloading of electron when electron uses electron builder to package

556. 下一个更大元素 III

Leetcode (4) -- find the median of two positively ordered arrays

JS matrix zero

Exercise 8-8 moving letters
随机推荐
J-luggage lock of ICPC Shenyang station in 2021 regional games (simple code)
ConstraintLayout 的使用
7-9 find a small ball with a balance
retrofit
中国PETG市场预测及战略研究报告(2022版)
Leetcode (4) -- find the median of two positively ordered arrays
7-14 sum integer segments (C language)
天谋科技 Timecho 完成近亿元人民币天使轮融资,打造工业物联网原生时序数据库
TS code automatically generates JS
FPGA blocking assignment and non blocking assignment
别再问自己适不适合做软件测试了
Zabbix添加Calculated items后保存页面成空白
Preliminary summary of structure
Similarities and differences between Allegro, OrCAD, net alias, port, off page connector and how to select them
Niuke: crossing the river
Find specified characters
China PETG market forecast and Strategic Research Report (2022 Edition)
Polestar美股上市:5.5万台交付如何支持得起超200亿美元估值
牛客网:过河卒
Statistical capital consonants