跳到正文

个人记录

来做梦 1005 总结

_recollector_约 11 分钟
原文

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

文章信息与版本

文章信息

发布
2025年10月5日
收录
2026年4月18日
更新
2026年11月9日
篇幅
约 11 分钟

版本

  1. v2当前
  2. v1

A

考虑维护以 uu 为终点的路径 fuf_u 和经过 uu 的路径 gug_u。

不难得到 fu=max⁡(fv−w)+au,gu=secmax⁡(fv−w)+max⁡(fv−w)+auf_u=\max(f_v-w)+a_u,g_u=\operatorname{secmax}(f_v-w)+\max(f_v-w)+a_u。

答案就是 max⁡(gu)\max(g_u)。

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
namespace florr{
	#ifdef __linux__
	#define gc getchar_unlocked
	#define pc putchar_unlocked
	#else
	#define gc _getchar_nolock
	#define pc _putchar_nolock
	#endif
    #define meow ios_base::sync_with_stdio(false);cin.tie(0)
	#define rep(i,l,r,k) for(int i=l;i<=r;i+=k)
	#define rrep(i,r,l,k) for(int i=r;i>=l;i-=k)
	#define loop(i,st,ed,nxt) for(int i=st;i!=ed;i=nxt)
	#define file(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout);
	const int inf=0x3f3f3f3f;
	const long long infll=0x3f3f3f3f3f3f3f3fll;
	const __int128 infi128=(__int128(1)<<126);
	mt19937 __rnd(time(0));
	template<typename T=int>T rd()
	{
		T x=0;bool w=0;
		char ch=gc();
		for(;!isdigit(ch);ch=gc())w|=(ch=='-');
		for(;isdigit(ch);ch=gc())x=(x<<3)+(x<<1)+(ch^48);
		return w?-x:x;
	}
	template<typename T=int>void wt(T x)
	{
		if(x<0)pc('-'),x=-x;
		if(x>9)wt(x/10);
		pc(x%10+48);
	}
	__int128 abs(__int128 x){return x<0?-x:x;}
	int rnd(int l,int r){return abs((int)(__rnd()))%(r-l+1)+l;}
	string rdstr(string charSet)
	{
		bool mp[256];
		memset(mp,0,sizeof mp);
		for(auto chars:charSet)mp[chars]=1;
		string s;
		char ch=gc();
		for(;ch!=EOF&&!mp[ch];ch=gc());
		for(;mp[ch];ch=gc())s+=ch;
		return s;
	}
	void wtstr(string s,int l,int r){rep(i,l,r,1)pc(s[i]);}
	template<typename T,int N>struct Fenwick
	{
		T tr[N+5];
		void add(int x,int v){rep(i,x,N,i&-i)tr[i]+=v;}
		int qry(int x){return x<=0?0:tr[x]+qry(x-(x&-x));}
	};
	template<typename T,int MAX>struct matrix
	{
		T a[MAX][MAX];
		int n,m,mod;
		matrix(int _n=0,int _m=0){n=_n,m=_m,memset(a,0,sizeof a);}
		void clr(){memset(a,0,sizeof a);}
		int* operator[](const int&x){return a[x];}
	};
}//namespace florr
using namespace florr;
const int N=3e5+5;
int a[N];
vector<pair<int,int> >edge[N];
int f[N],g[N],ans;
void dfs(int u,int fa)
{
	int mx=0,se=0;
	for(auto [v,w]:edge[u])
	{
		if(v==fa)continue;
		dfs(v,u);
		if(f[v]-w>=mx)
		{
			se=mx;
			mx=f[v]-w;
		}
		else if(f[v]-w>=se)se=f[v]-w;
	}
	g[u]=mx+se+a[u];
	f[u]=mx+a[u];
	ans=max(ans,g[u]);
}
void solve()
{
	int n=rd();
	rep(i,1,n,1)a[i]=rd();
	rep(i,1,n-1,1)
	{
		int u=rd(),v=rd(),w=rd();
		edge[u].emplace_back(v,w);
		edge[v].emplace_back(u,w);
	}
	dfs(1,0);
	wt(ans);
}
signed main()
{
//	file("");
//	meow;
	signed T=1;
	//cin>>T;
	//T=rd<signed>();
	rep(i,1,T,1)solve();
}
/*
1. freopen 开了吗?
2. long long 开了吗
3. 数组开的够大吗
4. 对拍过了吗
5. 极限数据测了吗
*/

B

先考虑所有 fai=i−1fa_i=i-1 的做法。

设 fif_i 为以 ii 结尾的答案数量,不难发现

{fi=0,si=(fi=fstop−1,si=)\left\{\begin{matrix} f_i=0,s_i=( \\ f_i=f_{s_{top}-1},s_i=) \end{matrix}\right.
C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
namespace florr{
	#ifdef __linux__
	#define gc getchar_unlocked
	#define pc putchar_unlocked
	#else
	#define gc _getchar_nolock
	#define pc _putchar_nolock
	#endif
    #define meow ios_base::sync_with_stdio(false);cin.tie(0)
	#define rep(i,l,r,k) for(int i=l;i<=r;i+=k)
	#define rrep(i,r,l,k) for(int i=r;i>=l;i-=k)
	#define loop(i,st,ed,nxt) for(int i=st;i!=ed;i=nxt)
	#define file(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout);
	const int inf=0x3f3f3f3f;
	const long long infll=0x3f3f3f3f3f3f3f3fll;
	const __int128 infi128=(__int128(1)<<126);
	mt19937 __rnd(time(0));
	template<typename T=int>T rd()
	{
		T x=0;bool w=0;
		char ch=gc();
		for(;!isdigit(ch);ch=gc())w|=(ch=='-');
		for(;isdigit(ch);ch=gc())x=(x<<3)+(x<<1)+(ch^48);
		return w?-x:x;
	}
	template<typename T=int>void wt(T x)
	{
		if(x<0)pc('-'),x=-x;
		if(x>9)wt(x/10);
		pc(x%10+48);
	}
	__int128 abs(__int128 x){return x<0?-x:x;}
	int rnd(int l,int r){return abs((int)(__rnd()))%(r-l+1)+l;}
	string rdstr(string charSet)
	{
		bool mp[256];
		memset(mp,0,sizeof mp);
		for(auto chars:charSet)mp[chars]=1;
		string s;
		char ch=gc();
		for(;ch!=EOF&&!mp[ch];ch=gc());
		for(;mp[ch];ch=gc())s+=ch;
		return s;
	}
	void wtstr(string s,int l,int r){rep(i,l,r,1)pc(s[i]);}
	template<typename T,int N>struct Fenwick
	{
		T tr[N+5];
		void add(int x,int v){rep(i,x,N,i&-i)tr[i]+=v;}
		int qry(int x){return x<=0?0:tr[x]+qry(x-(x&-x));}
	};
	template<typename T,int MAX>struct matrix
	{
		T a[MAX][MAX];
		int n,m,mod;
		matrix(int _n=0,int _m=0){n=_n,m=_m,memset(a,0,sizeof a);}
		void clr(){memset(a,0,sizeof a);}
		int* operator[](const int&x){return a[x];}
	};
}//namespace florr
using namespace florr;
const int N=5e5+5;
int n,fa[N],dp[N];
stack<int>st;
string s;
vector<int>edge[N]; 
void dfs(int u)
{
	int pre=-1;
	if(s[u]=='(')st.emplace(u);
	else if(!st.empty())
	{
		pre=st.top();st.pop();
		dp[u]=dp[fa[pre]]+1;
	}
	for(auto v:edge[u])
		dfs(v);
	if(s[u]=='(')st.pop();
	else if(~pre)st.emplace(pre);
}
void DFS(int u)
{
	for(auto v:edge[u])
	{
		dp[v]+=dp[u];
		DFS(v);
	}
}
void solve()
{
	n=rd();
	s="#"+rdstr("()");
	rep(i,2,n,1)
		edge[fa[i]=rd()].emplace_back(i);
	dfs(1);
	DFS(1);
	int ans=0;
	rep(i,1,n,1)ans=(ans^(i*dp[i]));
	wt(ans);
}
signed main()
{
//	file("");
//	meow;
	signed T=1;
	//cin>>T;
	//T=rd<signed>();
	rep(i,1,T,1)solve();
}
/*
1. freopen 开了吗?
2. long long 开了吗
3. 数组开的够大吗
4. 对拍过了吗
5. 极限数据测了吗
*/

C

设 fi,jf_{i,j} 为 ii 保留 jj 个儿子的最小操作数。0≤j≤20 \le j \le 2。

则有 fu,0=∑v∈son⁡(u)min⁡(fv,0−1,fv,2)+degu+1f_{u,0}=\sum_{v\in \operatorname{son}(u)}\min(f_{v,0}-1,f_{v,2})+deg_u+1,fu,1=∑v∈son(u)⁡fv,0−max⁡v∈son(u)⁡(fv,0−fv,1)f_{u,1}=\sum_{v\in\operatorname{son(u)}}f_{v,0}-\max_{v\in\operatorname{son(u)}}(f_{v,0}-f_{v,1}),fu,2=fu,1−secmax⁡v∈son(u)⁡(fv,0−fv,1)f_{u,2}=f_{u,1}-\operatorname{secmax}_{v\in\operatorname{son(u)}}(f_{v,0}-f_{v,1})。

因为 fu,2≤fu,1f_{u,2} \le f_{u,1},所以我们答案就是 (n−1)−m+∑i=1nisroot⁡(i)min⁡(dpi,0,dpi,2)(n-1)-m+\sum_{i=1}^n\operatorname{isroot}(i)\min(dp_{i,0},dp_{i,2})。

D