QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#65425#5139. DFS Order 2magicduck#RE 3ms5632kbC++142.7kb2022-11-30 16:21:492022-11-30 16:21:51

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-11-30 16:21:51]
  • 评测
  • 测评结果:RE
  • 用时:3ms
  • 内存:5632kb
  • [2022-11-30 16:21:49]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
template <typename T> inline void read(T &F) {
    int R = 1; F = 0; char CH = getchar();
    for(; !isdigit(CH); CH = getchar()) if(CH == '-') R = -1;
    for(; isdigit(CH); CH = getchar()) F = F * 10 + CH - 48;
    F *= R;
}
inline void file(string str) {
    freopen((str + ".in").c_str(), "r", stdin);
    freopen((str + ".out").c_str(), "w", stdout);
}
const LL mod = 998244353;
const int N = 5e2 + 10;
int n, siz[N], t[N]; LL f[N][N], g[N], res[N][N]; vector<int> edge[N];
void dfs(int x, int fa) {
    siz[x] = g[x] = 1; int s = 0; t[x] = fa;
    for(int i : edge[x]) {
        if(fa == i) continue;
        dfs(i, x); siz[x] += siz[i];
        g[x] = g[x] * g[i] % mod;
        s++; g[x] = g[x] * s % mod;
    }
}
LL d[N], sumx[N], sumy[N];
void dp(const vector<int> &s) {
    for(int i = 1; i <= n; i++)
        d[i] = sumx[i] = sumy[i] = 0;
    d[1] = sumx[1] = sumy[1] = 1;
    for(int i : s) {
        //cout << i << " ";
        for(int j = n; j >= 1; j--) {
            if(i + j <= n) {
                (sumy[i + j] += sumy[j] * sumx[j]) %= mod;
                (d[i + j] += d[j] * sumx[j]) %= mod;
                (sumx[i + j] += d[j] * sumx[j] + sumx[j]) %= mod;
            }
            d[j] = d[j] * sumy[j] % mod;
            sumx[j] = sumx[j] * sumy[j] % mod;
            (sumy[j] += d[j]) %= mod;
        }
    }
    //puts("!!!");
    //cerr << d[1] << " " << d[2] << " " << d[3] << " " << d[4] << endl;
}
void solve(int x, int fa) {
    if(fa) {
        LL r = 1; vector<int> s;
        for(int i : edge[fa]) {
            if(i != t[fa] && x != i)
                s.emplace_back(siz[i]), r = r * g[i] % mod;
        }
        dp(s);
        for(int i = 1; i <= n; i++) {
            LL res = 0;
            for(int j = 1; j < i; j++)
                res += f[fa][j] * d[i - j] % mod;
            res = res % mod * r % mod;
            f[x][i] = res;
        }
    }
    for(int i = 1; i <= n; i++)
        res[x][i] = f[x][i] * g[x] % mod;
    for(int i : edge[x]) {
        if(i == fa) continue;
        solve(i, x);
    }
}

int main() {
    //file("test");
    read(n);
    for(int i = 1; i < n; i++) {
        int x, y; read(x), read(y);
        edge[x].emplace_back(y);
        edge[y].emplace_back(x);
    }    
    dfs(1, 0);
    f[1][1] = 1; solve(1, 0);
    LL ans = 0;
    for(int i = 1; i <= n; i++, puts(""))
        for(int j = 1; j <= n; j++)
            cout << res[i][j] << " ", ans += res[i][j];
    ans %= mod;
    assert(ans == g[1] * n % mod);
    #ifdef _MagicDuck
        fprintf(stderr, "# Time: %.3lf s", (double)clock() / CLOCKS_PER_SEC);
    #endif
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 5444kb

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: 5632kb

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
Dangerous Syscalls

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: