A
考虑维护以 为终点的路径 和经过 的路径 。
不难得到 。
答案就是 。
#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
先考虑所有 的做法。
设 为以 结尾的答案数量,不难发现
#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
设 为 保留 个儿子的最小操作数。。
则有 ,,。
因为 ,所以我们答案就是 。