結果

問題 No.1098 LCAs
ユーザー ysystem7ysystem7
提出日時 2020-09-01 22:36:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 378 ms / 2,000 ms
コード長 1,702 bytes
コンパイル時間 3,190 ms
コンパイル使用メモリ 216,908 KB
実行使用メモリ 34,768 KB
最終ジャッジ日時 2024-04-30 21:39:25
合計ジャッジ時間 9,225 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 1 ms
6,944 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 3 ms
6,948 KB
testcase_14 AC 3 ms
6,944 KB
testcase_15 AC 3 ms
6,944 KB
testcase_16 AC 3 ms
6,944 KB
testcase_17 AC 2 ms
6,944 KB
testcase_18 AC 353 ms
18,360 KB
testcase_19 AC 334 ms
18,480 KB
testcase_20 AC 326 ms
18,508 KB
testcase_21 AC 328 ms
18,452 KB
testcase_22 AC 330 ms
18,480 KB
testcase_23 AC 363 ms
18,740 KB
testcase_24 AC 302 ms
18,740 KB
testcase_25 AC 294 ms
18,924 KB
testcase_26 AC 305 ms
18,796 KB
testcase_27 AC 309 ms
18,924 KB
testcase_28 AC 378 ms
33,304 KB
testcase_29 AC 365 ms
34,768 KB
testcase_30 AC 367 ms
33,104 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define cinf(n,x) for(int i=0;i<(n);i++)cin>>x[i];
#define ft first
#define sc second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(v) (v).begin(),(v).end()
#define LB(a,x) lb(all(a),x)-a.begin()
#define UB(a,x) ub(all(a),x)-a.begin()
#define mod 1000000007
//#define mod 998244353
#define FS fixed<<setprecision(15)
using namespace std;
typedef long long ll;
const double pi=3.141592653589793;
template<class T> using V=vector<T>;
using Graph = vector<vector<int>>;
using P=pair<ll,ll>;
typedef unsigned long long ull;
typedef long double ldouble;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline void out(T a){ cout << a << '\n'; }
void YN(bool ok){if(ok) cout << "Yes" << endl; else cout << "No" << endl;}
//void YN(bool ok){if(ok) cout << "YES" << endl; else cout << "NO" << endl;}


const ll INF=1e18;
const int mx=200005;

V<V<ll>> G;

V<ll> a,b;

void dfs(ll v,ll p){
    ll tmp1=0,tmp2=0;
    for(ll u:G[v]){
        if(u==p) continue;
        dfs(u,v);
        a[v]+=a[u];
        tmp1+=a[u];
        tmp2+=a[u]*a[u];
    }
    a[v]++;
    b[v]=(tmp1*tmp1-tmp2)/2;
}

int main(){
    cin.tie(0);ios::sync_with_stdio(false);
    ll n;
    cin>>n;
    G.resize(n);
    a.resize(n);
    b.resize(n);
    rep(i,n-1){
        ll u,v;
        cin>>u>>v;
        u--;v--;
        G[u].pb(v);
        G[v].pb(u);
    }
    rep(i,n) a[i]=0;
    dfs(0,-1);
    rep(i,n) cout<<2*a[i]+2*b[i]-1<<endl;
}
0