UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#191264#3386. 树计数shiziping1001253ms59756kbC++111.6kb2023-10-08 16:09:042023-10-08 18:36:49

answer

#include <iostream>
using namespace std;
#define int long long
const int maxn = 500100,mod = 998244353;
const int inv2 = ((mod+1)>>1);
int siz[maxn],a[maxn],n,qwq[maxn];
int head[maxn],to[maxn<<1],nxt[maxn<<1],cnt = 0;
int read() {
	int ans = 0,f = 1;
	char c = getchar();
	while(c > '9' || c < '0') {if(c == '-') f = -1; c = getchar();}
	while(c <= '9' && c >= '0') {ans = (ans<<3)+(ans<<1)+c-'0'; c = getchar();}
	return ans*f;
}
void add(int u,int v) {
	cnt++;
	to[cnt] = v;
	nxt[cnt] = head[u];
	head[u] = cnt;
}
int sum = 0;
void dfs(int now,int pa) {
	siz[now] = 1;
	for(int i = head[now]; i; i = nxt[i]) {
		if(to[i] == pa) continue;
		dfs(to[i],now);
		(siz[now] += siz[to[i]]) %= mod;
		(qwq[now] += qwq[to[i]]) %= mod;
	}
	(qwq[now] += siz[now]) %= mod;
}
int su(int a,int b) {return ((a-b>=0)?(a-b):(a-b+mod));}
int ad(int a,int b) {return ((a+b<mod)?(a+b):(a+b-mod));}
void dfss(int now,int pa,int from) { //from is qwq[now] after changing root
	int ans = 0;
	for(int i = head[now]; i; i = nxt[i]) {
		if(to[i] == pa) continue;
		ans = (ans+qwq[to[i]]*su(n,siz[to[i]])%mod)%mod;
		dfss(to[i],now,ad(su(su(from,siz[to[i]]),siz[to[i]]),n));
	}
	//cout << ans << ' ' << from << ' ' << "j" << endl;
	if(now != 1) ans = (ans+su(su(from,su(qwq[now],siz[now])),n)*siz[now]%mod)%mod;
	sum = (sum+ans)%mod;
	//cout << now << ' ' << ans << endl;
}
signed main() {
	n = read();
	for(int i = 1; i <= n; i++) read();
	for(int i = 1; i < n; i++) {
		int u = read(),v = read();
		add(u,v),add(v,u);
	}
	dfs(1,0);
	dfss(1,0,qwq[1]);
	cout << (sum*inv2%mod);
	return 0;
}

详细

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

Test #1:

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

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

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

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

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

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

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: 93ms
memory: 59756kb

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: 238ms
memory: 28592kb

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: 365ms
memory: 28652kb

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: 448ms
memory: 28588kb

input:

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

output:

737983211

result:

ok 1 number(s): "737983211"