結果
問題 | No.2638 Initial fare |
ユーザー |
![]() |
提出日時 | 2024-02-20 04:05:39 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 179 ms / 2,000 ms |
コード長 | 716 bytes |
コンパイル時間 | 1,841 ms |
コンパイル使用メモリ | 195,320 KB |
最終ジャッジ日時 | 2025-02-19 17:53:38 |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 25 |
ソースコード
#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; }