QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#750233#2429. Conquer The World2020zymWA 8ms59168kbC++141.3kb2024-11-15 13:34:272024-11-15 13:34:27

Judging History

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

  • [2024-11-15 13:34:27]
  • 评测
  • 测评结果:WA
  • 用时:8ms
  • 内存:59168kb
  • [2024-11-15 13:34:27]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
long long ans,dep2[1000010],f[1000010];
int rt[1000010],n,cnt,h[1000010],b1[1000010],b2[1000010];
struct node{long long v1,v2;};
bool operator < (node x,node y){return x.v1<y.v1;}
multiset<node>s[1000010];
struct node2{int s,t,w;}a[1000010];
void add(int x,int y,int z){a[++cnt].s=h[x],a[cnt].t=y,a[cnt].w=z,h[x]=cnt;}
int merge(int x,int y){
	if(s[x].size()<s[y].size())swap(x,y);
	for(node i:s[y])s[x].insert(i);
	s[y].clear();
	return x;
}
void dfs(int x,int fa){
	rt[x]=x;
	if(b1[x]>b2[x])s[rt[x]].insert((node){dep2[x],b1[x]-b2[x]});
	else f[x]=b2[x]-b1[x];
	for(int i=h[x];i;i=a[i].s)
		if(a[i].t!=fa){
			
			dep2[a[i].t]=dep2[x]+a[i].w;
			dfs(a[i].t,x);
			
			ans+=a[i].w*1ll*f[a[i].t];
			
			f[x]+=f[a[i].t];
			rt[x]=merge(rt[x],rt[a[i].t]);
		}
	while(f[x]){
		
		if(s[rt[x]].empty())break;
		
		node op=(*s[rt[x]].begin());
		s[rt[x]].erase(s[rt[x]].begin());
		
		int gg=min(f[x],op.v2);
		f[x]-=gg,op.v2-=gg;
		
		ans+=(op.v1-dep2[x])*1ll*gg;
		
		if(op.v2!=0)s[rt[x]].insert(op);
	
	}
}
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n-1;i++){
		int x,y,z;
		scanf("%d%d%d",&x,&y,&z);
		add(x,y,z),add(y,x,z);
	}
	for(int i=1;i<=n;i++)scanf("%d%d",&b1[i],&b2[i]);
	dfs(1,0);
	printf("%lld\n",ans);
}

Details

Test #1:

score: 100
Accepted
time: 7ms
memory: 59168kb

Test #2:

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

Test #3:

score: 0
Accepted
time: 3ms
memory: 57144kb

Test #4:

score: -100
Wrong Answer
time: 8ms
memory: 57128kb