結果

問題 No.2638 Initial fare
ユーザー umezoumezo
提出日時 2024-02-20 04:05:39
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 108 ms / 2,000 ms
コード長 716 bytes
コンパイル時間 2,455 ms
コンパイル使用メモリ 203,956 KB
実行使用メモリ 30,708 KB
最終ジャッジ日時 2024-02-20 04:05:45
合計ジャッジ時間 5,622 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
8,640 KB
testcase_01 AC 3 ms
8,640 KB
testcase_02 AC 3 ms
8,640 KB
testcase_03 AC 3 ms
8,640 KB
testcase_04 AC 80 ms
17,480 KB
testcase_05 AC 86 ms
17,812 KB
testcase_06 AC 3 ms
8,640 KB
testcase_07 AC 72 ms
17,620 KB
testcase_08 AC 56 ms
17,528 KB
testcase_09 AC 63 ms
18,040 KB
testcase_10 AC 64 ms
18,120 KB
testcase_11 AC 3 ms
8,640 KB
testcase_12 AC 79 ms
17,528 KB
testcase_13 AC 54 ms
17,196 KB
testcase_14 AC 65 ms
17,996 KB
testcase_15 AC 63 ms
18,128 KB
testcase_16 AC 3 ms
8,640 KB
testcase_17 AC 69 ms
18,272 KB
testcase_18 AC 66 ms
18,012 KB
testcase_19 AC 76 ms
19,736 KB
testcase_20 AC 82 ms
18,816 KB
testcase_21 AC 81 ms
20,216 KB
testcase_22 AC 3 ms
8,640 KB
testcase_23 AC 106 ms
30,708 KB
testcase_24 AC 108 ms
28,472 KB
testcase_25 AC 3 ms
8,640 KB
testcase_26 AC 99 ms
24,812 KB
testcase_27 AC 105 ms
28,844 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define rep(i, n) for(int i=0;i<(int)(n);i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

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

vector<int> G[200200];
int dp[200200][4];
ll ans=0;
void dfs(int v,int p){
  dp[v][0]=1;
  ll c1=0,c2=0;
  for(auto nv:G[v]){
    if(nv==p) continue;
    dfs(nv,v);
    rep(i,3) dp[v][i+1]+=dp[nv][i];
    c1+=dp[nv][0];
    c2+=dp[nv][1];
  }
  ans+=c1*(c1-1)/2+(c1-1)*c2;
}

int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);

  int n;
  cin>>n;
  rep(i,n-1){
    int u,v;
    cin>>u>>v;
    u--,v--;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  dfs(0,-1);
  rep(i,n) for(int j=1;j<=3;j++) ans+=dp[i][j];
  cout<<ans<<endl;
  
  return 0;
}
0