QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#667600#5139. DFS Order 2HHAZWA 301ms7312kbC++203.9kb2024-10-23 00:34:142024-10-23 00:34:14

Judging History

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

  • [2024-10-23 00:34:14]
  • 评测
  • 测评结果:WA
  • 用时:301ms
  • 内存:7312kb
  • [2024-10-23 00:34:14]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int>pii;
typedef pair<ll,ll>pll;
typedef pair<ld,ld>pdd;
typedef pair<ll, pair<ll, ll> > plpair;
typedef vector<int> vi;
typedef vector<ll> vl;
#define endl '\n'
#define rep(i, a, b) for (ll i = (a); i <= (b); ++i)
#define per(i, a, b) for(ll i = (a); i >= (b); --i)
#define debug(x) cout<<#x<<": "<<x<<endl
#define lowbit(id) id & (-id)
#define fi first
#define sec second
#define lc id<<1
#define rc id<<1|1
#define sz(x) (int)(x).size()
#define all(t) (t).begin(), (t).end()
#define meh {cout<<"NO"<<endl;return;}
#define yay {cout<<"YES"<<endl;return;}
#define vin(v) for(auto&x:v)cin>>x;
#define print(v) for (auto x: v)cout<<x<<' ';cout<<endl;
#define sqrt(x) sqrtl(x)
mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count());
const ll N = 505;
const int mod = 998244353;

ll n;
ll dp[N][N], ans[N][N];
ll f[N], val[N], siz[N], son[N], g[N];
vector<ll>ve[N];

ll qsm(ll n,ll b, ll Mod = 998244353){
	n = n % Mod;
	ll s = 1,a = n;
	while(b){
		a = a % Mod;
		if(b&1)
			s = s * a;
		b >>= 1;
		a = a * a;
		s = s % Mod;
	}
	return s;
}

void init_dfs(ll u, ll fa) {
	siz[u]++;
	ll j = 1;
	for(auto v : ve[u]) {
		if(v != fa) {
			init_dfs(v, u);
			siz[u] += siz[v];
			son[u]++;
			j *= val[v];
			j %= mod;
		}
	}
	val[u] = j * f[son[u]] % mod;
}

void dfs(ll u, ll fa) {
	rep(i, 0, son[u]) {
		rep(j, 0, siz[u]) {
			dp[i][j] = 0;
		}
	}
	dp[0][0] = 1;
	ll d = 1;
	for(auto v : ve[u]) {
		if(v != fa) {
			d *= val[v];
			per(i, son[u], 1) {
				per(j, siz[u], siz[v]) {
					dp[i][j] += dp[i - 1][j - siz[v]];
					dp[i][j] %= mod;
				}
			}
		}
	}
	for(auto v : ve[u]) {
		if(v != fa) {
			rep(i, 0, 500) {
				g[i] = 0;
			}
			rep(i, 1, son[u]) {
				rep(j, siz[v], siz[u]) {
					dp[i][j] -= dp[i - 1][j - siz[v]];
					dp[i][j] = (dp[i][j] + mod) % mod;
					// if(v==3 && dp[i][j]) cout << i << ' ' << j << ' ' << dp[i][j] << endl;
				}
			}
			rep(i, 0, son[u] - 1) {
				rep(j, 0, siz[u]) {
					g[j] += ((((dp[i][j] * f[i]) % mod) * f[son[u] - 1 - i]) % mod) * d % mod;
					g[j] % mod;
					// if(v==3 && dp[i][j]) cout << i << ' ' << j << ' ' << ((((dp[i][j] * f[i]) % mod) * f[son[u] - 1 - i]) % mod) * d % mod << endl;
				}
			}
			// if(v==3)
			// rep(j, 0, son[u]) {
			// 	debug(g[j]);
			// }
			rep(i, 1, n) {
				ll di = 0;
				rep(j, i + 1, n) {
					if(ans[u][i]) {
						di += g[j - i - 1];
						di %= mod;
					}
				}
				if(di) {
					di = ans[u][i] * qsm(di, mod - 2) % mod;
				}
				rep(j, i + 1, n) {
					if(ans[u][i]) {
						ans[v][j] += (g[j - i - 1] * di) % mod;
						ans[v][j] %= mod;
					}
				}
			}
			per(i, son[u], 1) {
				per(j, siz[u], siz[v]) {
					dp[i][j] += dp[i - 1][j - siz[v]];
					dp[i][j] %= mod;
				}
			}
		}
	}
	for(auto v : ve[u]) {
		if(v != fa) {
			dfs(v, u);
		}
	}
}

void solve() {
	cin >> n;
	f[0] = 1;
	rep(i, 1, n) {
		f[i] = f[i - 1] * i % mod;
	}
	rep(i, 2, n) {
		ll u, v;
		cin >> u >> v;
		ve[u].push_back(v);
		ve[v].push_back(u);
	}
	init_dfs(1, 0);
	ans[1][1] = val[1];
	dfs(1, 0);
	rep(i, 1, n) {
		rep(j, 1, n) {
			cout << ans[i][j] << " \n"[j == n];
		}
	}
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int T = 1;
	// cin >> T;
	//	cout.flush();
	//		ofstream outfile("C:\\Users\\86187\\OneDrive\\桌面\\1.txt");
	//		outfile << "a[1]=0;a[2]=1;";
	//		ll l = 1e7 + 1;
	//		rep(i, 3, 1e9) {
	//			printf("%lld\n", i);
	//			ll x = (i - 1) * ((a[i - 1] + a[i - 2]) % mod);
	//			x %= mod;
	//			if(i == l) {
	//				outfile << "a[" << i << "]=" << x << ";";
	//			}
	//			if(i == l + 1) {
	//				outfile << "a[" << i << "]=" << x << ";";
	//				l += 1e7;
	//			}
	//		}
	while(T--) {
		solve();
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

5
1 2
1 3
3 4
3 5

output:

4 0 0 0 0
0 2 0 0 2
0 2 2 0 0
0 0 1 2 1
0 0 1 2 1

result:

ok 25 numbers

Test #2:

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

input:

10
9 2
9 6
10 5
1 5
1 6
9 3
5 8
4 3
7 9

output:

24 0 0 0 0 0 0 0 0 0
0 0 0 4 2 2 8 2 2 4
0 0 0 4 4 4 4 4 4 0
0 0 0 0 4 4 4 4 4 4
0 12 0 0 0 0 0 12 0 0
0 12 0 0 12 0 0 0 0 0
0 0 0 4 2 2 8 2 2 4
0 0 6 6 0 0 0 0 6 6
0 0 12 0 0 12 0 0 0 0
0 0 6 6 0 0 0 0 6 6

result:

ok 100 numbers

Test #3:

score: 0
Accepted
time: 3ms
memory: 5996kb

input:

100
18 100
91 87
28 83
11 98
51 52
24 91
72 53
18 19
89 16
77 35
26 25
73 16
96 70
56 44
69 10
63 30
54 95
39 66
58 98
8 71
58 65
74 73
2 64
12 19
32 81
31 54
43 41
84 59
55 75
72 81
59 37
10 94
93 2
64 47
13 32
36 84
28 22
30 28
25 77
47 6
80 52
54 17
23 40
47 88
49 53
65 27
99 59
25 70
91 9
74 1
7...

output:

8388559 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 62914557 0 62914557 62914557 0 62914557 0 0 0 62914557 0 62914557 ...

result:

ok 10000 numbers

Test #4:

score: 0
Accepted
time: 194ms
memory: 7312kb

input:

500
382 156
418 376
91 15
142 274
449 174
375 82
118 175
421 458
361 222
14 474
11 324
368 341
227 424
231 249
81 435
250 271
118 38
147 61
124 408
135 1
244 316
301 80
39 313
90 118
290 465
465 250
277 341
8 105
319 373
305 379
309 200
180 398
47 489
463 259
173 492
494 343
251 193
111 32
401 270
4...

output:

219078761 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...

result:

ok 250000 numbers

Test #5:

score: -100
Wrong Answer
time: 301ms
memory: 6808kb

input:

500
164 415
76 333
437 167
184 28
281 491
40 64
184 6
407 459
141 469
370 186
226 142
165 243
26 175
442 345
496 451
351 277
467 136
42 10
14 435
109 202
22 267
354 312
232 273
141 158
219 356
329 405
212 65
345 166
378 79
114 224
213 79
371 23
454 276
150 9
82 291
24 111
157 396
22 475
268 163
57 8...

output:

744716203 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...

result:

wrong answer 508th numbers differ - expected: '814099510', found: '324777382'