QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#515275 | #5139. DFS Order 2 | Fluoresce | WA | 0ms | 7804kb | C++20 | 3.4kb | 2024-08-11 16:42:59 | 2024-08-11 16:43:00 |
Judging History
answer
#include<bits/stdc++.h>
#include<unordered_map>
typedef long long ll;
typedef long double ld;
#define debug(a) cout<<a<<'\n'
#define Pll pair<ll,ll>
#define PII pair<int,int>
#define ft first
#define sd second
#define vec vector
using namespace std;
const int N = 5e3 + 10, M = 1e7, mod = 998244353;
const ll inf = 1e18;
const ld eps = 1e-8;
int mov[4][2] = { {0,1},{1,0},{-1,0},{0,-1} }, dx, dy;
ll n, m, k;
ll dp[N][N],f[N][N],temp[N][N],inv[N],p[N],sum[N];
int h[N], e[N<<1], ne[N<<1], idx;
void link(int x, int y) {
e[++idx] = y;
ne[idx] = h[x];
h[x] = idx;
}
bool st[N];
int sz[N];
ll pdfs(int u) {
sz[u]=st[u] = 1;
ll res = 1;
int cnt = 0;
for (int i = h[u]; ~i; i = ne[i]) {
int v = e[i];
if (!st[v]) {
res = res * pdfs(v) % mod;
sz[u] += sz[v];
++cnt;
}
}
st[u] = 0;
res = res * p[cnt]%mod;
return res;
}
void exgcd(ll a, ll b, ll& x, ll& y) {
if (!b) {
x = 1;
y = 0;
return;
}
exgcd(b, a % b, y, x);
y -= a / b * x;
}
void sdfs(int u) {
st[u] = 1;
vec<int> son;
for (int i = h[u]; ~i; i = ne[i]) {
int v = e[i];
if (!st[v]) {
son.push_back(v);
}
}
if (!son.size())return;
for (int i = 1; i <= son.size(); ++i)for (int j = 1; j <= n; ++j)f[i][j] = 0;
f[0][0] = 1;
for (int i = 1; i <= son.size(); ++i) {
int v = sz[son[i - 1]];
for (int j = n; j >= v; --j) {
for (int k = son.size(); k >= 1; --k) {
f[k][j] = (f[k][j] + f[k - 1][j - v]) % mod;
}
}
}
for (auto v : son) {
temp[0][0] = 1;
int x = sz[v];
memset(sum, 0, sizeof sum);
for (int i = 1; i <= son.size(); ++i) {
for (int j = 1; j < n; ++j) {
if (j < x)temp[i][j] = f[i][j];
else temp[i][j] = (f[i][j] - temp[i - 1][j - x])%mod;
sum[j] = (sum[j] + temp[i][j] * p[i] % mod * p[son.size() - i - 1] % mod) % mod;
}
}
sum[0] = p[son.size() - 1] % mod;
for (int i = 1; i <= n; ++i) {
if (dp[u][i]) {
ll s = dp[u][i] * inv[son.size()] % mod;
for (int j = 0; j < n - i; ++j) {
dp[v][i + j + 1] = (dp[v][i + j + 1] + s * sum[j]) % mod;
}
}
}
sdfs(v);
}
st[u] = 0;
}
void solve() {
cin >> n;
memset(h, -1, sizeof h);
ll x, y, z;
for (int i = 1; i < n; ++i) {
cin >> x >> y;
link(x, y);
link(y, x);
}
p[0] = 1;
for (int i = 1; i <= n; ++i) {
p[i] = i * p[i - 1] % mod;
exgcd(p[i], mod, x, y);
inv[i] = (x % mod + mod) % mod;
}
dp[1][1] = pdfs(1);
sdfs(1);
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
cout << (dp[i][j] % mod + mod)%mod << ' ';
}
cout << '\n';
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifndef ONLINE_JUDGE
streambuf* cinbackup = cin.rdbuf(), * coubackup = cout.rdbuf();
ifstream fin("in.txt");
ofstream fout("out.txt");
cin.rdbuf(fin.rdbuf());
cout.rdbuf(fout.rdbuf());
#endif
int _ = 1, __ = 1;
//cin >> _;
while (_--) {
solve();
}
return 0;
}
详细
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 7804kb
input:
5 1 2 1 3 3 4 3 5
output:
4 0 0 0 0 0 2 2 0 0 0 2 2 0 0 0 0 1 2 1 0 0 1 2 1
result:
wrong answer 8th numbers differ - expected: '0', found: '2'