結果

問題 No.2638 Initial fare
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2024-02-29 09:54:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 68 ms / 2,000 ms
コード長 735 bytes
コンパイル時間 7,057 ms
コンパイル使用メモリ 205,236 KB
実行使用メモリ 8,064 KB
最終ジャッジ日時 2024-02-29 09:54:50
合計ジャッジ時間 16,669 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 2 ms
6,548 KB
testcase_02 AC 2 ms
6,548 KB
testcase_03 AC 2 ms
6,548 KB
testcase_04 AC 50 ms
7,808 KB
testcase_05 AC 64 ms
8,064 KB
testcase_06 AC 2 ms
6,548 KB
testcase_07 AC 50 ms
7,936 KB
testcase_08 AC 45 ms
7,680 KB
testcase_09 AC 47 ms
8,064 KB
testcase_10 AC 68 ms
8,064 KB
testcase_11 AC 2 ms
6,548 KB
testcase_12 AC 47 ms
7,936 KB
testcase_13 AC 42 ms
7,424 KB
testcase_14 AC 49 ms
7,936 KB
testcase_15 AC 45 ms
8,064 KB
testcase_16 AC 2 ms
6,548 KB
testcase_17 AC 46 ms
7,808 KB
testcase_18 AC 45 ms
7,808 KB
testcase_19 AC 50 ms
7,808 KB
testcase_20 AC 49 ms
7,808 KB
testcase_21 AC 49 ms
7,808 KB
testcase_22 AC 2 ms
6,548 KB
testcase_23 AC 50 ms
7,936 KB
testcase_24 AC 49 ms
8,064 KB
testcase_25 AC 2 ms
6,548 KB
testcase_26 AC 51 ms
8,064 KB
testcase_27 AC 52 ms
8,064 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0