当前位置:网站首页>[atcoder1981] short diameter (graph theory thinking)
[atcoder1981] short diameter (graph theory thinking)
2022-06-11 07:36:00 【CaptainHarryChen】
The question
Give a tree , It is required to delete the minimum points , Make the tree connected and the diameter less than or equal to K
(N<=2000)
Answer key
Simple problems are also easy to think complex .
about K For the even , Enumerate a point , Will be away from this point >K/2 The condition can be satisfied by deleting all of , Take the minimum number of deletion points .
about K It's odd , Enumerate an edge , The tree is divided into two by this side , Change its depth >(K-1)/2 Delete all of , Take the minimum number of deletion points .
The exam was complicated : Find the diameter , Then try to delete a dot from both ends of the diameter , Contains a number of special cases ... A dead end
Code
#include<cstdio> #include<vector> using namespace std; const int MAXN=2005; int N,K; vector<int> adj[MAXN]; int dep[MAXN],fa[MAXN],cnt; void dfs(int u,int f,int lim) {
for(int i=0;i<(int)adj[u].size();i++) {
int v=adj[u][i]; if(v!=f) {
dep[v]=dep[u]+1; if(dep[v]>lim) cnt++; dfs(v,u,lim); } } } int main() {
scanf("%d%d",&N,&K); for(int i=1;i<N;i++) {
int u,v; scanf("%d%d",&u,&v); adj[u].push_back(v); adj[v].push_back(u); } int ans=0x3F3F3F3F; if(K%2==0) {
for(int i=1;i<=N;i++) {
cnt=0; dep[i]=0;dfs(i,0,K/2); ans=min(ans,cnt); } } else {
for(int i=1;i<=N;i++) for(int j=0;j<(int)adj[i].size();j++) {
cnt=0; dep[i]=0;dfs(i,adj[i][j],K/2); dep[adj[i][j]]=0;dfs(adj[i][j],i,K/2); ans=min(ans,cnt); } } printf("%d\n",ans); return 0; } 边栏推荐
- Arduino_ Esp32 development record
- Find the longest common substring of N arrays (strings)
- C language volatile
- Post-processing of ffmpeg miscellaneous notes
- MySQL设置管理员密码无法生效的案例一则
- 群晖DS918创建m.2 固态硬盘SSD读写缓存
- Aiop introduction
- Building a full-featured NAS server with raspberry pie (06): built-in file synchronization tool for penetration
- 【AtCoder1998】Stamp Rally(整体二分+并查集)
- If you want to save an IP address, what data type is better? 99% of people will answer wrong!
猜你喜欢

2. Graduated from this course, and the bank has outsourced testing work for more than 4 months. Talk about some real feelings

JVM learning record (VII) -- class loading process and parental delegation model

Paging of the flask page

Sdl-2 thread logic
![[IOT] intelligent hardware: how to obtain the WiFi signal strength of hardware products](/img/85/5766d8269391820b5e142178530657.png)
[IOT] intelligent hardware: how to obtain the WiFi signal strength of hardware products

Nim product

Summary of classic interview questions

Multi thread review summary parsing volatile keyword

QT table display data

C language to write a calculator calculation logic
随机推荐
. Net C Foundation (6): namespace - scope with name
C language Yanghui triangle code
.NET C#基础(6):命名空间 - 有名字的作用域
After 4 years of naked resignation from the test, the test post of 15K interview was rubbed on the ground, and the result made me collapse and cry
Import on CSDN MD file
Use definite integral to calculate triangle area
C language three chess games
QT picture adaptive display control
零基础自学SQL课程 | OUTER JOIN外连接
Use of wordcloud
Rabin-Miller素数测试
Android and IOS reverse analysis / security detection / penetration testing framework
Tetris preliminary
JVM learning record (VII) -- class loading process and parental delegation model
Regular Expression Matching
Nim product
Cartland number application
May 30-June 5, 2022 AI industry weekly (issue 100): three years
Ffmpeg extraction package format extracts AAC and customizes adtc header to realize arbitrary frame decoding
MFC custom string linked list