QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#726911#4889. 愚蠢的在线法官NineSuns0 1ms3780kbC++142.3kb2024-11-09 10:17:242024-11-09 10:17:25

Judging History

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

  • [2024-11-09 10:17:25]
  • 评测
  • 测评结果:0
  • 用时:1ms
  • 内存:3780kb
  • [2024-11-09 10:17:24]
  • 提交

answer

#include <bits/stdc++.h>
#define ll long long
#define pii pair <int, int>
#define fi first
#define se second
#define pb push_back

using namespace std;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
const int N = 3005, mod = 998244353; 
int n, v[N], vis[N], dfn[N], sz[N], a[N], dt, ld[N], rd[N], k; 
vector <int> e[N]; 
ll qpow (ll x, ll y = mod-2) {
	ll res = 1;
	while (y) {
		if (y&1) res = res*x%mod;
		x = x*x%mod; y >>= 1;
	}
	return res; 
}
void dfs (int p, int fa) {
	sz[p] = vis[p]; 
	for (int i : e[p]) {
		if (i == fa) continue; 
		dfs(i, p); sz[p] += sz[i]; 
	}
	sort(e[p].begin(), e[p].end(), [&](int x, int y) { return sz[x] < sz[y]; });
}
void tdfs (int p, int fa) {
	if (vis[p]) dfn[p] = ++dt; 
	ld[p] = dt;
	for (int i : e[p]) {	
		if (i == fa) continue; 
		tdfs(i, p); 
	} 
	rd[p] = dt; 
}
ll ans = 1; 
ll sk[N][N], f[N], g[N]; 
ll td[N]; 
void dfs2 (int p, int fa, int k) {
	if (vis[p]) {
		g[dfn[p]] = (v[dfn[p]]+k)%mod;
		ans = ans*(v[p]+k)%mod; 
		for (int j = dfn[p]+1;j <= dfn[p]+sz[p]-1;j++) sk[p][j] -= sk[p][dfn[p]]; 
		for (int j : e[p]) {
			if (j == fa) continue; 
			for (int i = ld[j];i <= rd[j];i++) sk[j][i] = sk[p][i]; 
			dfs2(j, p, (-v[p])%mod);  
			for (int i = ld[j];i <= rd[j];i++) sk[p][i] = sk[j][i]; 
		}
		return; 
	}
	ll s = 0; 
	for (int i : e[p]) {
		if (i == fa || !sz[i]) continue; 
		for (int j = ld[i];j <= rd[i];j++) (sk[i][j] = sk[p][j]-s) %= mod;
		dfs2(i, p, k); 
		for (int j = ld[i];j <= rd[i];j++) sk[p][j] = sk[i][j]; 
		
		for (int j = ld[i];j <= rd[i];j++) td[j] = v[p]+k; 
		for (int j = ld[i];j <= rd[i];j++) {
			ll sv = qpow(g[j])*td[j]%mod; 
			(k += sv*v[p]%mod*sk[i][j])%mod; (s += sv*sk[i][j]) %= mod; 
			for (int z = j;z <= rd[i];z++) (td[z] -= sv*g[j]) %= mod; 
		}
	}
	
}

void solve () {
	cin >> n >> k; 
	for (int i = 1;i <= n;i++) cin >> v[i]; 
	for (int i = 1;i <= k;i++) {
		cin >> a[i]; 
		if (vis[a[i]]) {
			cout << "0"; return; 
		}
		vis[a[i]] = 1; sk[1][i] = 1; 
	}
	for (int i = 1;i < n;i++) {
		int x, y; cin >> x >> y; 
		e[x].pb(y); e[y].pb(x); 
	}
	dfs(1, 0); tdfs(1, 0); 
	dfs2(1, 0, 0);  
	if (ans < 0) ans += mod; 
	cout << ans;
}

signed main () {
//	ios::sync_with_stdio(0);
//	cin.tie(0); cout.tie(0);
	int T = 1;
	while (T--) solve();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Runtime Error

Test #1:

score: 0
Runtime Error

input:

499999 500000
879485015 176694934 629415436 677018935 33186863 696674214 19586946 878479076 318116264 823399347 140314195 715329843 996129441 446979068 600062488 847953138 978347569 865596472 147980317 199880680 187953368 989585254 457868128 466175307 381871948 369138343 826894839 963935318 36550896...

output:


result:


Subtask #2:

score: 0
Wrong Answer

Test #2:

score: 6
Accepted
time: 1ms
memory: 3696kb

input:

10 1
663730929 273617752 74933376 562874267 346105266 779139305 198742356 291012786 227170675 127136999
2
10 8
5 10
1 5
9 8
6 10
4 6
3 1
2 4
7 3

output:

273617752

result:

ok 1 number(s): "273617752"

Test #3:

score: 6
Accepted
time: 0ms
memory: 3780kb

input:

10 10
144077216 482507381 588297929 801675226 21569141 816295251 425507414 150613951 822735519 802838587
7 10 9 2 1 6 8 3 5 4
10 9
6 10
5 6
2 5
8 5
3 5
1 10
4 2
7 1

output:

816324383

result:

ok 1 number(s): "816324383"

Test #4:

score: 0
Wrong Answer
time: 0ms
memory: 3700kb

input:

10 2
136932305 774891472 782708047 361400653 241613404 206577781 241535900 917672952 105332067 165467540
2 5
2 4
5 4
1 4
7 2
3 5
10 5
8 3
6 10
9 10

output:

862864724

result:

wrong answer 1st numbers differ - expected: '830180673', found: '862864724'

Subtask #3:

score: 0
Runtime Error

Test #43:

score: 0
Runtime Error

input:

500000 600
375999961 486674339 753591626 263678997 153496902 843204506 294273913 59353025 80121537 938426018 309354784 359915003 480316315 880954496 544396164 478808641 583197144 202111021 277512785 193266475 511298159 750302398 30978705 278891583 701736665 516664158 47658598 456194527 517690925 870...

output:


result:


Subtask #4:

score: 0
Skipped

Dependency #2:

0%

Subtask #5:

score: 0
Runtime Error

Test #77:

score: 0
Runtime Error

input:

500000 500000
200910665 704700912 664276 824905098 512233060 623259142 478040808 509760810 756074623 387351466 261683363 140331101 135736712 184881987 425557684 61914673 951508934 787260914 386285199 40458274 175322609 429002885 606957721 742057849 342942076 104844271 656874266 826513447 76400873 55...

output:


result:


Subtask #6:

score: 0
Skipped

Dependency #1:

0%