跳到正文

题解

心连心题解byTwelveHexar

NumberTrart约 24 分钟
原文

这是 2026年3月29日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年3月14日
收录
2026年3月29日
更新
2026年11月13日
篇幅
约 24 分钟
获赞
3
题目
P15976

版本

  1. v2当前
  2. v1

Part 00 读题

Part 01 十分显然的前置结论

  1. 图是基环树。
  2. 边权几乎不起什么作用。
    更准确的说,除了最后计算结果要用到总边权和以外,不会用到边权。
  3. 操作的先后顺序没有任何影响。
    更进一步,一直重复降低边权和速度最快的操作总是最优的。
    更更近一步,如果没有办法降低边权和,那么总边权和最小值一定是初始的总边权和;否则,最少的操作次数是边权总和除以最快的降低边权和速度(下取整)+1

(好了可以全部输出 yes 骗分了)

Part 02 朴素到不能再朴素的算法

暴力枚举操作哪条边,并暴力计算指令传递,Θ(n2)\Theta(n^2)。

2020 分到手。

代码不放了,这要是不会写可以退役了。

Part 03 第一个核心优化

人帅心善的出题人贴心赠送了 subtask 2~5。

我们来浅浅的端详一下 subtask 2 的限制。

l≤6l\le 6,其中 ll 是基环树环长。

何意味?

环长极小的情况下,几乎所有边都不在基环树的“基环”上而在“树”上。

为了方便,我们定义“子树”:将基环树的“基环”的边去掉,全部节点和剩下的边构成一个森林。可以证明这个森林里每棵树都有且仅有 11 个环上节点。令每棵树的根都是其唯一的环上的节点。

鉴于图的大部分在子树上,而子树极其少,我们考虑把子树整体来看。

我们发现,当操作边不在一棵子树上的时候,一棵子树受到的影响无碍乎 22 种情况:

  1. 从环上的边收到了指令 11。
  2. 从环上的边收到了指令 −1-1。

可以看出两种情况的贡献是互为相反数的,而使用树上 dp 可以很轻松求出这两个值。

而当操作边在一棵子树上时,虽然情况有很多,但是能粗略地分为 22 种:

  1. 向环上的边发送指令 11。
  2. 向环上的边发送指令 −1-1。

而且每种情况可以贪心的取最优值,而计算就要用到换根 dp 了。(大胆的预言一下这里会有很多人写出 bug)

经过了这么一番折腾,我们成功且高度地“封装”了每棵子树,使得我们无需再去考虑每个子树内部的传递。

或者说,基环树只剩基环了。

但是操作相应的要分为 33 种:

  1. 操作环上的边。
  2. 操作子树内的边,使得子树向环上的边发送指令 11。
  3. 同上,但发送 −1-1。

暴力传递从 Θ(n2)\Theta(n^2) 优化到了 Θ(l2)\Theta(l^2),而 dp 预处理是 Θ(n)\Theta(n) 的。

又有 1515 分到手。

Part 04 第二个核心优化的铺垫

浅浅的端详一下 subtask 4。

n−l≤5n-l\le5

也就是说,这回基环树的大头在基环上而不是子树上了。

但由于子树最多只有 55 个,而环上的所有边的贡献只有 33 类情况,对应上文的三种操作,我们甚至可以把子树看做相对于环转动。

顺带一提,几乎每次子树旋转都会使子树贡献取相反数。

由此我们便得出了 Θ((n−l)n)\Theta((n-l)n) 的做法。

又到手了…… 55 分?!

看来这个做法还得改改。

Part 05 第二个核心优化

摆在我们面前的有 33 条路:

  1. 保留环贡献不变、子树绕环旋转的思路。
  2. 注意到几乎每次子树绕环旋转贡献都会取相反数,以此作为突破口继续思考。
  3. 之前的做法能拿到 55 分是因为每次旋转只统计 55 个变化,考虑把环的旋转也概括为有限的变化。

但其实只有一条路,因为第一条路是死路,而另两条路可以一起走。

考虑一个挂满了子树的基环,对一条环上边进行操作,然后将操作边右移一步。

如果我们认为是只有环上的指令数转了,边和子树都没有转,我们惊讶地发现……

所有边和子树的贡献都取相反数了!……不对,怎么有几个不和谐的地方?!

是的,操作边的附近和对面会有一些贡献不取相反数的位置。并且随着环上的指令的旋转,这些位置会一起转,而修正系数是由环的指令决定的。

另两种情况也类似。

这样,环上的指令旋转的时候,我们可以先大刀阔斧地给所有贡献取相反数,再修正特定位置贡献。

也就是说,我们封装了基环树的基环。

它啥都不剩了。

Part 06 汇总

把基环树拆成基环和子树,Θ(n)\Theta(n)。

三个子树 dp,Θ(n)\Theta(n)。

暴力计算 3 种情况的初始贡献,Θ(n)\Theta(n)。

把环的指令转 3 圈,Θ(n)\Theta(n)。

总计,Θ(n)\Theta(n)。

注意不要见祖宗哦

Part 07 代码

以防我自己看不懂,很详细的解释清楚了每个参数。

C++
#include<iostream>
#include<vector>
#include<queue>
//#define debugdp0
//#define debugdp1
//#define debugres
//#define debug2
//#define debug3
//#define debugloop 
//#define debugRE
using namespace std;
const int N=100005;
int n,loophead,looplen;
long long sumw,ans=200000ll/*2e5*/;
vector<int> g[N];
int f[N];//If u on loop,f[u] marks the next node.If not,f[u] marks the father node.
int in[N];//TEMP
bool onloop[N];
long long dp0[N]/*impact when outedge which sends in signal is 1*/,dp1[N][2];//relative increasement minimized when 0:send -1 out,1:send 1 out
void detectloop()//O(n)
{
	queue<int> q;
	for(int i=1;i<=n;i++) if(in[i]==1) q.push(i);
	while(q.size())
	{
		int u=q.front();
		q.pop();
		for(int i=0;i<g[u].size();i++)
			if(in[g[u][i]])
			{
				in[g[u][i]]--;
				in[u]--;
				if(in[g[u][i]]==1) q.push(g[u][i]);
			}
	}
	for(int i=1;i<=n;i++)
		if(in[i]==2)
		{
			loophead=i;
			looplen++;
			onloop[i]=true;
#ifdef debugloop
			cout<<i<<" onloop\n";
#endif
			for(int j=0;j<g[i].size();j++)
				if(in[g[i][j]])
				{
					f[i]=g[i][j];
					int u=g[i][j];
					while(u!=i)
					{
						onloop[u]=true;
#ifdef debugloop
						cout<<u<<" onloop\n";
#endif
						looplen++;
						for(int k=0;k<g[u].size();k++)
							if(in[g[u][k]]&&f[g[u][k]]!=u)
							{
								f[u]=g[u][k];
								u=g[u][k];
								break;
							}
					}
					break;
				}
			break;
		}
}
void dfs1(int,int,int,int);
void dfs0(int u)
{
#ifdef debugRE
	cout<<"dfs0"<<u<<endl;
#endif
	for(int i=0;i<g[u].size();i++)
		if(g[u][i]!=f[u]&&!onloop[g[u][i]])
		{
			f[g[u][i]]=u; 
			dfs0(g[u][i]);
			dp0[u]-=dp0[g[u][i]];
			dp0[u]-=1;//send -1
		}
#ifdef debugdp0
	cout<<"dp0["<<u<<"]="<<dp0[u]<<'\n';
#endif
	if(onloop[u])
	{
		dp1[u][0]=dp1[u][1]=200000ll;//2e5
		dfs1(u,u,1,0);
	}
}
void dfs1(int u,int rt,int mfout,int mfimpact/*considering the father edge*/)
{
	if(u!=rt)
	{
#ifdef debugdp1
		cout<<"dp1["<<rt<<"]["<<mfout<<"] records "<<dp0[u]<<"+"<<mfimpact<<"="<<dp0[u]+mfimpact<<'\n';
#endif
		dp1[rt][mfout]=min(dp1[rt][mfout],dp0[u]+mfimpact);
	}
	mfimpact=-mfimpact;
	mfout=1-mfout;
	for(int i=0;i<g[u].size();i++)
		if(g[u][i]!=f[u]&&!onloop[g[u][i]])
		{
			mfimpact-=dp0[g[u][i]];
			mfimpact-=1;//send -1
		}
	for(int i=0;i<g[u].size();i++)
		if(g[u][i]!=f[u]&&!onloop[g[u][i]])
		{
			//send -1 --> send 1
			//dp0* -1 --> dp0* 0
			//edge -1 --> edge 1
			dfs1(g[u][i],rt,mfout,mfimpact+2+dp0[g[u][i]]);
		}
}
int main()//O(n) in total
{
	ios::sync_with_stdio(false);
	cin>>n;
	for(int i=1,u,v,w;i<=n;i++)
	{
		cin>>u>>v>>w;
		g[u].push_back(v);
		g[v].push_back(u);
		in[u]++;
		in[v]++;
		sumw+=w;
	}
	detectloop();
	for(int i=1;i<=n;i++)
		if(onloop[i]) dfs0(i);
	long long res,mark/*edge mark*/,u,opu,opm;
	
	//caculation of all node_out1 type
	//start with loophead
	res=0;
	res+=dp1[loophead][1];
	mark=1,u=loophead;
	for(int i=1;i<=looplen/2;i++)
	{
		res+=mark;//edge
		u=f[u];
		res+=mark*dp0[u];//node
		mark=-mark;
	}
	if(looplen%2==1)//opposite an edge
	{
		//in this view,'+' means mark and '-' means -mark.
		//     u>>>>>
		// >-> - >0< - <-< + will be turn into
		// >+> + >-> - >0< -
		//this edge bears no contribution.
		opu=f[u],opm=-2*mark;//marking this node is supposed to be caculated with more -2*mark times of contribution\
		upon recalculating
		//the edges bears more -2*mark=opm contribution upon recalculating.
		u=f[u],mark=-mark;//pass the edge
	}
	else
	{
		//      u
		//+ >-> 0 <-< + <+< - will be turn into
		//- >+> + >-> 0 <-< +
		res+=2*mark*dp0[u];//to compensate the contribution of the opposite node
		opu=u,opm=mark;//marking this node is supposed to be caculated with more mark times of contribution upon\
		recalculating,and SO DOES THE NEXT ONE!!!!!!!!
		//the edge bears more -2*mark=-2opm contribution upon recalculating.
		mark=-mark;//passing the node
	}
	for(int i=1;i<=looplen/2;i++)
	{
		res+=mark*dp0[u];//node
		res+=mark;//edge
		u=f[u];
		mark=-mark;
	}
	ans=min(ans,res);
#ifdef debugres
	cout<<"precalc node_out1 with beginnode="<<loophead<<":"<<res<<'\n';
#endif
	//OPM SHOULDN'T BE CHANGED DURING RECALCULATING!!!!!!!
	//start recalculating
	for(u=loophead;f[u]!=loophead;)
	{
		//in this view,+ means k=1 and - means k=-1
		//      u>>>>>
		//+ <+< # >+> + >-> - will be turn into
		//- <-< + <+< # >+> +
		res-=dp1[u][1];
		res-=dp0[u];
		res-=2;
		res=-res;//reverse almost every contribution
		u=f[u];
		res+=dp0[u];
		res+=dp1[u][1];
		if(looplen%2==1)//recalculating operations mentioned above
		{
			res+=opm;
			res+=dp0[opu]*opm;
			opu=f[opu];
		}
		else
		{
			res+=-2*opm;
			res+=dp0[opu]*opm;
			opu=f[opu];
			res+=dp0[opu]*opm;
		}
		ans=min(ans,res);
#ifdef debugres
		cout<<"@Recalc node_out1 with beginnode="<<u<<":"<<res<<'\n';
#endif
	}
#ifdef debugres
	cout<<'\n';
#endif
	
	
	
	
	//caculation of all node_out-1 type
	//start with loophead
	res=0;
	res+=dp1[loophead][0];
#ifdef debug2
	cout<<"$node "<<loophead<<" self-contributes "<<dp1[loophead][0]<<'\n';
#endif
	mark=-1,u=loophead;
	for(int i=1;i<=looplen/2;i++)
	{
		res+=mark;//edge
#ifdef debug2
		cout<<"$edge from "<<u<<" to "<<f[u]<<" contributes "<<mark<<'\n';
#endif
		u=f[u];
		res+=mark*dp0[u];//node
#ifdef debug2
		cout<<"$node "<<u<<" contributes "<<mark<<"*"<<dp0[u]<<'\n';
#endif
		mark=-mark;
	}
	if(looplen%2==1)//opposite an edge
	{
		//     u>>>>>
		// >-> - >0< - <-< + will be turn into
		// >+> + >-> - >0< -
		//this edge bears no contribution.
		opu=f[u],opm=-2*mark;//marking this node is supposed to be caculated with more -2*mark times of contribution\
		upon recalculating
		//the edges bears more -2*mark=opm contribution upon recalculating.
		u=f[u],mark=-mark;//pass the edge
	}
	else
	{
		//      u
		//+ >-> 0 <-< + <+< - will be turn into
		//- >+> + >-> 0 <-< +
		res+=2*mark*dp0[u];//to compensate the contribution of the opposite node
		opu=u,opm=mark;//marking this node is supposed to be caculated with more mark times of contribution upon\
		recalculating,and SO DOES THE NEXT ONE!!!!!!!!
		//the edge bears more -2*mark=-2opm contribution upon recalculating.
		mark=-mark;//passing the node
	}
	for(int i=1;i<=looplen/2;i++)
	{
		res+=mark*dp0[u];//node
#ifdef debug2
		cout<<"$node "<<u<<" contributes "<<mark<<"*"<<dp0[u]<<'\n';
#endif
		res+=mark;//edge
#ifdef debug2
		cout<<"$edge from "<<u<<" to "<<f[u]<<" contributes "<<mark<<'\n';
#endif
		u=f[u];
		mark=-mark;
	}
	ans=min(ans,res);
#ifdef debugres
	cout<<"precalc node_out-1 with beginnode="<<loophead<<":"<<res<<'\n';
#endif
	//OPM SHOULDN'T BE CHANGED DURING RECALCULATING!!!!!!!
	//start recalculating
	for(u=loophead;f[u]!=loophead;)
	{
		//      u>>>>>
		//- <-< # >-> - >+> + will be turn into
		//+ <+< - <-< # >-> -
		res-=dp1[u][0];
		res+=dp0[u];
		res+=2;
		res=-res;//reverse almost every contribution
		u=f[u];
		res-=dp0[u];
		res+=dp1[u][0];
		if(looplen%2==1)//recalculating operations mentioned above
		{
			res+=opm;
			res+=dp0[opu]*opm;
			opu=f[opu];
		}
		else
		{
			res+=-2*opm;
			res+=dp0[opu]*opm;
			opu=f[opu];
			res+=dp0[opu]*opm;
		}
		ans=min(ans,res);
#ifdef debugres
		cout<<"@Recalc node_out-1 with beginnode="<<u<<":"<<res<<'\n';
#endif
	}
#ifdef debugres
	cout<<'\n';
#endif
	
	
	
	//caculation of all loopedge type
	//start with loophead---f[loophead]
	res=1;
	mark=-1,u=f[loophead];
	res+=dp0[loophead]+dp0[f[loophead]];
#ifdef debug3
	cout<<"$edge from "<<loophead<<" to "<<f[loophead]<<" contributes 1\n";
	cout<<"$node "<<loophead<<" contributes 1*"<<dp0[loophead]<<'\n';
	cout<<"$node "<<f[loophead]<<" contributes 1*"<<f[dp0[loophead]]<<'\n';
#endif
	for(int i=1;i<=(looplen-1)/2;i++)
	{
		res+=mark;//edge
#ifdef debug3
		cout<<"$edge from "<<u<<" to "<<f[u]<<" contributes "<<mark<<'\n';
#endif
		u=f[u];
		res+=mark*dp0[u];//node
#ifdef debug3
		cout<<"$node "<<u<<" contributes "<<mark<<"*"<<dp0[u]<<'\n';
#endif
		mark=-mark;
	}
	if(looplen%2==0)//opposite an edge
	{
		//     u>>>>>
		// >-> - >0< - <-< + will be turn into
		// >+> + >-> - >0< -
		//this edge bears no contribution.
		opu=f[u],opm=-2*mark;//marking this node is supposed to be caculated with more -2*mark times of contribution\
		upon recalculating
#ifdef debug3
		cout<<"$opu="<<opu<<" opm="<<opm<<'\n';
#endif
		//the edges bears more -2*mark=opm contribution upon recalculating.
		u=f[u],mark=-mark;//pass the edge
	}
	else
	{
		//      u
		//+ >-> 0 <-< + <+< - will be turn into
		//- >+> + >-> 0 <-< +
		res+=2*mark*dp0[u];//to compensate the contribution of the opposite node
		opu=u,opm=mark;//marking this node is supposed to be caculated with more mark times of contribution upon\
		recalculating,and SO DOES THE NEXT ONE!!!!!!!!
#ifdef debug3
		cout<<"$opu="<<opu<<" opm="<<opm<<" (double operation)\n";
#endif
		//the edge bears more -2*mark=-2opm contribution upon recalculating.
		mark=-mark;//passing the node
	}
	for(int i=1;i<=(looplen-1)/2;i++)
	{
		res+=mark*dp0[u];//node
#ifdef debug3
		cout<<"$node "<<u<<" contributes "<<mark<<"*"<<dp0[u]<<'\n';
#endif
		res+=mark;//edge
#ifdef debug3
		cout<<"$edge from "<<u<<" to "<<f[u]<<" contributes "<<mark<<'\n';
#endif
		u=f[u];
		mark=-mark;
	}
	ans=min(ans,res);
#ifdef debugres
	cout<<"$precalc edge with beginedge="<<loophead<<"->"<<f[loophead]<<":"<<res<<'\n';
#endif
	//OPM SHOULDN'T BE CHANGED DURING RECALCULATING!!!!!!!
	//start recalculating
	for(u=loophead;f[u]!=loophead;)
	{
		//      u>>>>>
		//- <-< + <#> + >-> - >+> + will be turn into
		//+ <+< - <-< + <#> + >-> -
		res-=2*dp0[f[u]];
		res=-res;//reverse almost every contribution
		u=f[u];
		if(looplen%2==0)//recalculating operations mentioned above
		{
			res+=opm;
			res+=dp0[opu]*opm;
			opu=f[opu];
		}
		else
		{
			res+=-2*opm;
			res+=dp0[opu]*opm;
			opu=f[opu];
			res+=dp0[opu]*opm;
		}
		ans=min(ans,res);
#ifdef debugres
		cout<<"@Recalc edge with beginedge="<<u<<"->"<<f[u]<<":"<<res<<'\n';
#endif
	}
	//IT TAKES HEXAR V. 2.5 HOURS TO FINISH THIS STD
	//CURRENTLY 6 ERRORS ARE FIXED ALREADY:mark error,latter-half precalculate error,side-beginning-edge recalculate-contribution error,subtree f[] missing,debugment remained,opposite node contribution error.
	//I JUST CAN'T IMAGINE HOW REDSTONE OTHERS WILL BE WHEN SOLVING THIS
	if(ans<0) cout<<"No "<<(sumw+1-ans-1)/(-ans);
	else cout<<"Yes "<<sumw;
	return 0;
}

心之所向,一马平川。