QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#511391#5139. DFS Order 2FluoresceWA 1ms5928kbC++203.0kb2024-08-09 20:23:092024-08-09 20:23:10

Judging History

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

  • [2024-08-09 20:23:10]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:5928kb
  • [2024-08-09 20:23:09]
  • 提交

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], temp[N][N],inv[N],p[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];

void dfs(int u) {
    st[u] = 1;
    p[u]=sz[u] = 1;
    int cnt = 1;
    for (int i = h[u]; ~i; i = ne[i]) {
        int v = e[i];
        if (!st[v]) {
            dfs(v);
            sz[u] += sz[v];
            p[u] = (p[u] * p[v]) % mod;
            p[u] = (p[u] * cnt) % mod;
            ++cnt;
        }
    }
    st[u] = 0;
}

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 dfs1(int u) {
    st[u] = 1;
    vec<int> son;
    for (int i = h[u]; ~i; i = ne[i]) {
        if (!st[e[i]]) {
            son.push_back(sz[e[i]]);
        }
    }
    if (son.size() == 0) {
        st[u] = 0;
        return;
    }
    ll iv = inv[son.size()];
    for (int i = 1; i <= n; ++i) {
        if (dp[u][i]) {
            ll x = dp[u][i] * iv;
            temp[u][i + 1] = (temp[u][i + 1] + x) % mod;
            for (auto s : son) {
                if (i + s + 1 <= n)temp[u][i + s + 1] = (temp[u][i + s + 1] + x) % mod;
            }
        }
    }
    for (int i = h[u]; ~i; i = ne[i]) {
        int v = e[i];
        if (!st[v]) {
            memcpy(dp[v], temp[u], sizeof(temp[u]));
            for (int j = 1; j <= n; ++j) {
                if (dp[u][j]) {
                    if (j + sz[v] + 1 <= n)dp[v][j + sz[v] + 1] = (dp[v][j + sz[v] + 1] - dp[u][j] * iv);
                }
            }
            dfs1(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);
    }
    for (int i = 1; i <= n; ++i) {
        exgcd(i, mod, x, y);
        inv[i] = (x % mod + mod) % mod;
    }
    dfs(1);
    dp[1][1] = p[1];
    dfs1(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;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: -100
Wrong Answer
time: 1ms
memory: 5928kb

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 4 4 4 4 4 0 
0 0 0 4 8 0 4 8 0 0 
0 0 0 0 4 8 0 4 8 0 
0 12 0 0 0 0 0 12 0 0 
0 12 0 0 12 0 0 0 0 0 
0 0 0 4 4 4 4 4 4 0 
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:

wrong answer 15th numbers differ - expected: '2', found: '4'