QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#431474#7419. Jiry Matchingsfzj2007WA 5ms22196kbC++143.8kb2024-06-05 17:02:302024-06-05 17:02:31

Judging History

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

  • [2024-06-05 17:02:31]
  • 评测
  • 测评结果:WA
  • 用时:5ms
  • 内存:22196kb
  • [2024-06-05 17:02:30]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
template<typename T>inline void read(T &x){
	x=0;
	bool flag=0;
	char ch=getchar();
	while(ch<'0'||ch>'9') flag=flag||(ch=='-'),ch=getchar();
	while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
	x=flag?-x:x;
}
template<typename T,typename ...Args>inline void read(T &x,Args &...args){
	read(x),read(args...);
}
template<typename T>inline void prt(T x){
	if(x>9) prt(x/10);
	putchar(x%10+'0');
}
template<typename T>inline void put(T x){
	if(x<0) putchar('-'),x=-x;
	prt(x);
}
template<typename T>inline void put(char ch,T x){
	put(x),putchar(ch);
}
template<typename T,typename ...Args>inline void put(char ch,T x,Args ...args){
	put(ch,x),put(ch,args...);
}
#define ll long long
#define inf 0x3f3f3f3f3f3f3f3f
#define N 200005
#define convex vector<ll>
#define matrix array<array<convex,2>,2>
inline convex minkowski(const convex &a,const convex &b){
	if(!a.size()||!b.size()) return convex();
	convex c(a.size()+b.size()-1);
	c[0]=a[0]+b[0];
	int i=1,j=1,k=1;
	while(i<a.size()&&j<b.size()) 
		if(a[i]-a[i-1]<b[j]-b[j-1]) c[k++]=a[i]-a[i-1],i++;
		else c[k++]=b[j]-b[j-1],j++;
	while(i<a.size()) c[k++]=a[i]-a[i-1],i++;
	while(j<b.size()) c[k++]=b[j]-b[j-1],j++;
	for(int i=1;i<c.size();i++) c[i]+=c[i-1];
	return c;
}
inline convex operator+(convex a,const ll &b){
	convex c(a.size()+1);
	for(int i=0;i<a.size();i++) c[i+1]=a[i]+b;
	c[0]=-inf;
	return c;
}
inline convex max(const convex &a,const convex &b){
	convex c(max(a.size(),b.size()));
	for(int i=0;i<max(a.size(),b.size());i++){
		c[i]=-inf;
		if(i<a.size()) c[i]=max(c[i],a[i]);
		if(i<b.size()) c[i]=max(c[i],b[i]);
	}
	return c;
}
int dis[N],st[N];
convex f[N][2];
inline matrix merge1(int l,int r){
	matrix res;
	if(l==r){
		res[0][1]=res[1][0]=f[st[l]][0],res[1][1]=max(f[st[l]][0],f[st[l]][1]);
		return res;
	}
	int mid=l+r>>1;
	matrix L=merge1(l,mid),R=merge1(mid+1,r);
	for(int a=0;a<2;a++)
		for(int b=0;b<2;b++){
			res[a][b]=max(max(max(minkowski(L[a][0],R[0][b]),minkowski(L[a][0],R[0][b])+dis[mid]),minkowski(L[a][0],R[1][b])),max(minkowski(L[a][1],R[0][b]),minkowski(L[a][1],R[1][b])));
		}
	return res;
}
inline matrix merge2(int l,int r){
	matrix res;
	if(l==r){
		res[0][0]=f[st[l]][0],res[0][1]=f[st[l]][1];
		return res;
	}
	int mid=l+r>>1;
	matrix L=merge2(l,mid),R=merge2(mid+1,r);
	res[0][0]=minkowski(L[0][0],R[0][0]);
	res[0][1]=max(minkowski(L[0][1],R[0][0]),minkowski(L[0][0],R[0][1]));
	return res;
}
int n,fa[N],siz[N],son[N],vson[N],dep[N],dfn[N],top[N],idx;
vector<pair<int,int>> e[N];
inline void dfs1(int x,int fat){
	fa[x]=fat,siz[x]=1,dep[x]=dep[fat]+1;
	for(auto tmp:e[x]){
		int v=tmp.first;
		if(v==fat) continue;
		dfs1(v,x),siz[x]+=siz[v];
		if(siz[v]>siz[son[x]]) son[x]=v,vson[x]=tmp.second;
	}
}
inline void dfs2(int x,int tf){
	top[x]=tf,dfn[x]=++idx;
	if(son[x]) dfs2(son[x],tf);
	for(auto tmp:e[x])
		if(tmp.first!=fa[x]&&tmp.first!=son[x]) dfs2(tmp.first,tmp.first);
	if(x!=tf) return;
	for(int p=x;p;p=son[p]){
		int tp=0;
		for(auto tmp:e[p]){
			int v=tmp.first,w=tmp.second;
			if(v==fa[p]||v==son[p]) continue;
			st[++tp]=v;
			convex f0=f[v][0],f1=f[v][1];
			f[v][0]=max(f0,f1),f[v][1]=f0+w;
		}
		if(!tp) f[p][0]=f[p][1]={0};
		else{
			matrix res=merge2(1,tp);
			f[p][0]=res[0][0],f[p][1]=res[0][1];	
		}
	}
	int tp=0;
	for(int p=x;p;p=son[p]) st[++tp]=p,dis[tp]=vson[p];
	matrix res=merge1(1,tp);
	f[x][0]=max(res[0][0],res[0][1]),f[x][1]=max(res[1][0],res[1][1]);
}
int main(){
	read(n);
	for(int i=1,u,v,w;i<n;i++)
		read(u,v,w),e[u].emplace_back(v,w),e[v].emplace_back(u,w);
	dfs1(1,0),dfs2(1,1);
	convex res=max(f[1][0],f[1][1]);
	for(int i=1;i<n;i++){
		if(i<res.size()) put(' ',res[i]);
		else putchar('?'),putchar(' ');
	}
	return 0;
}


Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 20992kb

input:

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

output:

5 6 ? ? 

result:

ok single line: '5 6 ? ? '

Test #2:

score: 0
Accepted
time: 0ms
memory: 22196kb

input:

10
2 8 -5
5 10 5
3 4 -5
1 6 5
3 9 5
1 7 -3
4 8 -5
10 8 -5
1 8 -3

output:

5 10 15 10 ? ? ? ? ? 

result:

ok single line: '5 10 15 10 ? ? ? ? ? '

Test #3:

score: 0
Accepted
time: 2ms
memory: 22148kb

input:

2
1 2 35

output:

35 

result:

ok single line: '35 '

Test #4:

score: -100
Wrong Answer
time: 5ms
memory: 21804kb

input:

100
75 98 770328247
87 98 -219729992
98 35 578758971
98 93 -348642106
63 98 -638036803
83 25 -744163505
21 98 810313834
97 25 131957988
19 98 -711567435
8 25 68874404
43 98 184473508
28 94 171940607
92 28 971759198
51 98 -674532123
28 6 797727320
98 95 1154600
98 58 683765502
28 12 358426364
4 42 65...

output:

918736686 1909198130 2870682157 3825083089 4484694703 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 

result:

wrong answer 1st lines differ - expected: '990461444 1951945471 290634640...? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?', found: '918736686 1909198130 287068215... ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? '