当前位置:网站首页>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
边栏推荐
- Too many files with unapproved license
- PCB中常用快捷键
- Exercise 8-2 calculate the sum and difference of two numbers
- 中国PETG市场预测及战略研究报告(2022版)
- allegro,orcad, net alias,port,off-page connector之间的异同点和如何选取
- Sendmail can't send mail and it's too slow to send. Solve it
- 论文分享:Generating Playful Palettes from Images
- 常见问题之PHP——ldap_add(): Add: Undefined attribute type in
- Frequently asked questions: PHP LDAP_ add(): Add: Undefined attribute type in
- Sub GHz wireless solution Z-Wave 800 Series zg23 SOC and zgm230s modules
猜你喜欢

Exercise 10-8 recursive implementation of sequential output of integers

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

修改数据库中的记录为什么报这个错

Fabric. JS document

Interface for querying IP home

556. 下一个更大元素 III

MySQL multi table query subquery

Polestar美股上市:5.5万台交付如何支持得起超200亿美元估值

7-9 find a small ball with a balance

Sub GHz wireless solution Z-Wave 800 Series zg23 SOC and zgm230s modules
随机推荐
6-9 statistics of single digits (15 points)
Four data flows and cases of grpc
js 2023. String pair equal to the target string after connection
中感微冲刺科创板:年营收2.4亿净亏1782万 拟募资6亿
数学常数表 by q779
Bibit pharmaceutical rushed to the scientific innovation board: annual revenue of 970000, loss of 137million, proposed to raise 2billion
puzzle(016.4)多米诺效应
Exercise 6-2 using functions to sum special A-string sequences
Exercise 6-1 classify and count the number of characters
Mysql多表查询 #子查询
动态获取权限
【北大青鸟昌平校区】互联网行业中,哪些岗位越老越吃香?
JS matrix zero
超简单手机地图开发
Interface for querying IP home
C library function - qsort()
Concat and concat_ Ws() differences and groups_ Use of concat() and repeat() functions
洛谷P3065 [USACO12DEC]First! G 题解
Exercise 10-6 recursively find Fabonacci sequence
Exercise 10-8 recursive implementation of sequential output of integers