結果

問題 No.1098 LCAs
ユーザー yukudoyukudo
提出日時 2020-07-01 03:37:31
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 182 ms / 2,000 ms
コード長 1,223 bytes
コンパイル時間 4,095 ms
コンパイル使用メモリ 167,000 KB
実行使用メモリ 29,492 KB
最終ジャッジ日時 2023-10-11 18:37:22
合計ジャッジ時間 8,517 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
9,676 KB
testcase_01 AC 3 ms
9,652 KB
testcase_02 AC 4 ms
9,672 KB
testcase_03 AC 4 ms
9,724 KB
testcase_04 AC 4 ms
9,640 KB
testcase_05 AC 4 ms
9,628 KB
testcase_06 AC 4 ms
9,624 KB
testcase_07 AC 4 ms
9,848 KB
testcase_08 AC 3 ms
9,852 KB
testcase_09 AC 4 ms
9,648 KB
testcase_10 AC 4 ms
9,668 KB
testcase_11 AC 4 ms
9,672 KB
testcase_12 AC 4 ms
9,724 KB
testcase_13 AC 4 ms
9,696 KB
testcase_14 AC 4 ms
9,720 KB
testcase_15 AC 5 ms
9,792 KB
testcase_16 AC 4 ms
9,724 KB
testcase_17 AC 4 ms
9,700 KB
testcase_18 AC 156 ms
18,012 KB
testcase_19 AC 147 ms
18,268 KB
testcase_20 AC 147 ms
18,008 KB
testcase_21 AC 144 ms
18,088 KB
testcase_22 AC 145 ms
18,192 KB
testcase_23 AC 100 ms
18,324 KB
testcase_24 AC 101 ms
18,240 KB
testcase_25 AC 95 ms
18,256 KB
testcase_26 AC 102 ms
18,196 KB
testcase_27 AC 100 ms
18,292 KB
testcase_28 AC 177 ms
28,484 KB
testcase_29 AC 175 ms
29,492 KB
testcase_30 AC 182 ms
28,344 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define REP(i,n) for(int i=0,_n=(int)(n);i<_n;++i)
#define ALL(v) (v).begin(),(v).end()
#define CLR(t,v) memset(t,(v),sizeof(t))
template<class T1,class T2>ostream& operator<<(ostream& os,const pair<T1,T2>&a){return os<<"("<<a.first<<","<<a.second<< ")";}
template<class T>void pv(T a,T b){for(T i=a;i!=b;++i)cout<<(*i)<<" ";cout<<endl;}
template<class T>void chmin(T&a,const T&b){if(a>b)a=b;}
template<class T>void chmax(T&a,const T&b){if(a<b)a=b;}

ll nextLong() { ll x; scanf("%lld", &x); return x;}

const int MAX_N = 212345;
vector<int> g[MAX_N];
ll sz[MAX_N];
ll ans[MAX_N];

void dfs(int cur, int prev) {
  ans[cur] = 0;
  sz[cur] = 1;
  for (int nxt : g[cur]) if (nxt != prev) {
    dfs(nxt, cur);
    ans[cur] -= sz[nxt] * sz[nxt];
    sz[cur] += sz[nxt];
  }
  ans[cur] += sz[cur] * sz[cur];
}

int main2() {
  int N = nextLong();
  REP(i, N-1) {
    int a = nextLong() - 1;
    int b = nextLong() - 1;
    g[a].push_back(b);
    g[b].push_back(a);
  }

  dfs(0, -1);
  REP(i, N) {
    cout << ans[i] << '\n';
  }

  REP(i, N) g[i].clear();
  return 0;
}

int main() {

#ifdef LOCAL
  for (;!cin.eof();cin>>ws)
#endif
    main2();
  return 0;
}
0