結果

問題 No.1098 LCAs
ユーザー minatominato
提出日時 2020-06-26 21:53:36
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 219 ms / 2,000 ms
コード長 4,715 bytes
コンパイル時間 3,486 ms
コンパイル使用メモリ 241,708 KB
実行使用メモリ 33,128 KB
最終ジャッジ日時 2023-09-18 04:18:03
合計ジャッジ時間 7,772 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 151 ms
17,308 KB
testcase_19 AC 160 ms
17,284 KB
testcase_20 AC 162 ms
17,216 KB
testcase_21 AC 159 ms
17,408 KB
testcase_22 AC 166 ms
17,172 KB
testcase_23 AC 107 ms
19,848 KB
testcase_24 AC 111 ms
19,976 KB
testcase_25 AC 97 ms
20,160 KB
testcase_26 AC 106 ms
19,924 KB
testcase_27 AC 102 ms
19,924 KB
testcase_28 AC 215 ms
31,760 KB
testcase_29 AC 218 ms
33,128 KB
testcase_30 AC 219 ms
31,780 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
using ll = long long;
using u64 = uint_fast64_t;
using pii =  pair<int, int>;
using pll =  pair<long long, long long>;
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()
constexpr char ln =  '\n';
constexpr long long MOD = 1000000007;
//constexpr long long MOD = 998244353;
template<class T1, class T2> inline bool chmax(T1 &a, T2 b) { if (a < b) { a = b; return true;} return false; }
template<class T1, class T2> inline bool chmin(T1 &a, T2 b) { if (a > b) { a = b; return true;} return false; }
inline int popcount(int x) {return __builtin_popcount(x);}
inline int popcount(long long x) {return __builtin_popcountll(x);}
void print() { cout << "\n"; }
template<class T, class... Args>
void print(const T &x, const Args &... args) {
    cout << x << " ";
    print(args...);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////

struct HeavyLightDecomposition {
    vector<vector<int>> G;
    vector<int> vid, head, sub, par, dep, inv, type;

    HeavyLightDecomposition(int N) :
        G(N),vid(N,-1),head(N),sub(N,1),
        par(N,-1),dep(N,0),inv(N),type(N){}

    void add_edge(int u,int v) {
        G[u].emplace_back(v);
        G[v].emplace_back(u);
    }

    void build(vector<int> rs={0}) {
        int c=0,pos=0;
        for(int r:rs) {
            dfs_sz(r);
            head[r]=r;
            dfs_hld(r,c++,pos);
        }
    }

    void dfs_sz(int v) {
        for(int &u:G[v]) if(u==par[v]) swap(u,G[v].back());
        if(~par[v]) G[v].pop_back();
        for(int &u:G[v]) {
            par[u]=v;
            dep[u]=dep[v]+1;
            dfs_sz(u);
            sub[v]+=sub[u];
            if(sub[u]>sub[G[v][0]]) swap(u,G[v][0]);
        }
    }

    void dfs_hld(int v,int c,int &pos) {
        vid[v]=pos++;
        inv[vid[v]]=v;
        type[v]=c;
        for(int u:G[v]){
            if(u==par[v]) continue;
            head[u]=(u==G[v][0]?head[v]:u);
            dfs_hld(u,c,pos);
        }
    }

    int lca(int u,int v){
        while(1){
            if(vid[u]>vid[v]) swap(u,v);
            if(head[u]==head[v]) return u;
            v=par[head[v]];
        }
    }

    int distance(int u,int v){
        return dep[u]+dep[v]-2*dep[lca(u,v)];
    }

    // for_each(vertex)
    template<typename F>
    void for_each(int u, int v, const F& f) {
        while(1){
            if(vid[u]>vid[v]) swap(u,v);
            f(max(vid[head[v]],vid[u]),vid[v]+1);
            if(head[u]!=head[v]) v=par[head[v]];
            else break;
        }
    }

    template<typename T,typename Q,typename F>
    T for_each(int u,int v,T ti,const Q &q,const F &f){
        T l=ti,r=ti;
        while(1){
            if(vid[u]>vid[v]){
                swap(u,v);
                swap(l,r);
            }
            l=f(l,q(max(vid[head[v]],vid[u]),vid[v]+1));
            if(head[u]!=head[v]) v=par[head[v]];
            else break;
        }
        return f(l,r);
    }

    // for_each(edge)
    // [l, r) <- attention!!
    template<typename F>
    void for_each_edge(int u, int v,const F& f) {
        while(1){
            if(vid[u]>vid[v]) swap(u,v);
            if(head[u]!=head[v]){
                f(vid[head[v]],vid[v]+1);
                v=par[head[v]];
            }else{
                if(u!=v) f(vid[u]+1,vid[v]+1);
                break;
            }
        }
    }

    int search(int v, int d) {
        if (dep[v] < d || d < 0) return -1;
        while (1) {
            if (dep[v] - dep[head[v]] + 1 <= d) {
                d -= dep[v] - dep[head[v]] + 1;
                v = par[head[v]];
            } else {
                return inv[vid[v]-d];
            }
        }
    }
};

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

    vector<ll> ans(N),sub(N);
    auto dfs=[&](auto&& self, int v, int pv)->void {
        vector<ll> chi;
        sub[v] = 1;
        for (auto nv : G[v]) {
            if (pv==nv) continue;
            self(self,nv,v);
            chi.emplace_back(sub[nv]);
            sub[v] += sub[nv];
        }
        ll S = accumulate(all(chi),0LL);
        ans[v] = S*S;
        for (auto nv : G[v]) {
            if (pv==nv) continue;
            ans[v] -= sub[nv]*sub[nv];
        }
        ans[v] += sub[v]*2-1;
    };
    dfs(dfs,0,-1);
    rep(i,N) cout << ans[i] << ln;
}
0