11.18 NOIP2024 模拟赛 #24 div1

怎么说

T1

糖丸了

看错题了,他要求最中间有一个,然后我没看见

就这还能有 \(90\)

T2

写的 dfs,然鹅他没下发大样例

下发完发现 T 了

然后改成 bfs 过了

T3

写的暴力,但是极限数据会 T

赛后果然 T 了

T4

一眼换根,但是柿子不是很会

然后码的暴力+性质

但是是在比赛结束后 \(3min\) 才码完

正解就是换根,看一眼 pdf 直接会了

然后第二个 A 掉 T4,还是最优解(其实是我链的部分一直不过,然后多写了个部分分)

感谢雪月花的链(
#include<bits/stdc++.h>
#define int long long
#define ll long long
#define fd(i,a,b) for(int i=(a);i<=(b);i=-~i)
#define bd(i,a,b) for(int i=(a);i>=(b);i=~-i)
#define db(x) cout<<"DEBUG "<<#x<<" = "<<x<<endl;
#define endl '\n'
using namespace std;

//#define SIZE (1<<20)
//char In[SIZE],Out[SIZE],*p1=In,*p2=In,*p3=Out;
//#define getchar() (p1==p2&&(p2=(p1=In)+fread(In,1,SIZE,stdin),p1==p2)?EOF:*p1++)
inline int read()
{
	int x=0,f=1;char c=getchar();
	while(c<'0'||c>'9'){if(c=='-') f=-1;c=getchar();}
	while(c>='0'&&c<='9'){x=x*10+(c-48);c=getchar();}
	return x*f;
}

const int N=1e5+509,M=1e6+509,inf=1e18+114514,mod=998244353;

int n,q,sum[N],in[N];
vector< pair<int,int> > e[N];

namespace SubI
{
	int res[N],root=1;
	vector< pair<int,int> > query[N];
	struct node{int id,dis;}f[2][N];//0 best 1 second
	inline bool operator<(node x,node y)
	{
		if(x.dis==y.dis) return (x.id<y.id);
		return (x.dis<y.dis);
	}
	inline bool operator==(node x,node y)
	{
		return (x.id==y.id);
	}
	
	void dfs(int x,int fa)
	{
		if(e[x].size()<=1) f[0][x]={x,0};
		else f[0][x]={inf,inf};
		f[1][x]={inf,inf};
		for(auto [y,z]:e[x])
		{
			if(y==fa) continue;
			dfs(y,x);
			node yy=f[0][y];
			yy.dis+=z;
			if(yy<f[0][x]) f[1][x]=f[0][x],f[0][x]=yy;
			else if(yy<f[1][x]) f[1][x]=yy;
		}
		sum[f[0][x].id]++;
		if(f[1][x].id==inf) f[1][x]=f[0][x];
	}
	
	//#define DEB
	void DP(int x,int fa)
	{
		for(auto [j,id]:query[x])
			res[id]=sum[j];
	#ifdef DEB
		cerr<<"x="<<x<<endl;
		cerr<<"x--best "<<x<<':'<<f[0][x].id<<' '<<f[0][x].dis<<endl;
		cerr<<"x--secn "<<x<<':'<<f[1][x].id<<' '<<f[1][x].dis<<endl;
	#endif
		for(auto [y,z]:e[x])
		{
			if(y==fa) continue;
	#ifdef DEB
			cerr<<"y="<<y<<endl;
			cerr<<"y--best "<<y<<':'<<f[0][y].id<<' '<<f[0][y].dis<<endl;
			cerr<<"y--secn "<<y<<':'<<f[1][y].id<<' '<<f[1][y].dis<<endl;
	#endif
			if(f[0][y]==f[0][x])
			{
	#ifdef DEB
				cerr<<"best=best"<<endl;
	#endif
				sum[f[0][x].id]--;
				sum[f[1][x].id]++;
				DP(y,x);
				sum[f[0][x].id]++;
				sum[f[1][x].id]--;
			}
			else
			{
				node X=f[0][x];
				X.dis+=z;
				if(X<f[0][y])
				{
	#ifdef DEB
					cerr<<"second<best"<<endl;
	#endif
					sum[f[0][y].id]--;
					sum[f[0][x].id]++;
					node p=f[1][y];
					f[1][y]=f[0][y];
					f[0][y]=X;
					DP(y,x);
					f[0][y]=f[1][y];
					f[1][y]=p;
					sum[f[0][y].id]++;
					sum[f[0][x].id]--;
				}
				else
				{
	#ifdef DEB
					cerr<<"second>=best"<<endl;
	#endif
					DP(y,x);
				}
			}
		}
	}
	
	void Main()
	{
		q=read();
		fd(i,1,q)
		{
			int s=read(),t=read();
			query[t].push_back({s,i});
		}
		dfs(root,0);
		DP(root,0);
		fd(i,1,q) printf("%lld\n",res[i]);
	}
}

namespace SubII
{
	int dfn[N],dis[N],tot,l,r;
	void Dfs(int x,int fa)
	{
		dfn[x]=++tot;
		for(auto [y,z]:e[x])
		{
			if(y==fa) continue;
			dis[y]=dis[x]+z;
			Dfs(y,x);
		}
	}
	void Main()
	{
		int l=-1,r=-1;
		fd(i,1,n)
		{
			if(in[i]==1)
			{
				if(l==-1) l=i;
				else r=i;
			}
		}
		Dfs(l,0);
		q=read();
		while(q--)
		{
			int s=read(),t=read();
			int res=0;
			if(s==l)
			{
				res=dfn[t]-1;
				if((dis[t]-dis[l]<dis[r]-dis[t])||(dis[t]-dis[l]==dis[r]-dis[t]&&r>l))
					++res;
				printf("%lld\n",res);
			}
			else
			{
				res=dfn[r]-dfn[t];
				if((dis[t]-dis[l]>dis[r]-dis[t])||(dis[t]-dis[l]==dis[r]-dis[t]&&r<l))
					++res;
				printf("%lld\n",res);
			}
		}
	}
}

signed main()
{
#define FJ
#ifdef FJ
	freopen("race.in","r",stdin);
	freopen("race.out","w",stdout);
#else
	freopen("race1.in","r",stdin);
	freopen("race.out","w",stdout);
#endif
//#define io
#ifdef io
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
#endif
	
	int cnt1=0;
	
	n=read();
	fd(i,1,n-1)
	{
		int x=read(),y=read(),z=read();
		e[x].push_back({y,z});
		e[y].push_back({x,z});
		++in[x],++in[y];
	}
	fd(i,1,n) if(in[i]==1) ++cnt1;
	if(cnt1!=2) SubI::Main();
	else SubII::Main();
	
	return 0;
}

总结

  • T1 看错题,又双叒叕挂分
  • T4 差一点码完
posted @   whrwlx  阅读(5)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· 【.NET】调用本地 Deepseek 模型
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
点击右上角即可分享
微信分享提示