QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#668209#6623. Perfect MatchingslongyinWA 0ms3700kbC++201.7kb2024-10-23 12:30:262024-10-23 12:30:35

Judging History

This is the latest submission verdict.

  • [2024-10-23 12:30:35]
  • Judged
  • Verdict: WA
  • Time: 0ms
  • Memory: 3700kb
  • [2024-10-23 12:30:26]
  • Submitted

answer

#include <bits/stdc++.h>
#define int long long
using namespace std;

const int N = 4e3 + 5;
const int MOD = 998244353;

vector<int> edges[N];
int dp[N][N][2];
int cnt[N];

void dfs(int u, int fa) {
    cnt[u] = 1;
    dp[u][0][0] = 1;
    for (int v : edges[u]) {
        if (v == fa)
            continue;
        dfs(v, u);
        for (int j = cnt[u] / 2; j >= 0; j--) {
            for (int k = cnt[v] / 2; k >= 0; k--) {
                if (k) {
                    dp[u][j + k][0] = (dp[u][j + k][0] + (dp[v][k][0] + dp[v][k][1]) % MOD * dp[u][j][0] % MOD) % MOD;
                    dp[u][j + k][1] = (dp[u][j + k][1] + (dp[v][k][0] + dp[v][k][1]) % MOD * dp[u][j][1] % MOD) % MOD;
                }
                dp[u][j + k + 1][1] = (dp[u][j + k + 1][1] + dp[u][j][0] * dp[v][k][0] % MOD) % MOD;
            }
        }
        cnt[u] += cnt[v];
    }
}

int pw[N];
void init(int n = N - 1) {
    pw[0] = pw[1] = pw[2] = 1;
    for (int i = 3; i <= n; i++) {
        pw[i] = pw[i - 2] * (i - 1) % MOD;
    }
}

signed main() {
    ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);

    init();
    int n;
    cin >> n;
    for (int i = 1; i <= 2 * n - 1; i++) {
        int u, v;
        cin >> u >> v;
        edges[u].emplace_back(v);
    }

    dfs(1, 0);
    int ans = pw[2 * n];
    for (int i = 1; i <= n; i++) {
        //cout << dp[1][i][0] << " " << dp[1][i][1] << endl;
        if (i % 2 == 1)
            ans = ((ans - (dp[1][i][0] + dp[1][i][1]) % MOD * pw[2 * n - 2 * i] % MOD) % MOD + MOD) % MOD;
        else
            ans = ((ans + (dp[1][i][0] + dp[1][i][1]) % MOD * pw[2 * n - 2 * i] % MOD) % MOD + MOD) % MOD;
    }
    cout << ans << endl;

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3680kb

input:

2
1 2
1 3
3 4

output:

1

result:

ok 1 number(s): "1"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3700kb

input:

3
1 2
2 3
3 4
4 5
5 6

output:

5

result:

ok 1 number(s): "5"

Test #3:

score: -100
Wrong Answer
time: 0ms
memory: 3620kb

input:

10
2 1
3 2
4 2
5 3
6 3
7 5
8 4
9 3
10 5
11 3
12 9
13 11
14 8
15 5
16 1
17 4
18 1
19 11
20 19

output:

654729075

result:

wrong answer 1st numbers differ - expected: '223263378', found: '654729075'