結果
| 問題 | 
                            No.2638 Initial fare
                             | 
                    
| コンテスト | |
| ユーザー | 
                             srjywrdnprkt
                         | 
                    
| 提出日時 | 2024-02-29 09:54:33 | 
| 言語 | C++17  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 67 ms / 2,000 ms | 
| コード長 | 735 bytes | 
| コンパイル時間 | 1,879 ms | 
| コンパイル使用メモリ | 198,292 KB | 
| 最終ジャッジ日時 | 2025-02-19 21:56:50 | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge5 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 25 | 
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main(){
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    ll n, ans=0;
    cin >> n;
    vector<pair<ll, ll>> e(n);
    vector<ll> deg(n);
    for (ll i = 0; i < n - 1; i++){
        ll a, b;
        cin >> a >> b;
        a--; b--;
        e[i] = {a, b};
        deg[a]++;
        deg[b]++;
    }
    //距離1=線路の数
    ans += n - 1;
    //距離2=各駅の次数C2
    for (int i=0; i<n; i++){
        ans += deg[i] * (deg[i] - 1) / 2;
    }
    //距離3=各線路の両端の次数-1の積
    for (int i=0; i<n-1; i++){
        ans += (deg[e[i].first] - 1) * (deg[e[i].second] - 1);
    }
    cout << ans << endl;
    return 0;
}
            
            
            
        
            
srjywrdnprkt