From d4b17f35cc3c2d30835478e8236ece603bdf1ff9 Mon Sep 17 00:00:00 2001 From: Zengtudor Date: Tue, 4 Nov 2025 11:10:05 +0800 Subject: [PATCH] =?UTF-8?q?feat(P5022):=20=E6=B7=BB=E5=8A=A0=E6=A0=91?= =?UTF-8?q?=E9=81=8D=E5=8E=86=E7=AE=97=E6=B3=95=E4=BB=A5=E5=A4=84=E7=90=86?= =?UTF-8?q?=E5=B8=A6=E5=88=A0=E9=99=A4=E8=BE=B9=E7=9A=84=E7=89=B9=E6=AE=8A?= =?UTF-8?q?=E6=83=85=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 实现新的DFS算法来处理当m≠n-1时需要尝试删除每条边的情况。添加了新的数据结构来存储边信息和比较不同删除方案的结果。当检测到环时,会尝试删除每条边并比较所有可能的遍历顺序,选择字典序最小的方案。 --- src/11/3/P5022.cpp | 55 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 46 insertions(+), 9 deletions(-) diff --git a/src/11/3/P5022.cpp b/src/11/3/P5022.cpp index 031f96d..9722ae3 100644 --- a/src/11/3/P5022.cpp +++ b/src/11/3/P5022.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -5,26 +6,40 @@ #include #include #include +#include #include using ll = int64_t; const ll maxn = 5000+7; -ll n,m; -std::vector,std::greater>> e; +ll n,m,edge[maxn][2],del=-1,sum=0; +std::vector,std::greater>> e,ne; std::bitset b; - +std::vectornans,ans; +#define printf static inline void dfs(ll f,ll now){ - // printf("dfs f=%lld, now=%lld\n",f,now); - std::cout<>n>>m; e.resize(n+1); + nans.resize(n+1); + ans.resize(n+1); + b[1]=true; for(ll i=1;i<=m;i++){ ll u,v; std::cin>>u>>v; + edge[i][0]=u,edge[i][1]=v; e[u].push(v); e[v].push(u); } - dfs(0,1); + for(ll i=1;i<=m;i++)ans[i]=1e9+7; + ne=e; + if(m==n-1){ + dfs(0,1); + }else{ + for(ll i=1;i<=m;i++){ + printf("BEGIN TREE i=%lld\n",i); + e=ne; + b.reset(); + b[1]=true; + del=i; + sum=0; + dfs(0, 1); + } + for(int i=1;i<=n;i++){ + std::cout<