QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#679888#5139. DFS Order 2jieloscML 124ms283728kbC++202.8kb2024-10-26 19:05:502024-10-26 19:05:50

Judging History

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

  • [2024-10-26 19:05:50]
  • 评测
  • 测评结果:ML
  • 用时:124ms
  • 内存:283728kb
  • [2024-10-26 19:05:50]
  • 提交

answer

#include <bits/stdc++.h>
#define int int64_t

using std::cin;
using std::cout;
const char endl = '\n';

const int p = 998244353;
int fac[503] = {1};

std::vector<int> edge[502];
std::vector<int> son[502];
int sz[502];
int fa[502];
int subt[502];
void dfs(int u) {
  sz[u] = subt[u] = 1;
  for (auto &v : edge[u]) {
    if (v == fa[u]) continue;
    fa[v] = u;
    son[u].push_back(v);
    dfs(v);
    sz[u] += sz[v];
  }
  for (auto &v : son[u]) subt[u] = subt[u] * subt[v] % p;
  subt[u] = subt[u] * fac[son[u].size()] % p;
}

int Ans[502][502];

int n;

int ans[1000][1000];

void ddfs(int u) {
  int bp[1000][1000];
  // std::vector<std::vector<int>> bp(1000, std::vector<int>(1000, 0));
  // memset(bp, 0, sizeof(bp));
  for (int i = 0; i < 1000; ++i)
    for (int j = 0; j < 1000; ++j) bp[i][j] = 0;
  bp[0][0] = 1;
  for (auto &v : son[u]) {
    for (int j = 500; j >= sz[v]; --j) {
      for (int k = 500; k; --k)
        bp[j][k] = (bp[j][k] + bp[j - sz[v]][k - 1] % p) % p;
    }
  }
  // for (int i = 0; i <= 500; i++) {
  //   for (int j = 0; j <= 500; j++)
  //     if (bp[i][j]) std::cerr << bp[i][j] << " \n"[j == 500];
  // }
  for (auto &v : son[u]) {
    for (int i = 0; i < 502; ++i)
      for (int j = 0; j < 502; ++j) ans[i][j] = bp[i][j];
    // auto ans = bp;
    for (int j = 0; j <= sz[u]; ++j) {
      for (int k = 0; k <= (int)son[u].size(); ++k) {
        if (j >= sz[v] and k >= 1)
          ans[j][k] = ((ans[j][k] - ans[j - sz[v]][k - 1]) % p + p) % p;
      }
    }
    int base = 1;
    for (auto vv : son[u])
      if (vv != v) base = base * subt[vv] % p;
    std::vector<int> anssj(502);
    for (int j = 0; j + sz[v] <= sz[u]; ++j) {
      for (int k = 0; k < (int)son[u].size(); ++k) {
        anssj[j] =
          (anssj[j] + ans[j][k] * fac[k] % p * fac[son[u].size() - k - 1] % p) %
          p;
      }
      anssj[j] = anssj[j] * base % p;
    }
    for (int j = 0; j < n; ++j) {
      for (int l = 0; l <= j; ++l) {
        Ans[v][j + 1] = (Ans[v][j + 1] + Ans[u][l] * anssj[j - l] % p) % p;
      }
    }
    ddfs(v);
  }
}

void solve() {
  for (int i = 1; i <= 500; ++i) fac[i] = fac[i - 1] * i % p;
  cin >> n;
  for (int i = 1, u, v; i < n; ++i) {
    cin >> u >> v;
    edge[u].push_back(v);
    edge[v].push_back(u);
  }
  dfs(1);
  Ans[1][1] = 1;
  ddfs(1);
  for (int u = 1; u <= n; ++u) {
    for (int j = 1; j <= n; ++j)
      cout << Ans[u][j] * subt[u] % p << " \n"[j == n];
  }
}

int32_t main() {
#ifndef _DEBUG
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
#endif
  int tc = 1;
  // std::cin >> tc;
  while (tc--) solve();
#ifdef _DEBUG
  std::cout << std::endl;
  std::cout << "Time used: " << clock() << "ms" << std::endl;
  system("pause");
  return 0;
#endif
}

詳細信息

Test #1:

score: 100
Accepted
time: 5ms
memory: 33916kb

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: 11ms
memory: 50776kb

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: 124ms
memory: 283728kb

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: -100
Memory Limit Exceeded

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:


result: