QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#239225 | #5139. DFS Order 2 | tselmegkh# | WA | 2ms | 3948kb | C++17 | 4.1kb | 2023-11-04 19:17:31 | 2023-11-04 19:17:33 |
Judging History
answer
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long;
template <class T>
T power(T a, long long b) {
T s = 1;
for (; b; a *= a, b >>= 1) if (b & 1) s *= a;
return s;
}
template <int mod>
struct modular {
using mint = modular;
int v;
modular() : v(0) {}
modular(long long x) {if ((v = x % mod) < 0) v += mod;}
mint operator-() const {return -v;}
mint inv() const {return power(*this, mod - 2);}
mint &operator+=(const mint &a) {if ((v += a.v) >= mod) v -= mod; return *this;}
mint &operator-=(const mint &a) {if ((v -= a.v) < 0) v += mod; return *this;}
mint &operator*=(const mint &a) {v = (int)((long long)v * a.v % mod); return *this;}
mint &operator/=(const mint &a) {return *this *= a.inv();}
friend bool operator==(const mint &a, const mint &b){return a.v == b.v;}
friend bool operator!=(const mint &a, const mint &b){return a.v != b.v;}
friend mint operator+(const mint &a, const mint &b) {return mint(a) += b;}
friend mint operator-(const mint &a, const mint &b) {return mint(a) -= b;}
friend mint operator*(const mint &a, const mint &b) {return mint(a) *= b;}
friend mint operator/(const mint &a, const mint &b) {return mint(a) /= b;}
friend istream &operator>>(istream &is, mint &a) {return is >> a.v;}
friend ostream &operator<<(ostream &os, const mint &a) {return os << a.v;}
};
const int mod = 998244353;
using mint = modular<mod>;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<mint> fac(n + 1);
fac[0] = 1;
for (int i = 1; i <= n; i++) {
fac[i] = fac[i - 1] * i;
}
vector C(n + 1, vector<mint>(n + 1));
for (int i = 0; i <= n; i++) {
C[i][0] = 1;
for (int j = 1; j <= i; j++) {
C[i][j] = C[i - 1][j] + C[i - 1][j - 1];
}
}
vector<vector<int>> adj(n);
for (int i = 0; i < n - 1; i++) {
int x, y;
cin >> x >> y;
x--, y--;
adj[x].push_back(y);
adj[y].push_back(x);
}
vector<mint> s(n, 1), q(n, 1);
vector<int> sz(n), ch(n);
auto calc = [&](auto calc, int x, int p) -> void {
sz[x] = 1;
for (int y : adj[x]) {
if (y == p) continue;
ch[x]++;
calc(calc, y, x);
s[x] *= s[y];
sz[x] += sz[y];
}
for (int y : adj[x]) {
for (int z : adj[x]) {
if (z == y || z == p) continue;
q[y] *= s[z];
}
}
s[x] *= fac[ch[x]];
};
calc(calc, 0, -1);
vector dp(n, vector<mint>(n));
mint t = 1;
auto dfs = [&](auto dfs, int x, int p) -> void {
for (int y : adj[x]) {
if (y == p) continue;
int m = adj[x].size();
vector a(ch[x] + 1, vector<mint>(sz[x]));
a[0][0] = 1;
for (int z : adj[x]) {
if (z == y || z == p) continue;
for (int i = ch[x] - 1; i >= 0; i--) {
for (int j = sz[z]; j < sz[x]; j++) {
a[i + 1][j] += a[i][j - sz[z]];
}
}
}
vector<mint> b(sz[x]);
for (int i = 0; i < ch[x]; i++) {
for (int j = 0; j < sz[x]; j++) {
b[j] += a[i][j] * fac[i] * fac[ch[x] - 1 - i];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
if (i - j - 1 >= sz[x]) continue;
dp[y][i] += dp[x][j] * b[i - j - 1];
}
}
t *= q[y];
dfs(dfs, y, x);
t /= q[y];
}
for (int i = 0; i < n; i++) {
dp[x][i] *= t * s[x];
}
};
dp[0][0] = 1;
dfs(dfs, 0, -1);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << dp[i][j] << " \n"[j == n - 1];
}
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3756kb
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: 0ms
memory: 3532kb
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: -100
Wrong Answer
time: 2ms
memory: 3948kb
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 924293471 0 924293471 924293471 0 924293471 0 0 0 924293471 0 9242...
result:
wrong answer 115th numbers differ - expected: '62914557', found: '924293471'