跳到正文

个人记录

心连心std

NumberTrart约 20 分钟
原文
文章信息与版本

文章信息

发布
2026年2月28日
收录
2026年4月2日
更新
2026年11月13日
篇幅
约 20 分钟

版本

  1. v2当前
  2. v1
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.
	//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;
}
原文