QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#709832#6623. Perfect MatchingshansueRE 4ms66656kbC++202.4kb2024-11-04 17:03:122024-11-04 17:03:17

Judging History

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

  • [2024-11-04 17:03:17]
  • 评测
  • 测评结果:RE
  • 用时:4ms
  • 内存:66656kb
  • [2024-11-04 17:03:12]
  • 提交

answer

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;

typedef long long LL;
const int N = 2000;
const LL mod = 998244353;

    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] = 0;
    for(int i = 0, v; i < g[u].size(); i++){
        v = g[u][i];
        if(v == fr)
            continue;

        // if(u == 1 && v == 2)
        //     printf("1->2\n");
        // if(u == 1 && v == 3)
        //     printf("1->3\n");

        dfs(v, u);
        sz[u] += sz[v];
    }
    sz[u]++;

    f[u][0][0] = 1;

    for(int vi = 0, v; vi < g[u].size(); vi++){
        v = g[u][vi];
        if(v == fr)
            continue;
        
        for(int i = sz[u] / 2; i >= 1; i--)
            for(int j = sz[v] / 2; j >= 0; j--){
                if(j > 0 && i - 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(){
    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);
    }

    memset(f, 0 ,sizeof f);
    dfs(1, 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: 3ms
memory: 66608kb

input:

2
1 2
1 3
3 4

output:

1

result:

ok 1 number(s): "1"

Test #2:

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

input:

3
1 2
2 3
3 4
4 5
5 6

output:

5

result:

ok 1 number(s): "5"

Test #3:

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

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:

223263378

result:

ok 1 number(s): "223263378"

Test #4:

score: 0
Accepted
time: 4ms
memory: 66524kb

input:

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

output:

225215244

result:

ok 1 number(s): "225215244"

Test #5:

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

input:

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

output:

210104685

result:

ok 1 number(s): "210104685"

Test #6:

score: 0
Accepted
time: 3ms
memory: 66608kb

input:

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

output:

211263144

result:

ok 1 number(s): "211263144"

Test #7:

score: 0
Accepted
time: 4ms
memory: 66656kb

input:

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

output:

226024809

result:

ok 1 number(s): "226024809"

Test #8:

score: -100
Runtime Error

input:

1977
2 1
3 1
4 1
5 4
6 4
7 1
8 3
9 5
10 2
11 3
12 2
13 3
14 2
15 9
16 9
17 2
18 17
19 5
20 16
21 2
22 2
23 15
24 16
25 22
26 14
27 6
28 4
29 24
30 25
31 28
32 15
33 27
34 32
35 24
36 10
37 18
38 15
39 33
40 3
41 27
42 3
43 35
44 15
45 11
46 19
47 21
48 4
49 28
50 6
51 3
52 14
53 14
54 14
55 25
56 18...

output:


result: