QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#465228#6406. Stage CleardengrkWA 0ms3956kbC++143.0kb2024-07-06 19:07:312024-07-06 19:07:31

Judging History

This is the latest submission verdict.

  • [2024-08-15 21:05:17]
  • hack成功,自动添加数据
  • (/hack/778)
  • [2024-07-06 19:07:31]
  • Judged
  • Verdict: WA
  • Time: 0ms
  • Memory: 3956kb
  • [2024-07-06 19:07:31]
  • Submitted

answer

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N=75;
struct Edge{
	int u,v;
};
Edge edge[N];
bool in_tree[N],ban[N];
int n,m,fa[N],cnt[N],fr[N];
int len_list=0,e[N],ne[N],h[N];
long long a[N],b[N],c[N],d[N];
void add_once(int a,int b){
	e[len_list]=b;
	ne[len_list]=h[a];
	h[a]=len_list;
	len_list++,cnt[a]++;
}
bool cmp(int u,int v){
	if((c[u]<d[u])!=(c[v]<d[v])) return c[u]<d[u];
	if(c[u]<d[u]) return c[u]<c[v];
	return d[u]>d[v];
}
void dfs(int u,int from){
	fr[u]=from;
	for(int i=h[u];i>=0;i=ne[i])
		dfs(e[i],i);
}
struct Heap{
	int n,a[N],p[N];
	void up(int u){
		while(u>1&&cmp(a[u],a[u/2])){
			swap(p[a[u]],p[a[u/2]]);
			swap(a[u],a[u/2]); u/=2;
		}
	}
	void down(int u){
		while(u*2<=n&&cmp(a[u*2],a[u])||u*2<n&&cmp(a[u*2+1],a[u]))
			if(u*2==n||cmp(a[u*2],a[u*2+1])){
				swap(p[a[u]],p[a[u*2]]);
				swap(a[u],a[u*2]); u*=2;
			}
			else{
				swap(p[a[u]],p[a[u*2+1]]);
				swap(a[u],a[u*2+1]); u=u*2+1;
			}
	}
	void push(int x){
		a[++n]=x; p[x]=n; up(n);
	}
	void pop(){
		if(n==1){
			p[a[1]]=0;
			n=0; return ;
		}
		p[a[1]]=0,p[a[n]]=1;
		a[1]=a[n--]; down(1);
	}
	void erase(int x){
		if(n==1){
			p[a[1]]=0;
			n=0; return ;
		}
		p[a[n]]=p[x]; a[p[x]]=a[n--];
		up(p[x]),down(p[x]); p[x]=0;
	}
	bool empty(){
		return n==0;
	}
	int top(){
		return a[1];
	}
};
Heap hp;
int merge(int u){
	int v=fa[u],i,la;
	long long nd=d[u]-c[u]+d[v]-c[v];
	long long na=max(c[v],c[v]-d[v]+c[u]);
	long long nb=na+nd;
	if(cnt[u]>cnt[v]){
		for(i=h[v],la=-1;i>=0;i=ne[i])
			if(e[i]==u)
				if(la==-1) h[v]=ne[i];
				else ne[la]=ne[i];
			else fa[e[i]]=u,la=i;
		fa[u]=fa[v],fr[u]=fr[v],e[fr[v]]=u;
		if(la>=0) ne[la]=h[u],h[u]=h[v],h[v]=-1;
		ban[v]=1,cnt[u]+=cnt[v],cnt[v]=0;
		c[u]=na,d[u]=nb;
		return u;
	}
	else{
		for(i=h[u],la=-1;i>=0;i=ne[i])
			fa[e[i]]=v,la=i;
		if(la>=0) ne[la]=h[v],h[v]=h[u],h[u]=-1;
		ban[u]=1,cnt[v]+=cnt[u],cnt[u]=0;
		c[v]=na,d[v]=nb;
		return v;
	}
}
long long solve(){
	int i,s1; len_list=0;
	long long ans=0,sum=0;
	memset(h,-1,sizeof h);
	memset(ban,0,sizeof ban);
	memset(cnt,0,sizeof cnt);
	for(i=1;i<=m;i++){
		if(!in_tree[i]) continue;
		fa[edge[i].v]=edge[i].u;
		add_once(edge[i].u,edge[i].v);
	}
	dfs(1,-1);
	for(i=1;i<=n;i++)
		c[i]=a[i],d[i]=b[i];
	for(i=2;i<=n;i++) hp.push(i);
	while(!hp.empty()){
		s1=hp.top(),hp.pop();
		if(fa[s1]==1) continue;
		if(!cmp(s1,fa[s1])) continue;
		hp.erase(fa[s1]);
		hp.push(merge(s1));
	}
	hp.push(1);
	while(!hp.empty()){
		s1=hp.top(),hp.pop();
		ans=min(ans,sum-c[s1]);
		sum=sum-c[s1]+d[s1];
		for(i=h[s1];i>=0;i=ne[i])
			if(!ban[e[i]]) hp.push(e[i]);
	}
	return -ans;
}
int main(){
//	freopen("HP.in","r",stdin);
//	freopen("HP.out","w",stdout);
	scanf("%d%d",&n,&m);
	for(int i=2;i<=n;i++)
		scanf("%lld%lld",&a[i],&b[i]);
	for(int i=1;i<=m;i++)
		scanf("%d%d",&edge[i].u,&edge[i].v);
	for(int i=1;i<n;i++) in_tree[i]=1;
	printf("%lld",solve());
//	fclose(stdin);
//	fclose(stdout);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

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

output:

4

result:

ok 1 number(s): "4"

Test #2:

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

input:

15 14
254040392438309 117083115436273
500005748229691 557255157630172
821034233718230 865199673774998
659892147898798 987564141425694
81172575487567 811635577877255
751768357864605 341103322647288
454926350150218 140191090713900
921608121471585 659295670987251
223751724062143 505619245326640
8907765...

output:

1665396301509143

result:

ok 1 number(s): "1665396301509143"

Test #3:

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

input:

18 17
636830992776530 847574431876821
330869946457865 78274534165482
450581372553540 11565219334965
8736347226844 17186323694285
870805093198860 559070167736042
674369178493171 930151818400874
641605209598997 222521062460239
450936030349531 469197172169023
831295459816974 626096008793091
53095460351...

output:

2375957544280218

result:

ok 1 number(s): "2375957544280218"

Test #4:

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

input:

20 19
539893468691183 767805205447882
240338186903141 960937349402327
942645580569365 896509929612645
542601575005817 191461109090531
540992546866047 765080044816119
904535155855114 858111921213175
452499200048240 115895143306864
983856946412026 838504718536099
586421298181479 265212699386882
677124...

output:

800919806038419

result:

ok 1 number(s): "800919806038419"

Test #5:

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

input:

24 23
114281007218527 308690671179962
145951034437731 718976086594208
709172151907814 926071954787084
224496444610281 498657753059525
874422017133378 857676356343078
532175866197017 818525693672607
303837639402605 374469705563954
512244364294540 952911486867703
748959419417502 249992707230361
512696...

output:

114281007218527

result:

ok 1 number(s): "114281007218527"

Test #6:

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

input:

36 35
389328367777319 678636570542258
32216944647452 612585362150577
891592845704885 596030605892036
688825276167602 461516360471825
916552899998310 106733202183953
400050408958777 670724326933521
995792861502757 894514508573875
14511185222713 612305257166443
175168368096281 508263855969282
85578802...

output:

171942144116875

result:

ok 1 number(s): "171942144116875"

Test #7:

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

input:

36 35
759037289890767 849577210686635
16379509883489 441829377955433
589378488455351 990818352083122
871208015900506 727359003875494
207852561142533 28766987248469
81321183353129 892618157632070
198487099788393 519364502513651
83942803274015 988821788304459
868185445880277 269956013388079
3834515054...

output:

759037289890767

result:

ok 1 number(s): "759037289890767"

Test #8:

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

input:

36 35
100792831728257 823656493168793
866936535786311 187861146327778
132998929717538 605906559206892
3319598846477 393401056223733
964444786730964 932398059281618
925176496607384 148825907337833
985037559482190 646827297289525
469876125353024 641923164294854
453796287874442 291205025001534
72806942...

output:

1397699717661157

result:

ok 1 number(s): "1397699717661157"

Test #9:

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

input:

36 36
245996406159980 462974248377488
839352152971124 40282565369163
572695144110271 507726167903341
671102350267895 18090181781241
643724978558334 551787913319524
936340565446887 517649577919257
158127116487034 175750969611510
396852573858996 670814068366285
534702788102341 124550558279140
69441153...

output:

2508008255775889

result:

ok 1 number(s): "2508008255775889"

Test #10:

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

input:

34 38
656738239290510 319959252044415
760511943177376 828562698756504
470087249708484 441916827764162
105399930988058 761192720347117
81742549616394 195819875734286
782982110569406 72384154665629
647269989285797 720280547207448
531182311814386 160821851115134
292963780645658 871789628567253
74499577...

output:

656738239290510

result:

ok 1 number(s): "656738239290510"

Test #11:

score: -100
Wrong Answer
time: 0ms
memory: 3868kb

input:

32 40
818105834607446 689904077664886
717146597564762 686987602224041
538827104521875 147060924732538
604913134601443 802546720879673
45376965619246 480061093729529
686039951678173 889398415870480
374408509732957 354006189233817
103818950629279 863526642478066
719174876808085 130061851080766
9744074...

output:

3237589908654880

result:

wrong answer 1st numbers differ - expected: '2289520618562758', found: '3237589908654880'