結果

問題 No.3373 Partial Complement Tree
コンテスト
ユーザー The Forsaking
提出日時 2025-12-04 14:43:54
言語 C++17
(gcc 13.3.0 + boost 1.89.0)
結果
AC  
実行時間 77 ms / 2,000 ms
コード長 1,029 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,701 ms
コンパイル使用メモリ 195,368 KB
実行使用メモリ 29,388 KB
最終ジャッジ日時 2025-12-04 14:44:00
合計ジャッジ時間 5,600 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 24
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:36:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   36 |     scanf("%d", &T);
      |     ~~~~~^~~~~~~~~~
main.cpp:38:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   38 |         scanf("%d", &n);
      |         ~~~~~^~~~~~~~~~
main.cpp:42:18: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   42 |             scanf("%d%d", &a, &b);
      |             ~~~~~^~~~~~~~~~~~~~~~

ソースコード

diff #
raw source code

#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
const int N = 2000010, MOD = 998244353, INF = 0x3f3f3f3f;
int n, m, w[N];


int e[N], ne[N], h[N], idx;
ll f[200010][4], res;
void add(int a, int b) { e[idx] = b, ne[idx] = h[a], h[a] = idx++; }

void dfs(int r, int fa) {
    f[r][1] = f[r][2] = f[r][3] = 0;
    for (int i = h[r]; ~i; i = ne[i]) {
        int j = e[i];
        if (j == fa) continue;
        dfs(j, r);
        res = (res + f[r][2] + f[j][1] * f[r][1]) % MOD;
        f[r][1]++, f[r][2] += f[j][1], f[r][3] += f[j][2];
    }
    res = (res + f[r][3]) % MOD;
}

void solve() {
    res = 0;
    dfs(1, -1);
    int r = 1;
    printf("%lld\n", res);
}

int main() {
    int T;
    scanf("%d", &T);
    while (T--) {
        scanf("%d", &n);
        memset(h, -1, sizeof(int) * (n + 10));
        idx = 0;
        for (int i = 1, a, b; i < n; i++) {
            scanf("%d%d", &a, &b);
            add(a, b), add(b, a);
        }
        solve();
    }
    return 0;
}
0