QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#424965#7997. 树 V 图RedreamMerAC ✓622ms66248kbC++172.2kb2024-05-29 20:15:552024-05-29 20:15:55

Judging History

你现在查看的是最新测评结果

  • [2024-05-29 20:15:55]
  • 评测
  • 测评结果:AC
  • 用时:622ms
  • 内存:66248kb
  • [2024-05-29 20:15:55]
  • 提交

answer

// #pragma GCC optimize("O3")
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#include <bits/stdc++.h>
using namespace std;

#define PB emplace_back
#define int long long
#define ll long long
#define vi vector<int>
#define siz(a) ((int) ((a).size()))
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define per(i, a, b) for (int i = (a); i >= (b); --i)
void print(vi n) { rep(i, 0, siz(n) - 1) cerr << n[i] << " \n"[i == siz(n) - 1]; }

const int N = 3e3, mod = 998244353;
int T, a, b, f[N + 5][N + 5], dep[N + 5], ans, s[N + 5], c[N + 5], rt;
vi st[N + 5], sq[N + 5], bel[N + 5];

int Dfs(int n, int fa, int x, int y) {
	int res = 1;
	for(int v : st[n]) if(v != fa) {
		if(s[v] == x) {
			dep[v] = dep[n] + 1;
			(res *= Dfs(v, n, x, y)) %= mod;
		}
		else if(s[v] != y) {
			// if(s[v] == 2) cout << n << ' ' << dep[n] << endl;
			if(x < s[v]) (res *= (dep[n] ? f[s[v]][dep[n] - 1] : 0) + f[s[v]][dep[n]]) %= mod;
			else (res *= f[s[v]][dep[n]] + f[s[v]][dep[n] + 1]) %= mod;
		}
		else rt = n;
	}
	return res;
}

void dfs(int n, int fa) {
	for(int v : sq[n]) if(v != fa) dfs(v, n);
	for(int x : bel[n]) {
		dep[x] = 0;
		int res = Dfs(x, 0, n, fa);
		if(n != 1) (f[n][dep[rt]] += res) %= mod;
		else (ans += res) %= mod;
	}
	// per(i, a, 0) (f[n][i] += f[n][i + 1]) %= mod;
}

signed main() {
	// freopen(".in", "r", stdin);
	// freopen(".out", "w", stdout);
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	for(cin >> T; T--; ) {
		ans = 0;
		cin >> a >> b;
		rep(i, 1, a) st[i].clear();
		rep(i, 1, b) sq[i].clear(), bel[i].clear(), c[i] = 0;
		rep(i, 1, b) rep(j, 0, a) f[i][j] = 0;
		int x, y;
		rep(i, 1, a - 1) {
			cin >> x >> y;
			st[x].PB(y), st[y].PB(x);
		}
		rep(i, 1, a) cin >> s[i], ++c[s[i]], bel[s[i]].PB(i);
		rep(i, 1, a) for(int v : st[i]) if(v > i && s[v] == s[i]) --c[s[i]];
		bool is = 1;
		rep(i, 1, b) is &= c[i] == 1;
		if(!is) {
			cout << "0\n";
			continue;
		}
		rep(i, 1, a) for(int v : st[i]) if(s[v] != s[i]) sq[s[i]].PB(s[v]);
		dfs(1, 0);
		// cout << f[2][0] << endl;
		cout << ans << '\n';
	}
	return cerr << endl << 1.0 * clock() / CLOCKS_PER_SEC << endl, 0;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 4240kb

input:

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

output:

11
15
8
9
5
2
1
0
15
18

result:

ok 10 numbers

Test #2:

score: 0
Accepted
time: 1ms
memory: 4236kb

input:

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

output:

21
4
5
2
13
17
6
0
0
0

result:

ok 10 numbers

Test #3:

score: 0
Accepted
time: 181ms
memory: 29268kb

input:

10
3000 2
2052 2631
688 2422
2401 352
654 1669
1566 2157
1187 334
179 178
948 1821
1084 99
878 793
410 336
2218 865
2558 236
2808 430
799 1238
1468 226
312 268
1860 991
2946 96
2540 241
2242 1103
299 1527
788 2026
1885 2104
229 1627
2461 2649
498 2642
1354 98
113 980
947 2790
1281 2232
2682 713
1841...

output:

2420
21248
731916691
864589674
3936
9543168
561554568
0
0
0

result:

ok 10 numbers

Test #4:

score: 0
Accepted
time: 201ms
memory: 66248kb

input:

10
3000 2
1962 622
2353 451
2284 659
848 1392
2032 111
2647 1540
706 1072
2158 461
887 1075
178 481
2350 2658
2309 96
753 1572
1703 2171
365 1286
2540 2487
2687 194
1585 1241
2147 1247
1720 701
507 2127
2207 812
1568 2621
725 1342
1385 245
2819 2731
2357 450
467 672
2549 1243
1517 702
1808 2415
76 8...

output:

2252
21504
217815511
420767421
3008
36864
187077271
0
0
0

result:

ok 10 numbers

Test #5:

score: 0
Accepted
time: 622ms
memory: 4596kb

input:

10
3000 2
774 1607
49 867
1384 2838
247 454
1369 2077
2145 1433
1057 1845
1940 2195
1869 967
2830 2829
739 2783
1474 1047
1688 2054
2958 1044
1041 135
768 1621
667 257
1309 1336
2456 2605
2402 1425
1572 1275
2327 1438
1111 1265
607 2583
2557 2788
2694 1378
2332 1848
107 1749
1804 236
2585 309
2761 4...

output:

383799
552301
9347
280893
298918
39575
180102
18385
0
78

result:

ok 10 numbers

Test #6:

score: 0
Accepted
time: 535ms
memory: 4372kb

input:

10
3000 2
630 377
2445 1749
374 616
1421 937
429 724
2962 2139
1501 2291
2916 2433
2404 2502
755 1952
2756 692
547 420
507 2438
1082 2413
2929 1624
2556 2310
2895 266
2697 2184
1597 502
524 2083
898 1989
190 1269
2189 2104
1751 1387
2986 2041
646 493
627 1899
2516 2434
456 2016
223 1222
2329 2784
94...

output:

7096
400826
74841
21895
331464
110972
215781
220870
0
57

result:

ok 10 numbers

Test #7:

score: 0
Accepted
time: 318ms
memory: 66088kb

input:

10
3000 2
1490 1252
1922 2810
462 631
2806 1842
1552 1043
493 2043
339 2377
1271 354
1043 1795
192 1242
1758 2799
433 452
2473 2444
292 1404
1171 770
1194 2617
2457 2502
124 2059
818 2883
81 835
300 1498
1265 601
2038 1830
1163 1973
1110 1369
1428 392
2061 1037
559 1133
1296 140
2216 565
1001 2432
7...

output:

3074
962904591
573541113
831594950
4847397
673682640
122362172
0
0
0

result:

ok 10 numbers

Test #8:

score: 0
Accepted
time: 208ms
memory: 29204kb

input:

10
3000 2
459 801
2023 2946
2356 57
2998 264
1498 2840
1755 1603
692 1624
1929 1474
1376 1542
2953 2021
934 2740
1747 1918
270 2928
925 2366
886 2966
1692 2224
1528 1835
1748 2417
76 2539
956 1834
2570 2423
418 146
559 1376
1278 2981
564 555
2200 1472
1006 749
1262 920
2410 429
2149 1023
1365 2864
2...

output:

101017
297761161
615903767
469063443
5029034
178745072
0
0
0
0

result:

ok 10 numbers

Test #9:

score: 0
Accepted
time: 237ms
memory: 29808kb

input:

10
3000 2
1130 2866
2675 1317
1817 1398
1916 550
777 1510
955 1371
2842 2940
1069 86
2086 1600
2635 2465
940 2644
954 2270
2517 895
2981 110
2477 993
2854 2889
247 1409
1008 2137
2436 2896
2588 1174
2774 1946
265 622
1903 100
1052 2594
1864 2791
453 1319
11 2630
2468 1464
952 325
632 2214
1218 2459
...

output:

32539
153019389
251875443
633091978
10314829
918558837
687532775
0
0
0

result:

ok 10 numbers

Test #10:

score: 0
Accepted
time: 246ms
memory: 62008kb

input:

10
3000 2
758 2015
2186 297
1518 1181
2657 1064
687 2929
1271 265
646 408
2332 217
2486 1313
362 1555
2701 1097
2014 2885
1691 43
2804 1610
1866 119
516 740
343 2335
1608 1278
330 1421
1967 2298
94 2157
1392 458
64 216
1090 541
1670 459
1870 750
1780 1973
2276 1910
1744 1699
2602 2901
2484 1380
1422...

output:

159905
198653564
859658661
608555088
9674254
170770457
138273469
0
0
0

result:

ok 10 numbers