QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#246181#7419. Jiry MatchingsczcWA 10ms48572kbC++144.9kb2023-11-10 17:00:382023-11-10 17:00:38

Judging History

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

  • [2024-06-05 15:26:21]
  • hack成功,自动添加数据
  • (/hack/645)
  • [2023-11-10 17:00:38]
  • 评测
  • 测评结果:WA
  • 用时:10ms
  • 内存:48572kb
  • [2023-11-10 17:00:38]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll INF=-1e17;
struct zhouji{
	vector<ll> v;
	inline zhouji operator * (zhouji ot){
		zhouji sum;
		sum.v.resize(v.size()+ot.v.size()-1);
		sum.v[0]=v[0]+ot.v[0];
		for(int i=1,j=1,k=1;i<(int)sum.v.size();i++){
			if(j==v.size()){sum.v[i]=sum.v[i-1]+ot.v[k]-ot.v[k-1]; k++; continue;}
			else if(k==ot.v.size()){sum.v[i]=sum.v[i-1]+v[j]-v[j-1]; j++; continue;}
			else if(v[j]-v[j-1]>ot.v[k]-ot.v[k-1]){ sum.v[i]=sum.v[i-1]+v[j]-v[j-1]; j++; }
			else{ sum.v[i]=sum.v[i-1]+ot.v[k]-ot.v[k-1]; k++; }
		}
		return sum;
	}
	inline zhouji operator * (ll x){
		zhouji sum;
		sum.v=v; sum.v.push_back(INF);
		for(int i=1;i<(int)sum.v.size();i++){
			sum.v[i]=max(sum.v[i],v[i-1]+x);
		}
		return sum;
	} 
	inline zhouji operator | (zhouji ot){
		zhouji sum;
		sum.v.resize(max(v.size(),ot.v.size()),INF);
		for(int i=0;i<(int)sum.v.size();i++){
			if(i<v.size()) sum.v[i]=max(sum.v[i],v[i]);
			if(i<ot.v.size()) sum.v[i]=max(sum.v[i],ot.v[i]);
		}
		return sum;
	}
	inline void print(){
		for(auto x:v){
			cout<<x<<" ";
		}
		cout<<endl;
	}
};
const int maxn=2e5+5;
vector< pair<ll,int> > G[maxn];
int n;
int siz[maxn],fa[maxn],dep[maxn];
ll val[maxn];
inline void dfs1(int x){
	if(fa[x]) G[x].erase(find_if(G[x].begin(),G[x].end(),[&](auto y){return y.second==fa[x];}));
	siz[x]=1;
	for(auto &y:G[x]){
		int v=y.second;
		fa[v]=x,dep[v]=dep[x]+1;
		dfs1(v);
		siz[x]+=siz[v];
		if(siz[v]>siz[G[x][0].second]) swap(y,G[x][0]);
	} 
}
int tot1=0;
zhouji g[2][maxn],f[2][maxn];
int top[maxn];
inline void get1(zhouji &x,zhouji &y,ll v){
//	cout<<"x: "<<endl;
//	x.print();
//	cout<<"y: "<<endl;
//	y.print();
	tot1++;
	g[0][tot1]=x|y,g[1][tot1]=x*v;
	x.v.clear(); y.v.clear();
}
inline void solve_light(int l,int r){
	if(l==r) return ;
	int mid=(l+r)>>1;
	solve_light(l,mid),solve_light(mid+1,r);
	zhouji a=g[0][l]*g[0][mid+1];
	zhouji b=g[1][l]*g[0][mid+1];
	zhouji c=g[0][l]*g[1][mid+1];
	g[0][l]=a;
	g[1][l]=b|c;
	g[0][mid+1].v.clear(),g[1][mid+1].v.clear();
}
int tot2=0;
zhouji h[2][2][maxn];
inline void get2(zhouji &x,zhouji &y,ll v){
//	cout<<"begin:\n";
//	x.print();
//	y.print();
//	cout<<"end:\n";
	tot2++;
	val[tot2]=v;
	h[0][1][tot2].v.clear(); h[1][0][tot2].v.clear();
	h[0][0][tot2]=x; h[1][1][tot2]=y;
	x.v.clear();
	y.v.clear();
}
inline void solve_weigh(int l,int r){
	if(l==r) return ;
	int mid=(l+r)>>1;
	solve_weigh(l,mid),solve_weigh(mid+1,r);
	if(l+1==r){
		zhouji tp=(h[0][0][l]*h[0][0][mid+1])*val[mid];
		tp.print();
//		cout<<"cnmdczc\n";
		for(int op1=0;op1<=1;op1++){
			zhouji t=h[op1][op1][l];
			for(int op2=0;op2<=1;op2++){
				h[op1][op2][l]=t*h[op2][op2][mid+1];
			}
		}
		h[1][1][l]=h[1][1][l]|tp;
	}
	else if(l+2==r){
		zhouji tp0=(h[0][0][l]*h[0][0][mid+1])*val[mid],tp1=(h[1][0][l]*h[0][0][mid+1])*val[mid];
		for(int op1=0;op1<=1;op1++){
			zhouji t0=h[op1][0][l],t1=h[op1][1][l];
			for(int op2=0;op2<=1;op2++){
				h[op1][op2][l]=(t0*h[op2][op2][mid+1])|(t1*h[op2][op2][mid+1]);
			}
		}
		h[0][1][l]=h[0][1][l]|tp0;
		h[1][1][l]=h[1][1][l]|tp1;
	}
	else{
		for(int op1=0;op1<=1;op1++){
			zhouji t0=h[op1][0][l],t1=h[op1][1][l];
			for(int op2=0;op2<=1;op2++){
				zhouji a=(t0*h[0][op2][mid+1])*val[mid];
				zhouji b=(t0|t1)*(h[0][op2][mid+1],h[1][op2][mid+1]);
				h[op1][op2][l]=a|b;
			}
		}
	}
	h[0][0][mid+1].v.clear();
	h[0][1][mid+1].v.clear();
	h[1][0][mid+1].v.clear();
	h[1][1][mid+1].v.clear();
}
inline void dfs2(int x){
//	cout<<"start: "<<x<<endl;
	if(!G[x].size()){
		f[0][x].v.resize(1,0);
		f[1][x].v.resize(1,0);
//		cout<<"end: fuck zhouji"<<x<<endl;
		return ;
	}
	for(auto y:G[x]){
//		cout<<x<<"->"<<y.second<<endl;
		int v=y.second;
		top[v]= v==G[x][0].second?top[x]:v;
		dfs2(v);
	}
	tot1=0;
	for(auto y:G[x]){
		int v=y.second;
		if(v!=G[x][0].second){
			get1(f[0][v],f[1][v],y.first);
		}
//		cout<<"ok"<<x<<"->"<<y.second<<endl;
	}
//	cout<<"YES:"<<x<<endl;
	if(!tot1){
		f[0][x].v.resize(1,0);
		f[1][x].v.resize(1,0);
	}
	else{
//		cout<<"YES begin light"<<tot1<<endl;
		solve_light(1,tot1);
//		cout<<"NO\n";
		f[0][x].v=g[0][1].v;
		f[1][x].v=g[1][1].v;
	}
//	cout<<"begin: "<<x<<endl;
//	f[0][x].print();
//	f[1][x].print();
//	cout<<"end:"<<x<<endl;
	if(top[x]==x){
		tot2=0;
		for(int czc=x; top[czc]==x ;czc=G[czc][0].second){
			get2(f[0][czc],f[1][czc],G[czc].size()?G[czc][0].first:0);
			if(!G[czc].size()) break;
		}
		solve_weigh(1,tot2);
		f[0][x]=h[0][0][1]|h[0][1][1];
		f[1][x]=h[1][0][1]|h[1][1][1];
	}
}
int main(){
	scanf("%d",&n);
	for(int i=1,x,y;i<n;i++){ ll z;
		scanf("%d%d%lld",&x,&y,&z);  
		G[x].push_back({z,y});
		G[y].push_back({z,x});
	}
	dep[1]=1; dfs1(1);
	top[1]=1; dfs2(1);
	zhouji ans=f[0][1]|f[1][1];
	for(int i=1;i<n;i++){
		if(i<ans.v.size()) printf("%lld ",ans.v[i]);
		else printf("? ");
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 10ms
memory: 48572kb

input:

5
1 2 3
2 3 5
2 4 4
3 5 2

output:

0 3 
0 2 
5 6 ? ? 

result:

wrong answer 1st lines differ - expected: '5 6 ? ?', found: '0 3 '