QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#320506#8217. King's Dinnerucup-team1303#WA 5ms36424kbC++146.3kb2024-02-03 17:26:202024-02-03 17:26:21

Judging History

你现在查看的是最新测评结果

  • [2024-02-03 17:26:21]
  • 评测
  • 测评结果:WA
  • 用时:5ms
  • 内存:36424kb
  • [2024-02-03 17:26:20]
  • 提交

answer

#include<bits/stdc++.h>

#define ll long long
#define mk make_pair
#define fi first
#define int long long
#define se second

using namespace std;

inline int read(){
	int x=0,f=1;char c=getchar();
	for(;(c<'0'||c>'9');c=getchar()){if(c=='-')f=-1;}
	for(;(c>='0'&&c<='9');c=getchar())x=x*10+(c&15);
	return x*f;
}

void cmax(ll &x,ll v){x=max(x,v);}
void cmin(ll &x,ll v){x=min(x,v);}

const int N=3e5+5;
vector<int>son[N],adj[N];
int n,cnt[N];
string str;
const ll INF=1e15;

void dfs(int u,int fa){
	for(int v:adj[u])if(v!=fa)son[u].emplace_back(v),dfs(v,u),cnt[u]++;
}

namespace solve1{

ll f[N][3],ans=0;
void DP(int u){
	for(int v:son[u])DP(v);
	f[u][0]=1;
	for(int v:son[u]){
		for(int j=0;j<=2;j++)ans+=f[u][j]*f[v][2-j];
		for(int j=0;j<=1;j++)f[u][j+1]+=f[v][j];
	}
}
void solve(){
	DP(1);
	cout<<ans<<endl;
}

};

namespace solve2_1{// aab / baa

ll g[N]; // ch[u] = b
vector<ll>f[N]; // ch[u] = a , j of son[u] = b
ll F[N],G[N];
// F[u] = max f[u][j] + j
// G[u] = max f[u][j] + cnt[u] - j
void DP(int u){
	for(int v:son[u])DP(v);
	f[u].resize(cnt[u]+1,-INF);

	// f
	f[u][0]=0;vector<ll>vals;
	for(int v:son[u])vals.emplace_back(g[v]-F[v]),f[u][0]+=F[v];
	sort(vals.begin(),vals.end(),greater<ll>());
	for(int j=1;j<=cnt[u];j++)f[u][j]=f[u][j-1]+vals[j-1];
	for(int j=0;j<=cnt[u];j++)f[u][j]+=1ll*j*(cnt[u]-j);

	// g
	g[u]=0;
	for(int v:son[u])g[u]+=max(G[v],g[v]);

	F[u]=G[u]=-INF;
	for(int j=0;j<=cnt[u];j++)cmax(F[u],f[u][j]+j),cmax(G[u],f[u][j]+cnt[u]-j);
}

void solve(){
	DP(1);
	ll ans=g[1];
	for(auto x:f[1])cmax(ans,x);
	cout<<ans<<endl;
}

}

namespace solve2_2{ // aba

ll f[N]; // ch[u] = a
vector<ll>g[N]; // ch[u] = b , j of son[u] = a
ll F[N],G[N];
// G[u] = max g[u][j]
// F[u] = max g[u][j] + j

void DP(int u){
	for(int v:son[u])DP(v);
	g[u].resize(cnt[u]+1,-INF);

	// g
	g[u][0]=0;vector<ll>vals;
	for(int v:son[u])vals.emplace_back(f[v]-G[v]),g[u][0]+=G[v];
	sort(vals.begin(),vals.end(),greater<ll>());
	for(int j=1;j<=cnt[u];j++)g[u][j]=g[u][j-1]+vals[j-1];
	for(int j=1;j<=cnt[u];j++)g[u][j]+=1ll*j*(j-1)/2;

	// f
	f[u]=0;
	for(int v:son[u])f[u]+=max(F[v],f[v]);

	F[u]=G[u]=-INF;
	for(int j=0;j<=cnt[u];j++)cmax(G[u],g[u][j]),cmax(F[u],g[u][j]+j);

	// cout<<"u = "<<u<<" f = "<<f[u]<<endl;
	// cout<<"g = ";for(int j=0;j<=cnt[u];j++)cout<<g[u][j]<<" \n"[j==cnt[u]];
}

void solve(){
	DP(1);
	ll ans=f[1];
	for(auto x:g[1])cmax(ans,x);
	cout<<ans*2<<endl;
}

}

namespace solve3{ // abc

ll f[N],g[N],h[N]; // ch[u] = a / b / c
ll F[N],G[N];
// F[u] : max g[u] + cnt_c
// G[u] : max g[u] + cnt_a

vector<ll> getvec(vector<ll>f,vector<ll>g,vector<ll>h){
	int n=f.size();vector<ll>L,R;
	if(n==0){
		vector<ll>res(1,0);
		return res;
	}
	// puts("getvec");
	// cout<<"f = ";for(int i=0;i<n;i++)cout<<f[i]<<" \n"[i==n-1];
	// cout<<"g = ";for(int i=0;i<n;i++)cout<<g[i]<<" \n"[i==n-1];
	// cout<<"h = ";for(int i=0;i<n;i++)cout<<h[i]<<" \n"[i==n-1];
	ll sum1=0,sum2=0,cnt2=0;
	vector<pair<ll,int> >adds;
	for(int v=0;v<n;v++){
		if(h[v]>=g[v])R.emplace_back(f[v]-h[v]),sum2+=h[v],cnt2++;
		else L.emplace_back(f[v]-g[v]),sum1+=g[v],adds.emplace_back(mk(g[v]-h[v],v));
	}
	sort(adds.begin(),adds.end());
	sort(R.begin(),R.end(),greater<ll>()),sort(L.begin(),L.end(),greater<ll>());

	int pl=0,pr=0;ll sum=0,cntR=0;
	auto Rel=[&](ll cur){
		// cout<<"Rel cur = "<<cur<<endl;
		while(pl<L.size()&&pr>0&&L[pl]>R[pr-1]-cur){
			sum+=L[pl],sum-=R[pr-1],cntR--;
			pl++,pr--;
		}
		while(pr<R.size()&&pl>0&&L[pl-1]<R[pr]-cur){
			sum+=R[pr],cntR++,sum-=L[pl-1];
			pl--,pr++;
		}
	};
	auto Ins=[&](ll cur){
		// cout<<"Ins cur = "<<cur<<endl;
		if(pl>=L.size()){assert(pr<R.size());sum+=R[pr],cntR++,pr++;return ;}
		if(pr>=R.size()){assert(pl<L.size());sum+=L[pl],pl++;return ;}
		if(L[pl]>R[pr]-cur)sum+=L[pl],pl++;
		else sum+=R[pr],cntR++,pr++;
	};
	auto Del=[&](ll cur){
		if(pl==0){assert(pr>0);sum-=R[pr-1],cntR--,pr--;return ;}
		if(pr==0){assert(pl>0);sum-=L[pl-1],pl--;return ;}
		if(L[pl-1]>R[pr]-cur)sum-=R[pr-1],pr--,cntR--;
		else sum-=L[pl-1],pl--;
	};
	auto cmp_g=[&](ll x,ll y){return x>y;};
	auto del_L=[&](ll val,ll cur){
		auto it=lower_bound(L.begin(),L.end(),val,cmp_g);
		if(it-L.begin()<pl)Ins(cur),pl--,sum-=val;L.erase(it);
		Rel(cur);
	};
	auto ins_R=[&](ll val,ll cur){
		auto it=lower_bound(R.begin(),R.end(),val,cmp_g);
		if(it-L.begin()<pr)Del(cur),pr++,sum+=val,cntR++;R.insert(it,val);
		Rel(cur);
	};

	// cout<<"now sum,cntR = "<<sum<<" "<<cntR<<" sum1,sum2,cnt2 = "<<sum1<<" "<<sum2<<" "<<cnt2<<endl;
	// cout<<"L = ";for(ll x:L)cout<<x<<" ";puts("");
	// cout<<"R = ";for(ll x:R)cout<<x<<" ";puts("");

	vector<ll>Ca(n+1);int p=0;
	for(int j=0;j<=n;j++){
		Ca[j]=sum-1ll*cntR*j+sum1+sum2+1ll*cnt2*j;
		// cout<<"Ca "<<j<<" = "<<Ca[j]<<endl;
		
		if(j+1<=n){
			Rel(j+1),Ins(j+1);
			while(p<adds.size()&&adds[p].fi<=j+1){
				int v=adds[p].se;
				// cout<<"v = "<<v<<endl;
				ins_R(f[v]-h[v],j+1),del_L(f[v]-g[v],j+1);
				sum2+=h[v],cnt2++,sum1-=g[v];
				p++;
			}
		}
	}

	return Ca;
}

void DP(int u){
	for(int v:son[u])DP(v);
	// cout<<"DP "<<u<<endl;

	for(int v:son[u]){
		f[u]+=max(F[v],max(f[v],h[v]));
		h[u]+=max(G[v],max(f[v],h[v]));
	}
	// cout<<"u = "<<u<<" f,h = "<<f[u]<<" "<<h[u]<<endl;

	vector<ll>ff,gg,hh;
	for(int v:son[u])ff.emplace_back(f[v]),gg.emplace_back(g[v]),hh.emplace_back(h[v]);
	auto Ca=getvec(ff,gg,hh),Cc=getvec(hh,gg,ff);

	F[u]=G[u]=-INF;
	for(int j=0;j<=cnt[u];j++)cmax(G[u],Ca[j]+j),cmax(F[u],Cc[j]+j),cmax(g[u],Ca[j]);
	// cout<<"u = "<<u<<" f,g,h = "<<f[u]<<" "<<g[u]<<" "<<h[u]<<" F,G = "<<F[u]<<" "<<G[u]<<endl;
}

void solve(){
	DP(1);
	cout<<max(f[1],max(g[1],h[1]))<<'\n';
}

}

void Solve(){
	cin>>n>>str;
	for(int i=1;i<=n-1;i++){
		int u=0,v=0;
		cin>>u>>v,adj[u].emplace_back(v),adj[v].emplace_back(u);
	}
	dfs(1,0);
	if(str.size()==1){cout<<n<<endl;return ;}
	if(str.size()==2){
		if(str[0]==str[1])cout<<2*(n-1)<<endl;
		else cout<<n-1<<endl;
		return ;
	}
	if(str[0]==str[1]&&str[1]==str[2])return solve1::solve();
	if(str[0]==str[1])return solve2_1::solve();
	if(str[0]==str[2])return solve2_2::solve();
	if(str[1]==str[2])return solve2_1::solve();
	solve3::solve();
}

signed main(void){

#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
#endif

	Solve();

	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 5ms
memory: 36424kb

input:

3
1
2
3

output:

3

result:

wrong answer Token parameter [name=row of the grid] equals to "3", doesn't correspond to pattern "[#.]+" (test case 1)