UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#198448#537. Graphwosile1003546ms162216kbC++112.6kb2023-11-26 11:12:372023-11-26 12:02:47

answer

#include<bits/stdc++.h>
using namespace std;
int n,m;
typedef long long ll;
ll p[500005];
struct edge{
	int to;
	ll val;
	edge(){}
	edge(int to_,int val_):to(to_),val(val_){}
};
vector<edge>G[500005];
ll ans=1,mx=0,mn=0;
int vis[500005],c[500005];
int k[500005];
ll b[500005];
int tmp[500005],id[500005];
int fl;
int pos;
void color(int x,int cl){
	tmp[++pos]=x;
	id[x]=pos;
	c[x]=cl;
	vis[x]=1;
	for(edge e:G[x]){
		if(vis[e.to]==0)color(e.to,-cl);
		else if(c[e.to]==cl)fl=0;
	}
}
void solve(int x){
	fl=1;
	pos=0;
	color(x,1);
	for(int i=1;i<=pos;i++)k[i]=0;
	k[1]=1,b[1]=0;
	if(fl){
		for(int i=1;i<=pos;i++)for(edge e:G[tmp[i]]){
			int v=id[e.to];
			if(k[v]==0)k[v]=-k[i],b[v]=e.val-b[i];
			else if(b[i]+b[v]!=e.val)ans=0;
		}
//		for(int i=1;i<=pos;i++)printf("%d %d %lld\n",tmp[i],k[i],b[i]);
		ll xmx=0x3f3f3f3f3f3f3f3f,xmn=0;
		for(int i=1;i<=pos;i++){
			if(k[i]==1)xmx=min(xmx,p[tmp[i]]-b[i]),xmn=max(xmn,-b[i]);
			else xmx=min(xmx,b[i]),xmn=max(xmn,b[i]-p[tmp[i]]);
		}
		ll sum1=0,sum2=0;
		for(int i=1;i<=pos;i++)sum1+=xmx*k[i]+b[i],sum2+=xmn*k[i]+b[i];
		if(xmx<xmn)ans=0;
		mx+=max(sum1,sum2),mn+=min(sum1,sum2);
	}
	else{
		ll vx=-1;
		for(int i=1;i<=pos;i++)for(edge e:G[tmp[i]]){
			int v=id[e.to];
			if(k[v]==0)k[v]=-k[i],b[v]=e.val-b[i];
			else if(k[i]!=k[v]){
				if(b[i]+b[v]!=e.val)ans=0;
			}
			else{
				if((e.val-b[i]-b[v])&1)ans=0;
				if(vx!=-1 && vx!=(e.val-b[i]-b[v])/(k[i]+k[v]))ans=0;
				else vx=(e.val-b[i]-b[v])/(k[i]+k[v]);
				if(vx<0)ans=0;
			}
		}
//		for(int i=1;i<=pos;i++)printf("%d %d %lld\n",tmp[i],k[i],b[i]);
//		printf("_ans=%d, vx=%d\n",ans,vx);
		for(int i=1;i<=pos;i++)b[i]+=k[i]*vx;
		for(int i=1;i<=pos;i++)if(b[i]<0 || b[i]>p[tmp[i]])ans=0;
		for(int i=1;i<=pos;i++)for(edge e:G[tmp[i]])if(b[i]+b[id[e.to]]!=e.val)ans=0;
		ll sum=0;
		for(int i=1;i<=pos;i++)sum+=b[i];
		mx+=sum,mn+=sum; 
	} 
//	printf("ans=%d\n",ans);
}
int read(){
	int x=0,c=getchar()-48;
	while(c<0 || c>9)c=getchar()-48;
	while(c>=0 && c<=9){
		x=x*10+c;
		c=getchar()-48;
	}
	return x;
}
int main(){
//	freopen("N537.in","r",stdin);
//	freopen("N537.out","w",stdout);
	n=read();
	m=read();
	for(int i=1;i<=n;i++)p[i]=read();
	ll tot=0;
	for(int i=1;i<=n;i++)tot+=p[i];
	for(int i=1;i<=m;i++){
		int u=read();
		int v=read();
		int w=read();
		G[u].push_back(edge(v,w));
		G[v].push_back(edge(u,w));
//		printf("connect %d %d %d\n",u,v,w);
	}
	for(int i=1;i<=n;i++)if(vis[i]==0)solve(i);
	if(ans==0)printf("NIE");
	else printf("%lld %lld\n",tot-mx,tot-mn);
	return 0;
	//quod erat demonstrandum
}

Details

小提示:点击横条可展开更详细的信息

Subtask #1:

score: 20
Accepted

Test #1:

score: 20
Accepted
time: 367ms
memory: 53776kb

input:

500000 499999
1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 100000...

output:

375223613298 375223613298

result:

ok single line: '375223613298 375223613298'

Test #2:

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

input:

7 6
20 20 20 20 20 20 20
1 3 5
1 4 6
1 5 7
1 2 8
2 7 10
2 6 20

output:

100 105

result:

ok single line: '100 105'

Test #3:

score: 0
Accepted
time: 37ms
memory: 25816kb

input:

100000 99999
100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 100...

output:

7500000000 7500000000

result:

ok single line: '7500000000 7500000000'

Test #4:

score: 0
Accepted
time: 4ms
memory: 12896kb

input:

1 0
10

output:

0 10

result:

ok single line: '0 10'

Test #5:

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

input:

3 2
5 10 5
1 2 5
2 3 3

output:

12 15

result:

ok single line: '12 15'

Subtask #2:

score: 20
Accepted

Test #6:

score: 20
Accepted
time: 4ms
memory: 12924kb

input:

20 54
611 408 813 673 406 352 106 741 490 585 410 621 624 607 756 747 595 889 452 420
1 3 842
1 4 76...

output:

NIE

result:

ok single line: 'NIE'

Test #7:

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

input:

20 19
1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000...

output:

NIE

result:

ok single line: 'NIE'

Test #8:

score: 0
Accepted
time: 4ms
memory: 12916kb

input:

4 6
50 50 50 50
1 2 10
1 4 12
1 3 17
2 3 5
2 4 9
3 4 11

output:

NIE

result:

ok single line: 'NIE'

Test #9:

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

input:

20 49
372 528 400 691 622 545 692 385 702 475 509 359 671 724 637 763 420 262 687 193
1 7 614
2 9 46...

output:

6308 6308

result:

ok single line: '6308 6308'

Test #10:

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

input:

20 45
604 785 589 728 765 933 296 281 519 502 398 699 768 381 705 694 470 498 184 455
1 3 475
1 7 58...

output:

5886 5886

result:

ok single line: '5886 5886'

Test #11:

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

input:

20 19
1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000...

output:

NIE

result:

ok single line: 'NIE'

Test #12:

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

input:

20 53
371 459 636 413 341 391 633 591 366 333 463 476 704 517 411 686 333 204 333 204
1 4 410
1 7 24...

output:

NIE

result:

ok single line: 'NIE'

Test #13:

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

input:

5 4
10 10 10 10 10
1 2 20
2 3 20
3 4 19
4 5 20

output:

NIE

result:

ok single line: 'NIE'

Test #14:

score: 0
Accepted
time: 4ms
memory: 12916kb

input:

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

output:

NIE

result:

ok single line: 'NIE'

Test #15:

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

input:

20 19
1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000...

output:

NIE

result:

ok single line: 'NIE'

Test #16:

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

input:

20 56
236 561 106 723 251 538 406 301 786 541 392 563 449 238 385 725 606 630 97 452
1 7 255
1 19 20...

output:

NIE

result:

ok single line: 'NIE'

Test #17:

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

input:

3 3
1 1 1
1 2 1
1 3 1
3 2 1

output:

NIE

result:

ok single line: 'NIE'

Test #18:

score: 0
Accepted
time: 4ms
memory: 12936kb

input:

10 12
20 20 20 20 20 6 8 15 5 8
1 3 7
2 5 11
2 4 8
3 2 8
6 8 10
6 10 2
7 8 12
7 9 4
7 10 4
8 9 8
8 1...

output:

103 110

result:

ok single line: '103 110'

Test #19:

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

input:

10 17
2 3 4 5 6 3 4 4 2 6
1 4 3
1 6 2
2 5 5
2 7 5
2 8 3
3 4 3
3 6 2
3 7 5
4 6 3
5 6 5
6 7 5
6 8 3
6 ...

output:

17 17

result:

ok single line: '17 17'

Test #20:

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

input:

20 43
469 606 497 329 465 804 295 257 397 428 385 442 496 307 60 660 300 367 745 753
1 7 529
2 3 724...

output:

3618 3618

result:

ok single line: '3618 3618'

Subtask #3:

score: 20
Accepted

Test #21:

score: 20
Accepted
time: 3ms
memory: 13216kb

input:

2000 5000
1000 459 636 413 341 391 633 591 366 333 463 476 704 517 411 686 333 204 333 204 531270 39...

output:

NIE

result:

ok single line: 'NIE'

Test #22:

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

input:

2000 7897
1134 1373 3908 2580 3081 1342 1545 1277 557 1142 1632 2605 2084 530 1962 1395 1605 1990 12...

output:

1991741 2021812

result:

ok single line: '1991741 2021812'

Test #23:

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

input:

200 411
1000 528 400 691 622 545 692 385 702 475 509 359 671 724 637 763 420 262 687 193 1000 1000 1...

output:

NIE

result:

ok single line: 'NIE'

Test #24:

score: 0
Accepted
time: 4ms
memory: 13508kb

input:

2000 7920
666742 779080 442993 328291 788067 342890 82684 639283 627804 289997 471190 142968 481334 ...

output:

501277468 504496090

result:

ok single line: '501277468 504496090'

Test #25:

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

input:

2000 5019
1000 408 813 673 406 352 106 741 490 585 410 621 624 607 756 747 595 889 452 420 1151 786 ...

output:

NIE

result:

ok single line: 'NIE'

Test #26:

score: 0
Accepted
time: 4ms
memory: 12968kb

input:

200 465
1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 10...

output:

89626 96112

result:

ok single line: '89626 96112'

Subtask #4:

score: 40
Accepted

Test #27:

score: 40
Accepted
time: 129ms
memory: 31008kb

input:

100000 249888
200000 200000 200000 200000 200000 200000 200000 200000 200000 200000 200000 200000 20...

output:

9985812930 10008914849

result:

ok single line: '9985812930 10008914849'

Test #28:

score: 0
Accepted
time: 1688ms
memory: 162216kb

input:

500000 1999856
361637 221953 576655 788104 700869 141844 782159 530312 54506 549072 222999 244082 43...

output:

124480869742 125389681031

result:

ok single line: '124480869742 125389681031'

Test #29:

score: 0
Accepted
time: 114ms
memory: 28316kb

input:

100000 259677
200000 785 589 728 765 933 296 281 519 502 398 699 768 381 705 694 470 498 184 455 200...

output:

NIE

result:

ok single line: 'NIE'

Test #30:

score: 0
Accepted
time: 394ms
memory: 80732kb

input:

500000 1215391
1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 10000...

output:

247892806870 250302034759

result:

ok single line: '247892806870 250302034759'

Test #31:

score: 0
Accepted
time: 771ms
memory: 89876kb

input:

500000 1299594
2000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 ...

output:

NIE

result:

ok single line: 'NIE'

Extra Test:

score: 0
Extra Test Passed