結果

問題 No.1098 LCAs
ユーザー NewGardenNewGarden
提出日時 2020-06-26 21:58:06
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 285 ms / 2,000 ms
コード長 1,012 bytes
コンパイル時間 1,653 ms
コンパイル使用メモリ 171,748 KB
実行使用メモリ 28,108 KB
最終ジャッジ日時 2023-09-18 04:27:33
合計ジャッジ時間 6,933 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
9,880 KB
testcase_01 AC 6 ms
10,024 KB
testcase_02 AC 5 ms
9,860 KB
testcase_03 AC 5 ms
10,004 KB
testcase_04 AC 6 ms
9,864 KB
testcase_05 AC 6 ms
9,884 KB
testcase_06 AC 6 ms
10,044 KB
testcase_07 AC 6 ms
10,040 KB
testcase_08 AC 6 ms
9,932 KB
testcase_09 AC 6 ms
9,920 KB
testcase_10 AC 6 ms
9,980 KB
testcase_11 AC 6 ms
10,104 KB
testcase_12 AC 6 ms
9,876 KB
testcase_13 AC 6 ms
10,044 KB
testcase_14 AC 6 ms
9,992 KB
testcase_15 AC 6 ms
9,884 KB
testcase_16 AC 7 ms
10,000 KB
testcase_17 AC 6 ms
10,104 KB
testcase_18 AC 245 ms
16,376 KB
testcase_19 AC 247 ms
16,480 KB
testcase_20 AC 248 ms
16,460 KB
testcase_21 AC 249 ms
16,432 KB
testcase_22 AC 246 ms
16,484 KB
testcase_23 AC 173 ms
19,092 KB
testcase_24 AC 174 ms
19,224 KB
testcase_25 AC 166 ms
19,140 KB
testcase_26 AC 198 ms
19,200 KB
testcase_27 AC 188 ms
19,200 KB
testcase_28 AC 285 ms
26,992 KB
testcase_29 AC 283 ms
28,108 KB
testcase_30 AC 283 ms
27,036 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
typedef long double ld;
const int inf=1e9+7;
const ll longinf=1LL<<60;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
#define F first
#define S second
constexpr char ln = '\n';

const int mx=200010;
const ll mod=1e9+7;


vector<int> v[mx];
vector<int> ne(mx);
vector<ll> cnt(mx,0);


ll dfs(int x, int p){
  ll tmp = 0;
  ne[x] = p;
  for(auto to:v[x]){
    if(to==p) continue;
    tmp += dfs(to,x) + 1;
  }
  return cnt[x] = tmp;
}

int main(){
  int n;
  cin >> n;
  rep(i,n-1){
    int x,y; cin >> x >> y; x--; y--;
    v[x].emplace_back(y);
    v[y].emplace_back(x);
  }
  dfs(0,-1);
  rep(i,n){
    vector<ll> a;
    ll sum = 0;
    for(auto to:v[i]){
      if(to==ne[i]) continue;
      a.emplace_back(cnt[to]+1);
      sum += cnt[to]+1;
    }
    ll ans = 1;
    for(auto it:a){
      ans += it*(sum-it);
    }
    ans += sum*2;
    cout << ans << ln;
  }
  return 0;
}
0