結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー sgswsgsw
提出日時 2021-08-14 23:07:26
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,720 bytes
コンパイル時間 3,620 ms
コンパイル使用メモリ 283,220 KB
実行使用メモリ 19,200 KB
最終ジャッジ日時 2024-04-15 20:33:02
合計ジャッジ時間 6,065 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 4 ms
5,376 KB
testcase_17 AC 4 ms
5,376 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 3 ms
5,376 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 3 ms
5,376 KB
testcase_24 AC 3 ms
5,376 KB
testcase_25 AC 3 ms
5,376 KB
testcase_26 AC 4 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 4 ms
5,376 KB
testcase_30 AC 4 ms
5,376 KB
testcase_31 AC 3 ms
5,376 KB
testcase_32 AC 3 ms
5,376 KB
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 AC 5 ms
5,376 KB
testcase_37 WA -
testcase_38 WA -
testcase_39 AC 2 ms
5,376 KB
testcase_40 AC 2 ms
5,376 KB
testcase_41 AC 2 ms
5,376 KB
testcase_42 AC 1 ms
5,376 KB
testcase_43 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
/*
    C++(GCC++17) Template for Programming-Contest.

    author : sgsw

    generated : 2021/08/14   
    when : 22:55:57

*/
using namespace std;

#pragma GCC optimize("Ofast")

#define ll long long
#define ld long double
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rep1(i, a, b) for (int i = (b) - 1; i >= a; i--)
#define endl '\n'
#define vvii vector<vector<int>>
#define vi vector<int>
#define vvll vector<vector<ll>>
#define vl vector<ll>

template<class T> ostream& operator << (ostream &s, vector<T> &P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }

template <class T>bool chmax(T &a, T b){if (a < b){a = b;return true;}return false;}
template <class T>bool chmin(T &a, T b){if (a > b){a = b;return true;}return false;}

template <class T = int>T gcd(T a, T b){return (b == 0) ? a : gcd(b, a % b);}
template <class T = int>T lcm(T a, T b){return a / gcd(a, b) * b;}
template<class T = int>T powMod(T x, T k, T m) {if (k == 0){return (T)1;}if (k % 2 == 0) {return powMod(x*x % m, k/2, m);}else{return x*powMod(x, k-1, m) % m;}}
template <class T = int>T extgcd(T a,T b,T &x,T &y){T g = a;x = 1;y = 0;if (b != 0) {g = extgcd(b, a % b, y, x), y -= (a / b) * x;}return g;}
template<class T = int> T invMod(T a,T m){T x,y;if (extgcd(a, m, x, y) == 1) {return (x + m) % m;}else{return -1;}}

using Pii  = pair<int,int>;
using Pll = pair<ll,ll>;
const int inf = 1e9;

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int n;
    cin >> n;
    vector<vector<int>> g(n);
    rep(i,0,n - 1){
        int u,v;
        cin >> u >> v;
        u--;v--;
        g[u].emplace_back(v);
        g[v].emplace_back(u);
    }

    vector<int> child(n);
    vector<int> par(n,-1);
    vector<int> dist(n,inf);
    int S = 0;dist[S] = 0;
    ll ans = 0;

    //step 1. dfs_1
    function<void(int)> dfs = [&](int u){
        for (int v : g[u]){
            if (dist[v] == inf){
                dist[v] = dist[u] + 1;
                par[v] = u;
                dfs(v);
            }
        }
        child[u]++;
        for (int v : g[u]){
            if (v == par[u])continue;
            child[u] += child[v];
        }
        ans += child[u];
    };
    dfs(S);

    //step 2. dfs_2
    ll final_ans = 0;
    dist.assign(n,inf);
    dist[S] = 0;

    function<void(int)> dfs2 = [&](int u){
        final_ans = (final_ans + ans);
        for (int v : g[u]){
            if (dist[v] == inf){
                dist[v] = dist[u] + 1;
                ans += n - 2 * child[v];
                dfs2(v);
            }
        }
        ans -= n - 2 * child[u];
    };

    dfs2(S);
    printf("%d\n",final_ans);
    return 0;
}
0