QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#710028#6623. Perfect MatchingshansueWA 0ms3864kbC++202.6kb2024-11-04 18:07:442024-11-04 18:07:46

Judging History

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

  • [2024-11-04 18:07:46]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3864kb
  • [2024-11-04 18:07:44]
  • 提交

answer

#pragma GCC optimize(2)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<bits/stdc++.h>
using namespace std;

typedef long long LL;
const int N = 4000;

const LL mod = 998244353;

int rt;

    int n;
    
    vector<int>g[N + 3];
    int sz[N + 3];
    LL f[N + 3][N + 3][2];

void dfs(int u, int fr){
    sz[u] = 1;
    f[u][0][0] = 1;

    if((int)g[u].size() == 1&&u!=rt)
        return ;
    int v = g[u][0];
    if(v == fr){
        swap(g[u][0], g[u][1]);
        v = g[u][0];
    }
    dfs(v, u);

    sz[u] += sz[v];
    for(int i = sz[u] / 2; i >= 1; i--){
        f[u][i][0] = (f[v][i][0] + f[v][i][1]) % mod;
        f[u][i][1] = f[v][i - 1][0];
    }

    for(int vi = 1; vi < g[u].size(); vi++){
        v = g[u][vi];
        if(v == fr)
            continue;

        dfs(v, u);

        sz[u] += sz[v];
        for(int i = sz[u] / 2; i >= 1; i--)
            for(int j = min(i, sz[v] / 2); j >= 0; j--){
                if(j > 0){
                    (f[u][i][0] += f[u][i - j][0] * (f[v][j][0] + f[v][j][1]) % mod) %= mod;
                    (f[u][i][1] += f[u][i - j][1] * (f[v][j][0] + f[v][j][1]) % mod) %= mod;
                }
                if(i - j >= 1)
                    (f[u][i][1] += f[u][i - j - 1][0] * f[v][j][0] % mod) %= mod;
            }

    }

}

    LL d[N + 3];

int main(){
  //  freopen("in.txt", "r", stdin);
 //   freopen("out.txt", "w", stdout);

    scanf("%d", &n);
    for(int i = 1, u, v; i <= 2 * n - 1; i++){
        scanf("%d%d", &u, &v);
        g[u].push_back(v);
        g[v].push_back(u);
    }
    srand(114514);
    rt=rand()%n+1;
    dfs(rt, 0);

    // printf("\n");
    // printf("[ ][ ]");
    // for(int i = 0; i <= 2 * n - 1; i++)
    //     printf("%4d", i);
    // printf("\n");
    // for(int u = 1; u <= 2 * n; u++){
    //     printf("[%d][0]", u);
    //     for(int i = 0; i <= 2 * n - 1; i++)
    //         printf("%4lld", f[u][i][0]);
    //     printf("\n");
    //     printf("[%d][1]", u);
    //     for(int i = 0; i <= 2 * n - 1; i++)
    //         printf("%4lld", f[u][i][1]);
    //     printf("\n");
    // }
    // printf("\n");

    d[0] = d[1] = 1;
    for(int i = 2; i <= n; i++)
        (d[i] = d[i - 1] * (2 * i - 1) % mod) %= mod;
    
    // for(int i = 1; i <= n; i++)
    //     printf("%lld\n", d[i]);
    // printf("\n");

    LL ans = 0;
    for(int i = 0; i <= n; i++)
        (ans += ((i & 1)?(-1):(1)) * ((f[1][i][0] + f[1][i][1]) * d[n - i] % mod) + mod) %= mod;
    
    printf("%lld", ans);

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

2
1 2
1 3
3 4

output:

1

result:

ok 1 number(s): "1"

Test #2:

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

input:

3
1 2
2 3
3 4
4 5
5 6

output:

15

result:

wrong answer 1st numbers differ - expected: '5', found: '15'