U727676 九尾查克拉树 题解
liujiayi666 · · 题解
正如题目背景所说的此题来历非常的神秘,创建这题的缘故是我考场上(看着错误题意)想到一个神秘做法,对于错误题意(就是本题)来讲是正解,并且以前还没有用过这种技巧,于是就有了这题和这篇tj
由于考试时候思路九曲十八弯,这里太小写不下,直接讲正解
以
然后我们注意到矩阵没有交换律非常的慌张,但是仔细分析我们发现对于同一条长链的
于是我们先预处理如果每个点是恰好和
(可能这里的推平有歧义说下放更合适但是推平比较形象就不改了)
时间复杂度 应该算比较小就当成常数算了)
#include<bits/stdc++.h>
using namespace std;
namespace Abb{
#define fi first
#define se second
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define per(i, a, b) for (int i = (a); i >= (b); --i)
#define wl( x ) while ( x )
#define pb push_back
#define pf push_front
#define eb emplace_back
#define il inline
typedef pair<int,int> pii;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
typedef __int128 LL;
typedef unsigned __int128 uLL;
typedef pair<int,ll> pil;
typedef pair<ll,int> pli;
typedef pair<ll,ll> pll;
typedef short si;
template<typename T>
inline void chkmi(T& x,T y){x=x<y?x:y;}
template<typename T>
inline void chkmx(T& x,T y){x=x<y?y:x;}
}
using namespace Abb;
namespace FastIO{
#define gc getchar
#define pc putchar
//#define getchar getchar_unlocked
//#define putchar putchar_unlocked
template<typename T>
inline void rd(T& x){
int f=1;x=0;char ch=gc();
wl(ch<'0'||ch>'9'){
if(ch=='-')f=-1;
ch=gc();
}
wl(ch>='0'&&ch<='9'){
x=x*10+(ch^48);
ch=gc();
}
x*=f;
}
template<typename T>
inline void wt(T x){
if(x<0)pc('-'),x=-x;
if(x>9)wt(x/10);
pc(x%10^48);
}
}
using namespace FastIO;
const int N=1e5+5;
const ll inf=1e16;
int n,q,k,a[N],b[N];
vector<int>e[N];
struct Matrix{
ll mat[3][3];
il void init(){
mat[0][0]=0;mat[0][1]=-inf;mat[0][2]=-inf;
mat[1][0]=-inf;mat[1][1]=0;mat[1][2]=-inf;
mat[2][0]=-inf;mat[2][1]=-inf;mat[2][2]=0;
}
il Matrix operator*(const Matrix& tw)const{
return
{
{
{
max({mat[0][0]+tw.mat[0][0],mat[0][1]+tw.mat[1][0],mat[0][2]+tw.mat[2][0]})
,
max({mat[0][0]+tw.mat[0][1],mat[0][1]+tw.mat[1][1],mat[0][2]+tw.mat[2][1]})
,
max({mat[0][0]+tw.mat[0][2],mat[0][1]+tw.mat[1][2],mat[0][2]+tw.mat[2][2]})
}
,
{
max({mat[1][0]+tw.mat[0][0],mat[1][1]+tw.mat[1][0],mat[1][2]+tw.mat[2][0]})
,
max({mat[1][0]+tw.mat[0][1],mat[1][1]+tw.mat[1][1],mat[1][2]+tw.mat[2][1]})
,
max({mat[1][0]+tw.mat[0][2],mat[1][1]+tw.mat[1][2],mat[1][2]+tw.mat[2][2]})
}
,
{
max({mat[2][0]+tw.mat[0][0],mat[2][1]+tw.mat[1][0],mat[2][2]+tw.mat[2][0]})
,
max({mat[2][0]+tw.mat[0][1],mat[2][1]+tw.mat[1][1],mat[2][2]+tw.mat[2][1]})
,
max({mat[2][0]+tw.mat[0][2],mat[2][1]+tw.mat[1][2],mat[2][2]+tw.mat[2][2]})
}
}
}
;
}
};
struct Vector{
ll vec[3];
il Vector operator*(const Matrix& tw)const{
return
{
{
max({vec[0]+tw.mat[0][0],vec[1]+tw.mat[1][0],vec[2]+tw.mat[2][0]})
,
max({vec[0]+tw.mat[0][1],vec[1]+tw.mat[1][1],vec[2]+tw.mat[2][1]})
,
max({vec[0]+tw.mat[0][2],vec[1]+tw.mat[1][2],vec[2]+tw.mat[2][2]})
}
}
;
}
};
int fa[N],dep[N],mxd[N],lson[N];
ll sum[N],dp[N][3];
void dfs1(int u){
mxd[u]=dep[u];
ll mi=inf;
for(int v:e[u]){
if(v==fa[u])continue;
fa[v]=u;dep[v]=dep[u]+1;
dfs1(v);
chkmi(mi,(ll)a[v]);
sum[u]+=sum[v];
if(mxd[v]>mxd[u]){
mxd[u]=mxd[v];
lson[u]=v;
}
}
dp[u][0]=sum[u];
dp[u][1]=sum[u]+b[u];
dp[u][2]=sum[u]+b[u]-mi;
sum[u]+=a[u];
chkmx(dp[u][2],sum[u]);
}
Vector *f[N];Matrix *g[N];
il Matrix GetMat(Vector& tw){
return
{
{
{
max({tw.vec[0],tw.vec[1],tw.vec[2]})
,
-inf
,
-inf
}
,
{
-inf
,
max(tw.vec[0],tw.vec[2])
,
tw.vec[0]
}
,
{
-inf
,
-inf
,
max(tw.vec[0],tw.vec[2])
}
}
}
;
}
void dfs2(int u){
if(lson[u]){
f[lson[u]]=f[u]+1;
g[lson[u]]=g[u]+1;
dfs2(lson[u]);
}
f[u][0]={{dp[u][0],dp[u][1],dp[u][2]}};
g[u][0].init();
Matrix t[3];
int Len=mxd[u]-dep[u]+1;
if(Len>1){
t[0].mat[0][0]=0;t[0].mat[0][1]=b[u];t[0].mat[0][2]=b[u];
t[0].mat[1][0]=0;t[0].mat[1][1]=-inf;t[0].mat[1][2]=-inf;
t[0].mat[2][0]=0;t[0].mat[2][1]=b[u];t[0].mat[2][2]=a[u];
g[u][1]=g[u][1]*t[0];
}
for(int v:e[u]){
if(v==fa[u]||v==lson[u])continue;
int len=mxd[v]-dep[v]+1;
f[v]=new Vector[len];
g[v]=new Matrix[len];
dfs2(v);
t[0].init();//t[0]推u
t[1].init();//t[1]推v
rep(i,1,len){
t[0]=g[u][i]*t[0];g[u][i].init();
t[1]=g[v][i-1]*t[1];
f[v][i-1]=f[v][i-1]*t[1];
t[2]=GetMat(f[v][i-1]);
f[u][i]=f[u][i]*t[0]*t[2];
}
if(len+1<Len)g[u][len+1]=g[u][len+1]*t[0]*t[2];
}
}
ll ret[N];
int main(){
rd(n);
rep(i,1,n)rd(a[i]);
rep(i,1,n)rd(b[i]),chkmx(b[i],a[i]);
rd(k);
int u,v;
rep(i,1,n-1){
rd(u),rd(v);
e[u].pb(v);
e[v].pb(u);
}
dfs1(k);
int Len=mxd[k]-dep[k]+1;
f[k]=new Vector[Len];
g[k]=new Matrix[Len];
dfs2(k);
Matrix t;t.init();
rep(i,0,Len-1){
t=g[k][i]*t;
f[k][i]=f[k][i]*t;
ret[i]=max({f[k][i].vec[0],f[k][i].vec[2]});
wt(ret[i]),pc(' ');
}
pc('\n');
return 0;
}
正确性经过和
暴力的代码
#include<bits/stdc++.h>
using namespace std;
namespace Abb{
#define fi first
#define se second
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define per(i, a, b) for (int i = (a); i >= (b); --i)
#define wl( x ) while ( x )
#define pb push_back
#define pf push_front
#define eb emplace_back
#define il inline
typedef pair<int,int> pii;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
typedef __int128 LL;
typedef unsigned __int128 uLL;
typedef pair<int,ll> pil;
typedef pair<ll,int> pli;
typedef pair<ll,ll> pll;
typedef short si;
template<typename T>
inline void chkmi(T& x,T y){x=x<y?x:y;}
template<typename T>
inline void chkmx(T& x,T y){x=x<y?y:x;}
}
using namespace Abb;
namespace FastIO{
#define gc getchar
#define pc putchar
//#define getchar getchar_unlocked
//#define putchar putchar_unlocked
template<typename T>
inline void rd(T& x){
int f=1;x=0;char ch=gc();
wl(ch<'0'||ch>'9'){
if(ch=='-')f=-1;
ch=gc();
}
wl(ch>='0'&&ch<='9'){
x=x*10+(ch^48);
ch=gc();
}
x*=f;
}
template<typename T>
inline void wt(T x){
if(x<0)pc('-'),x=-x;
if(x>9)wt(x/10);
pc(x%10^48);
}
}
using namespace FastIO;
const int N=15;
int n,q,k,a[N],b[N],bs[N];
vector<int>e[N];
int fa[N],dep[N],mxd[N];
void dfs1(int u){
mxd[u]=dep[u];
for(int v:e[u]){
if(v==fa[u])continue;
fa[v]=u;dep[v]=dep[u]+1;
dfs1(v);
chkmx(mxd[u],mxd[v]);
}
}
ll ret[N];
int main(){
rd(n);
rep(i,1,n)rd(a[i]);
rep(i,1,n)rd(b[i]);
rd(k);
int u,v;
rep(i,1,n-1){
rd(u),rd(v);
e[u].pb(v);
e[v].pb(u);
}
dfs1(k);
bs[0]=1;rep(i,1,n)bs[i]=3*bs[i-1];
rep(msk,0,bs[n]-1){
int nsk=msk,mx=0;
bool flag=1;ll res=0;
rep(u,1,n){
if(nsk%3==1)res+=a[u];
else if(nsk%3==2){
res+=b[u];
chkmx(mx,dep[u]);
bool mrk=0;
for(int v:e[u]){
mrk|=!(msk/bs[v-1]%3);
if(mrk)break;
}
flag&=mrk;
if(!flag)break;
}
nsk/=3;
}
if(flag)rep(i,mx,mxd[k])chkmx(ret[i],res);
}
rep(i,0,mxd[k])wt(ret[i]),pc(' ');pc('\n');
return 0;
}
数据生成器(由于是随机的数据强度不能保证)
#include<bits/stdc++.h>
using namespace std;
namespace Abb{
#define fi first
#define se second
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define per(i, a, b) for (int i = (a); i >= (b); --i)
#define wl( x ) while ( x )
#define pb push_back
#define pf push_front
#define eb emplace_back
#define il inline
typedef pair<int,int> pii;
typedef long long ll;
typedef double db;
typedef long double ld;
typedef __int128 LL;
typedef pair<int,ll> pil;
typedef pair<ll,int> pli;
typedef pair<ll,ll> pll;
typedef short si;
template<typename T>
inline void chkmi(T& x,T y){x=x<y?x:y;}
template<typename T>
inline void chkmx(T& x,T y){x=x<y?y:x;}
}
using namespace Abb;
namespace FastIO{
#define gc getchar
#define pc putchar
//#define getchar getchar_unlocked
//#define putchar putchar_unlocked
template<typename T>
inline void rd(T& x){
int f=1;x=0;char ch=gc();
wl(ch<'0'||ch>'9'){
if(ch=='-')f=-1;
ch=gc();
}
wl(ch>='0'&&ch<='9'){
x=x*10+(ch^48);
ch=gc();
}
x*=f;
}
template<typename T>
inline void wt(T x){
if(x<0)pc('-'),x=-x;
if(x>9)wt(x/10);
pc(x%10^48);
}
}
using namespace FastIO;
const int N=15;
mt19937 gen(time(0)^clock());
// 生成 n 个节点的随机无向树,返回边列表
vector<pii> generateRandomTree(int n) {
vector<pair<int, int>> edges;
if (n <= 1) return edges;
if (n == 2) {
edges.push_back({1, 2});
return edges;
}
// 1. 生成长度为 n-2 的 Prüfer 序列,每个元素在 [1, n]
uniform_int_distribution<int> dist(1, n);
vector<int> prufer(n - 2);
for (int i = 0; i < n - 2; ++i) {
prufer[i] = dist(gen);
}
// 2. 计算每个节点的度数:初始为 1,Prüfer 中出现一次度数 +1
vector<int> degree(n + 1, 1);
for (int x : prufer) {
++degree[x];
}
// 3. 把所有度数为 1 的节点放入最小堆
priority_queue<int, vector<int>, greater<int>> leaves;
for (int i = 1; i <= n; ++i) {
if (degree[i] == 1) {
leaves.push(i);
}
}
// 4. 根据 Prüfer 序列恢复树
for (int v : prufer) {
int u = leaves.top();
leaves.pop();
edges.push_back({u, v});
--degree[u];
--degree[v];
if (degree[v] == 1) {
leaves.push(v);
}
}
// 5. 最后剩下两个度数为 1 的节点,它们之间连一条边
int u = leaves.top();
leaves.pop();
int v = leaves.top();
leaves.pop();
edges.push_back({u, v});
return edges;
}
int n=1e5;
vector<pii>vc;
int main(){
wt(n);pc('\n');
rep(i,1,n)wt(gen()>>2),pc(' ');pc('\n');
rep(i,1,n)wt(gen()>>2),pc(' ');pc('\n');
wt(gen()%n+1);pc('\n');
vc=generateRandomTree(n);
for(auto it:vc)wt(it.fi),pc(' '),wt(it.se),pc('\n');
return 0;
}
后话:为了看看有没有更快的做法问了一下ds老师,结果ds老师的在链上
#include<bits/stdc++.h>
using namespace std;
namespace Abb{
#define fi first
#define se second
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define per(i, a, b) for (int i = (a); i >= (b); --i)
#define wl( x ) while ( x )
#define pb push_back
#define pf push_front
#define eb emplace_back
#define il inline
typedef pair<int,int> pii;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
typedef __int128 LL;
typedef unsigned __int128 uLL;
typedef pair<int,ll> pil;
typedef pair<ll,int> pli;
typedef pair<ll,ll> pll;
typedef short si;
template<typename T>
inline void chkmi(T& x,T y){x=x<y?x:y;}
template<typename T>
inline void chkmx(T& x,T y){x=x<y?y:x;}
}
using namespace Abb;
namespace FastIO{
#define gc getchar
#define pc putchar
//#define getchar getchar_unlocked
//#define putchar putchar_unlocked
template<typename T>
inline void rd(T& x){
int f=1;x=0;char ch=gc();
wl(ch<'0'||ch>'9'){
if(ch=='-')f=-1;
ch=gc();
}
wl(ch>='0'&&ch<='9'){
x=x*10+(ch^48);
ch=gc();
}
x*=f;
}
template<typename T>
inline void wt(T x){
if(x<0)pc('-'),x=-x;
if(x>9)wt(x/10);
pc(x%10^48);
}
}
using namespace FastIO;
const int N=2e6+5;
const ll inf=1e16;
int n,q,k,a[N],b[N];
vector<int>e[N];
struct Matrix{
ll mat[3][3];
il void init(){
mat[0][0]=0;mat[0][1]=-inf;mat[0][2]=-inf;
mat[1][0]=-inf;mat[1][1]=0;mat[1][2]=-inf;
mat[2][0]=-inf;mat[2][1]=-inf;mat[2][2]=0;
}
il Matrix operator*(const Matrix& tw)const{
return
{
{
{
max({mat[0][0]+tw.mat[0][0],mat[0][1]+tw.mat[1][0],mat[0][2]+tw.mat[2][0]})
,
max({mat[0][0]+tw.mat[0][1],mat[0][1]+tw.mat[1][1],mat[0][2]+tw.mat[2][1]})
,
max({mat[0][0]+tw.mat[0][2],mat[0][1]+tw.mat[1][2],mat[0][2]+tw.mat[2][2]})
}
,
{
max({mat[1][0]+tw.mat[0][0],mat[1][1]+tw.mat[1][0],mat[1][2]+tw.mat[2][0]})
,
max({mat[1][0]+tw.mat[0][1],mat[1][1]+tw.mat[1][1],mat[1][2]+tw.mat[2][1]})
,
max({mat[1][0]+tw.mat[0][2],mat[1][1]+tw.mat[1][2],mat[1][2]+tw.mat[2][2]})
}
,
{
max({mat[2][0]+tw.mat[0][0],mat[2][1]+tw.mat[1][0],mat[2][2]+tw.mat[2][0]})
,
max({mat[2][0]+tw.mat[0][1],mat[2][1]+tw.mat[1][1],mat[2][2]+tw.mat[2][1]})
,
max({mat[2][0]+tw.mat[0][2],mat[2][1]+tw.mat[1][2],mat[2][2]+tw.mat[2][2]})
}
}
}
;
}
};
struct Vector{
ll vec[3];
il Vector operator*(const Matrix& tw)const{
return
{
{
max({vec[0]+tw.mat[0][0],vec[1]+tw.mat[1][0],vec[2]+tw.mat[2][0]})
,
max({vec[0]+tw.mat[0][1],vec[1]+tw.mat[1][1],vec[2]+tw.mat[2][1]})
,
max({vec[0]+tw.mat[0][2],vec[1]+tw.mat[1][2],vec[2]+tw.mat[2][2]})
}
}
;
}
};
int fa[N],dep[N],mxd[N],lson[N],ord[N];
ll sum[N],dp[N][3];
Vector *f[N];Matrix *g[N];
il Matrix GetMat(Vector& tw){
return
{
{
{
max({tw.vec[0],tw.vec[1],tw.vec[2]})
,
-inf
,
-inf
}
,
{
-inf
,
max(tw.vec[0],tw.vec[2])
,
tw.vec[0]
}
,
{
-inf
,
-inf
,
max(tw.vec[0],tw.vec[2])
}
}
}
;
}
ll ret[N];
int main(){
rd(n);
rep(i,1,n)rd(a[i]);
rep(i,1,n)rd(b[i]),chkmx(b[i],a[i]);
rd(k);
int u,v;
rep(i,1,n-1){
rd(u),rd(v);
e[u].pb(v);
e[v].pb(u);
}
stack<int>stk;stk.push(k);
int now=0;
wl(!stk.empty()){
int u=stk.top();stk.pop();
ord[++now]=u;
mxd[u]=dep[u];
for(int v:e[u]){
if(v==fa[u])continue;
fa[v]=u;dep[v]=dep[u]+1;
stk.push(v);
}
}
per(tw,n,1){
int u=ord[tw];
ll mi=inf;
for(int v:e[u]){
if(v==fa[u])continue;
chkmi(mi,(ll)a[v]);
sum[u]+=sum[v];
if(mxd[v]>mxd[u]){
mxd[u]=mxd[v];
lson[u]=v;
}
}
dp[u][0]=sum[u];
dp[u][1]=sum[u]+b[u];
dp[u][2]=sum[u]+b[u]-mi;
sum[u]+=a[u];
chkmx(dp[u][2],sum[u]);
}
int Len=mxd[k]-dep[k]+1;
f[k]=new Vector[Len];
g[k]=new Matrix[Len];
rep(tw,1,n){
int u=ord[tw];
if(lson[u]){
f[lson[u]]=f[u]+1;
g[lson[u]]=g[u]+1;
}
for(int v:e[u]){
if(v==fa[u]||v==lson[u])continue;
int len=mxd[v]-dep[v]+1;
f[v]=new Vector[len];
g[v]=new Matrix[len];
}
}
per(tw,n,1){
int u=ord[tw];
f[u][0]={{dp[u][0],dp[u][1],dp[u][2]}};
g[u][0].init();
Matrix t[3];
int Len=mxd[u]-dep[u]+1;
if(Len>1){
t[0].mat[0][0]=0;t[0].mat[0][1]=b[u];t[0].mat[0][2]=b[u];
t[0].mat[1][0]=0;t[0].mat[1][1]=-inf;t[0].mat[1][2]=-inf;
t[0].mat[2][0]=0;t[0].mat[2][1]=b[u];t[0].mat[2][2]=a[u];
g[u][1]=g[u][1]*t[0];
}
for(int v:e[u]){
if(v==fa[u]||v==lson[u])continue;
int len=mxd[v]-dep[v]+1;
t[0].init();//t[0]推u
t[1].init();//t[1]推v
rep(i,1,len){
t[0]=g[u][i]*t[0];g[u][i].init();
t[1]=g[v][i-1]*t[1];
f[v][i-1]=f[v][i-1]*t[1];
t[2]=GetMat(f[v][i-1]);
f[u][i]=f[u][i]*t[0]*t[2];
}
if(len+1<Len)g[u][len+1]=g[u][len+1]*t[0]*t[2];
}
}
Matrix t;t.init();
rep(i,0,Len-1){
t=g[k][i]*t;
f[k][i]=f[k][i]*t;
ret[i]=max(f[k][i].vec[0],f[k][i].vec[2]);
wt(ret[i]),pc(' ');
}
pc('\n');
return 0;
}
话说现在编译器好像对dfs有什么特殊优化,dfs递归版时间<dfs序非递归版时间<bfs序版时间