UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#198450#537. Graphhegm1002644ms177208kbC++2.8kb2023-11-26 11:23:192023-11-26 12:03:02

answer

#include<bits/stdc++.h>
#define M 3000005
#define N 500005
#define int long long
using namespace std;
int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
int n,m,mx,mn;
struct edge
{
	int u,v,w;
}e[M];
struct fig
{
	int to,next,w;
}k[M*2];int head[N],tot=1;
bool vis[N],pos[M];
void add(int from,int to,int w)
{
	k[++tot].to=to;
	k[tot].w=w;
	k[tot].next=head[from];
	head[from]=tot;
}
int dep[N],val[N],p[N],ans[N];
struct pt
{
	int b,k;
	void clen()
	{
		b=0;
		k=0;
	}
	pt operator +(pt a)
	{
		pt c;c.clen();
		c.b=a.b+b;
		c.k=a.k+k;
		return c;
	}
	pt operator -(pt a)
	{
		pt c;c.clen();
		c.b=b-a.b;
		c.k=k-a.k;
		return c;
	}
}s[N],sum;
void dfs(int now,int f)
{
	dep[now]=dep[f]+1;vis[now]=1;
	for(int i=head[now];i;i=k[i].next)
	{
		if(k[i].to==f||vis[k[i].to])continue;
		pos[i>>1]=1;
		val[k[i].to]=k[i].w-val[now]; 
		dfs(k[i].to,now);
	}
}
void solve(int now,int f)
{
	if(ans[now]<0||ans[now]>p[now])
	{
		cout<<"NIE\n";
		exit(0);
	}
	vis[now]=1;mx+=ans[now];mn+=ans[now]; 
	for(int i=head[now];i;i=k[i].next)
	{
		if(k[i].to==f||!pos[i>>1])continue;
		ans[k[i].to]=k[i].w-ans[now];
		solve(k[i].to,now); 
	}
}
int l,r; 
void get(int now,int f)
{
	vis[now]=1;
	int pl=0,pr=p[now];
	pl-=s[now].b;pr-=s[now].b;
	sum=sum+s[now];
	if(s[now].k==1)
	{
		l=max(pl,l);
		r=min(pr,r);
	}
	else
	{
		l=max(l,-1*pr);
		r=min(r,-1*pl);
	}
	for(int i=head[now];i;i=k[i].next)
	{
		if(k[i].to==f||!pos[i>>1])continue;
		pt p={k[i].w,0};p=p-s[now];
		s[k[i].to]=p;
		get(k[i].to,now);
	}
 } 
signed main()
{
	n=read();m=read();int sup=0;
	for(int i=1;i<=n;i++)p[i]=read(),sup+=p[i];
	for(int i=1;i<=m;i++)
	{
		e[i].u=read();
		e[i].v=read();
		e[i].w=read(); 
		add(e[i].u,e[i].v,e[i].w);
		add(e[i].v,e[i].u,e[i].w);
	}
	for(int i=1;i<=n;i++)if(!vis[i])dfs(i,0); 
	memset(vis,0,sizeof(vis));
	for(int i=1;i<=m;i++)
	{
		if(!pos[i])
		{
			if(dep[e[i].u]>dep[e[i].v])swap(e[i].u,e[i].v);
			int u=e[i].u,v=e[i].v;
			if((dep[v]-dep[u])%2)
			{
				if(e[i].w!=val[v]+val[u])
				{
					cout<<"NIE\n";
					return 0;
				}
			}
			else
			{
				int w=e[i].w-val[v]+val[u];
				if(w%2)
				{
					cout<<"NIE\n";
					return 0;
				}
				if(!vis[u])
				{
					ans[u]=w/2;
					solve(u,0); 
				}
				else
				{
					if(ans[u]!=w/2)
					{
						cout<<"NIE\n";
						return 0;
					}
				}
			}
		}
	}
	for(int i=1;i<=n;i++)
	{
		if(!vis[i])
		{
			s[i].b=0;
			s[i].k=1;
			l=0;r=p[i];
			sum.clen(); 
			get(i,0);
			mx+=max(sum.b+sum.k*l,sum.b+sum.k*r);
			mn+=min(sum.b+sum.k*l,sum.b+sum.k*r);
			if(l>r)
			{
				cout<<"NIE\n";
				return 0;
			}
		}
	}
	cout<<sup-mx<<" "<<sup-mn<<"\n";
	return 0;
}

Details

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

Subtask #1:

score: 20
Accepted

Test #1:

score: 20
Accepted
time: 244ms
memory: 60752kb

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: 1688kb

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: 26ms
memory: 18180kb

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: 0ms
memory: 1668kb

input:

1 0
10

output:

0 10

result:

ok single line: '0 10'

Test #5:

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

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: 0ms
memory: 1664kb

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: 1ms
memory: 1656kb

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: 0ms
memory: 1656kb

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: 1696kb

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: 1696kb

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: 1656kb

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: 1ms
memory: 1660kb

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: 0ms
memory: 1656kb

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: 0ms
memory: 1652kb

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: 1656kb

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: 0ms
memory: 1660kb

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: 1648kb

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: 0ms
memory: 1692kb

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: 1688kb

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: 1692kb

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: 0ms
memory: 2076kb

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: 0ms
memory: 2380kb

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: 1688kb

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: 0ms
memory: 2372kb

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: 0ms
memory: 2072kb

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: 0ms
memory: 1728kb

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: 109ms
memory: 25156kb

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: 1326ms
memory: 177208kb

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: 113ms
memory: 23428kb

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: 423ms
memory: 111828kb

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: 401ms
memory: 110480kb

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