提出 #50056781


ソースコード 拡げる

#include <bits/stdc++.h>

using namespace std;

#define SZ(x) ((int)((x).size()))
#define lb(x) ((x) & (-(x)))
#define bp(x) __builtin_popcount(x)
#define bpll(x) __builtin_popcountll(x) 
#define mkp make_pair
#define pb push_back
#define fi first
#define se second
typedef long long ll;
typedef pair<int, int> pii;

const int MAXN = 3e3 + 10;
const int Mod = 998244353;

void add_mod(int &x, int y) {
    x += y;
    if (x >= Mod) {
        x -= Mod;
    }
}

int n;
vector<int> G[MAXN];
int fac[MAXN];
int f[MAXN][MAXN], tmp[MAXN], g[MAXN][2];

void dfs(int u, int fa) {
    f[u][0] = 1;
    for (auto v : G[u]) {
        if (v == fa) {
            continue;
        }
        dfs(v, u);
        memcpy(tmp, f[u], sizeof(tmp));
        memset(f[u], 0, sizeof(f[u]));
        for (int i = n - 1; i >= 1; i--) {
            add_mod(f[u][i], 1ll * tmp[i - 1] * g[v][1] % Mod);
        }
        for (int i = n - 1; i >= 0; i--) {
            add_mod(f[u][i], 1ll * tmp[i] * g[v][0] % Mod);
        }
    }

    for (int i = 0; i < n; i++) {
        add_mod(g[u][0], 1ll * f[u][i] * fac[i] % Mod);
        add_mod(g[u][1], 1ll * f[u][i] * fac[i + 1] % Mod);
    }
}

void solve() {
	cin >> n;
    for (int i = 1; i <= n - 1; i++) {
        int u, v;
        cin >> u >> v;
        G[u].push_back(v);
        G[v].push_back(u);
    }

    fac[0] = 1;
    for (int i = 1; i <= n; i++) {
        fac[i] = 1ll * fac[i - 1] * i % Mod;
    }
    dfs(1, 0);
    int ans = g[1][0];
    cout << ans << '\n';
}

int main() {
	ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
	int T = 1;
	while (T--) solve();
	return 0;
}

提出情報

提出日時
問題 C - Swap on Tree
ユーザ TLE_Automat
言語 C++ 20 (gcc 12.2)
得点 600
コード長 1697 Byte
結果 AC
実行時間 72 ms
メモリ 39292 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 600 / 600
結果
AC × 3
AC × 29
セット名 テストケース
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 02_path_00.txt, 02_path_01.txt, 03_star_00.txt, 03_star_01.txt, 03_star_02.txt, 03_star_03.txt
ケース名 結果 実行時間 メモリ
00_sample_00.txt AC 1 ms 3528 KiB
00_sample_01.txt AC 1 ms 3528 KiB
00_sample_02.txt AC 1 ms 3600 KiB
01_random_00.txt AC 56 ms 27556 KiB
01_random_01.txt AC 68 ms 30636 KiB
01_random_02.txt AC 22 ms 16932 KiB
01_random_03.txt AC 68 ms 30460 KiB
01_random_04.txt AC 4 ms 7196 KiB
01_random_05.txt AC 68 ms 30440 KiB
01_random_06.txt AC 68 ms 30404 KiB
01_random_07.txt AC 68 ms 30140 KiB
01_random_08.txt AC 6 ms 8908 KiB
01_random_09.txt AC 67 ms 30524 KiB
01_random_10.txt AC 28 ms 19540 KiB
01_random_11.txt AC 68 ms 30208 KiB
01_random_12.txt AC 1 ms 3900 KiB
01_random_13.txt AC 69 ms 30716 KiB
01_random_14.txt AC 10 ms 11784 KiB
01_random_15.txt AC 68 ms 30148 KiB
01_random_16.txt AC 27 ms 19284 KiB
01_random_17.txt AC 67 ms 30272 KiB
01_random_18.txt AC 26 ms 18496 KiB
01_random_19.txt AC 68 ms 30436 KiB
02_path_00.txt AC 54 ms 33280 KiB
02_path_01.txt AC 72 ms 39292 KiB
03_star_00.txt AC 3 ms 5396 KiB
03_star_01.txt AC 61 ms 15724 KiB
03_star_02.txt AC 57 ms 15432 KiB
03_star_03.txt AC 60 ms 15652 KiB