UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#191235#3386. 树计数gaojieming1001267ms44160kbC++1.0kb2023-10-08 14:50:582023-10-08 18:31:52

answer

#include<bits/stdc++.h>
#define ll long long
#define il inline
#define pn putchar('\n')
#define maxint 2147483647
#define min(x,y) (x<y?x:y)
#define max(x,y) (x>y?x:y)
#define mod 998244353
#define maxn 500005
#define int ll
using namespace std;
struct Gragh
{
    int v,nex;
}e[maxn<<1];
int n,top,tt,ans;
int a[maxn],c[maxn],si[maxn],sum[maxn];
il void build_gra(int u,int v)
{
    e[++top]=(Gragh){v,c[u]},c[u]=top;
}
il void upd(int &x,int y)
{
	x+=y;
	if(x>=mod)x-=mod;
	if(x<0)x+=mod;
}
il void dfs(int pos,int fa)
{
	si[pos]=1;
	for(int i=c[pos];i;i=e[i].nex)
	{
		int v=e[i].v;
		if(v==fa)continue;
		dfs(v,pos);
		si[pos]+=si[v];
		upd(ans,sum[v]*(n-si[v])%mod);
		upd(ans,sum[v]*sum[pos]%mod);
		upd(sum[pos],sum[v]);
	}
	upd(sum[pos],si[pos]);
}
signed main()
{
	scanf("%lld",&n);
	for(int i=1;i<=n;i++)
		scanf("%lld",&a[i]);
	for(int i=1;i<n;i++)
	{
		int u,v;
		scanf("%lld%lld",&u,&v);
		build_gra(u,v),build_gra(v,u);
	}
	dfs(1,0);
	printf("%lld",ans);
    return 0;
}

Details

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

Test #1:

score: 10
Accepted
time: 0ms
memory: 1208kb

input:

20
3 17 2 15 10 4 5 13 19 1 7 20 8 6 9 11 18 12 14 16
4 7
3 8
1 12
9 1
9 14
10 9
13 10
19 15
4 16
11...

output:

2826

result:

ok 1 number(s): "2826"

Test #2:

score: 10
Accepted
time: 1ms
memory: 1216kb

input:

98
2 77 64 22 40 76 45 19 56 14 84 52 63 24 81 21 13 48 8 38 11 6 15 89 28 18 50 54 93 82 85 62 44 1...

output:

415419

result:

ok 1 number(s): "415419"

Test #3:

score: 10
Accepted
time: 0ms
memory: 1212kb

input:

99
61 92 30 17 86 16 15 12 44 39 46 8 21 59 26 85 67 54 35 80 23 24 69 95 93 83 73 70 53 29 7 60 4 1...

output:

355654

result:

ok 1 number(s): "355654"

Test #4:

score: 10
Accepted
time: 0ms
memory: 1268kb

input:

1000
844 339 672 314 458 624 223 206 852 244 616 421 677 792 840 951 213 912 274 54 560 608 295 395 ...

output:

212773892

result:

ok 1 number(s): "212773892"

Test #5:

score: 10
Accepted
time: 0ms
memory: 1272kb

input:

998
652 84 859 373 51 203 193 141 746 872 457 109 210 930 242 158 405 645 299 304 632 259 499 46 322...

output:

685251349

result:

ok 1 number(s): "685251349"

Test #6:

score: 10
Accepted
time: 138ms
memory: 44156kb

input:

500000
493534 273026 242245 78527 129061 338625 141040 19308 123385 300331 26771 321717 193427 40601...

output:

431779943

result:

ok 1 number(s): "431779943"

Test #7:

score: 10
Accepted
time: 204ms
memory: 44160kb

input:

499999
312682 68740 179051 444262 443869 473195 473594 368488 25341 353475 357568 297752 7129 442748...

output:

791286765

result:

ok 1 number(s): "791286765"

Test #8:

score: 10
Accepted
time: 260ms
memory: 32476kb

input:

500000
455647 296864 43636 296093 132268 466123 333978 460076 109974 27544 296176 332000 130092 3482...

output:

65709067

result:

ok 1 number(s): "65709067"

Test #9:

score: 10
Accepted
time: 329ms
memory: 32496kb

input:

499999
298650 139497 134008 109978 451907 447533 381193 46277 493511 142163 261160 314126 424733 471...

output:

685301573

result:

ok 1 number(s): "685301573"

Test #10:

score: 10
Accepted
time: 335ms
memory: 32472kb

input:

500000
340262 127217 114286 61959 303434 90008 238624 300717 16690 381567 70308 466968 133612 149191...

output:

737983211

result:

ok 1 number(s): "737983211"