当前位置:网站首页>[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; } 边栏推荐
- MFC debugger OutputDebugString override
- Configuration software -- control drag and drop
- 2022 low voltage electrician test questions and online simulation test
- 2022.5.30-6.5 AI行业周刊(第100期):三年时光
- What is the difference between gaussdb for redis and redis?
- 【Oracle 数据库】奶妈式教程day03 排序查询
- [cluster] haproxy load balancing
- [IOT] intelligent hardware: how to obtain the WiFi signal strength of hardware products
- [Oracle database] mammy tutorial day03 Sorting Query
- Sdl-4 PCM playback
猜你喜欢

【软件测试】这样的简历已经刷掉了90%的面试者
![20200727 T2 small w playing game [generating function (binomial inversion technique)]](/img/a5/ae2192f4f37232cdcb01e81ad0297c.jpg)
20200727 T2 small w playing game [generating function (binomial inversion technique)]

Use definite integral to calculate triangle area
![2020080 simulation competition [horizontal and vertical coordinates do not affect each other, cactus minimum cut, combined meaning translation formula]](/img/4d/a67a63d2c4eb80c98315c3057b01b9.jpg)
2020080 simulation competition [horizontal and vertical coordinates do not affect each other, cactus minimum cut, combined meaning translation formula]

Building a full-featured NAS server with raspberry pie (06): built-in file synchronization tool for penetration
![[STL source code analysis] summary note (2): overview of containers](/img/66/60fba564ae6020dfb503c7fdf78529.jpg)
[STL source code analysis] summary note (2): overview of containers

Sdl-2 thread logic

Decimal to binary

2022年熔化焊接与热切割考试练习题及答案
![20200803 T3 my friends [divide and conquer NTT optimization recursion]](/img/35/01201e3136e3dd5cd562a0481f1ee9.jpg)
20200803 T3 my friends [divide and conquer NTT optimization recursion]
随机推荐
[IOT] project management: how to build a better cross functional team?
Sdl-2 thread logic
Directrix of ellipse
Uoj 554 [unr 4] challenges Hamilton [find Hamilton path (adjustment method)]
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
Rabin-Miller素数测试
[cluster] lvs+keepalived high availability cluster
Crmeb/v4.4 Standard Version open version mall source code applet official account h5+app mall source code
Seata的几种事务模式
【集群】LVS+keepalived高可用集群
QObject usage skills -- control function class
C language three chess games
[STL source code analysis] summary note (2): overview of containers
Wc2020 course selection
MFC debugger OutputDebugString override
Uoj 553 [unr 4] caproic acid set [computational geometry (points in circle → points in half plane)]
Ffmpe a small demo to understand 80% of common APIs
[software testing] 90% of the interviewers have been brushed out of such resumes
2022 low voltage electrician operation certificate test question simulation test platform operation
QT picture adaptive display control