CF1092E Minimal Diameter Forest
和CF455C Civilization貌似挺像。
我們求出每棵樹的直徑和中點,找到直徑最大的樹後,將別的樹的中點與直徑最大的樹的中點相連,即可完成構造。
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int n,m,u,v,ans,p,mid,pos;
int f[N];
bool vis[N],jay;
int cnt,head[N];
struct edge{int next,to;}e[N<<1];
int id,tot;
struct number{int len,id;}num[N];
inline void add(int u,int v)
{
cnt++;
e[cnt].next=head[u];
e[cnt].to=v;
head[u]=cnt;
}
void dfs(int u,int fa,int now)
{
vis[u]=true;
if (ans<now) ans=now,p=u;
for (register int i=head[u]; i; i=e[i].next) if (e[i].to!=fa) dfs(e[i].to,u,now+1);
}
void dfs2(int u,int fa)
{
f[u]=1;
for (register int i=head[u]; i; i=e[i].next)
if (e[i].to!=fa)
{
dfs2(e[i].to,u);
f[u]=max(f[u],f[e[i].to]+1);
}
}
void dfs3(int u,int fa,int now)
{
if (now==mid) {pos=u; jay=true; return;}
for (register int i=head[u]; i; i=e[i].next)
if (e[i].to!=fa && f[e[i].to]+1==f[u])
{
dfs3(e[i].to,u,now+1);
if (jay) return;
}
}
int main(){
scanf("%d%d",&n,&m);
for (register int i=1; i<=m; ++i) scanf("%d%d",&u,&v),add(u,v),add(v,u);
for (register int i=1; i<=n; ++i)
if (!vis[i])
{
ans=-1; pos=i; p=i;
dfs(i,0,0);
dfs2(p,0);
jay=false;
mid=(f[p]+1)/2;
dfs3(p,0,1);
tot++; num[tot].len=f[p]; num[tot].id=pos;
}
id=1;
for (register int i=2; i<=tot; ++i) if (num[i].len>num[id].len) id=i;
for (register int i=1; i<=tot; ++i) if (i!=id) add(num[id].id,num[i].id),add(num[i].id,num[id].id);
ans=0; p=0; dfs(1,0,0);
ans=0; dfs(p,0,0);
printf("%d\n",ans);
for (register int i=1; i<=tot; ++i) if (i!=id) printf("%d %d\n",num[id].id,num[i].id);
return 0;
}
相關文章
- Clear Code for Minimal APIAPI
- [LeetCode] 543. Diameter of Binary TreeLeetCode
- LeetCode 543. Diameter of Binary TreeLeetCode
- HDU - 6736 F - Forest ProgramREST
- CF1192B Dynamic Diameter 題解
- CentOS 7 Minimal 安裝 LXQTCentOSQT
- 通俗理解.NET 6 Minimal APIsAPI
- Machine Learning(13)- Random ForestMacrandomREST
- HDU4941Magical Forest(map)REST
- .NET 6 Minimal API 的經驗分享API
- 1118. Birds in Forest (25)REST
- CentOS 7.3 minimal 開啟網路服務CentOS
- rhel6.6 minimal安裝ssh不能用
- 第九篇:隨機森林(Random Forest)隨機森林randomREST
- 【Error】centos7 minimal connect: Network is unreachableErrorCentOS
- Segmentation of retinal OCT images using a random forest classifierSegmentationrandomREST
- 【Lintcode】1623. Minimal Distance In The Array
- VMware下centos6.3minimal搭建網路環境CentOS
- VMware安裝CentOS-6 minimal的網路配置CentOS
- 白話異常檢測演算法Isolation Forest演算法REST
- ML《決策樹(四)Bagging 和 Random Forest》randomREST
- Paper Reading: Cost-sensitive deep forest for price predictionREST
- 簡單聊下.NET6 Minimal API的使用方式API
- Codeforces 797C Minimal string:貪心+模擬
- Causal Inference理論學習篇-Tree Based-Causal ForestREST
- HEXO - Minimal 一款為開源作者而生的主題Hexo
- 林軒田機器學習技法課程學習筆記10 — Random Forest機器學習筆記randomREST
- Sequential Minimal Optimization: A Fast Algorithm for Training Support Vector Machines 論文研讀ASTGoAIMac
- 根據MediatR的Contract Messages自動生成Minimal WebApi介面WebAPI
- Causal Inference理論學習篇-Tree Based-From Uplift Tree to Uplift ForestREST
- Forest v1.5.13 釋出,宣告式 HTTP 框架,已超 1.7k starRESTHTTP框架
- 機器學習演算法系列(十八)-隨機森林演算法(Random Forest Algorithm)機器學習演算法隨機森林randomRESTGo
- 最常用的決策樹演算法!Random Forest、Adaboost、GBDT 演算法演算法randomREST
- Forest v1.5.12 釋出,宣告式 HTTP 框架,已超過 1.6k starRESTHTTP框架
- python--- 之The program 'python' can be found in the following packages: * python-minimal * python3PythonPackage
- 今日面試題分享:請問(決策樹、Random Forest、Booting、Adaboot)GBDT和XGBoost的區別是什麼?...面試題randomRESTboot
- 簡要介紹Active Learning(主動學習)思想框架,以及從IF(isolation forest)衍生出來的演算法:FBIF(Feedback-Guided Anomaly Discovery)框架REST演算法GUIIDE
- 【.NET 6】使用.NET 6開發minimal api以及依賴注入的實現、VS2022熱過載和自動反編譯功能的演示API依賴注入編譯