結果

問題 No.2638 Initial fare
コンテスト
ユーザー zjsdut
提出日時 2026-09-01 21:24:16
言語 C++23
(gcc 15.3.0 + boost 1.92.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 27 ms / 2,000 ms
+ 946µs
コード長 780 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,887 ms
コンパイル使用メモリ 338,820 KB
実行使用メモリ 9,740 KB
最終ジャッジ日時 2026-09-01 21:24:25
合計ジャッジ時間 8,490 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 25
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

/**
 *    author:  zjs
 *    created: 01.09.2026 20:20:59
**/
#include <bits/stdc++.h>
#include <cassert> // <bits/stdc++.h> does not include cassert since GCC 16.
using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    vector<int> deg(n + 1);
    vector<pair<int,int>> e(n - 1);
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        cin >> u >> v;
        deg[u]++;
        deg[v]++;
        e[i] = {u, v};
    }
    long long ans = n - 1;
    for (int i = 1; i <= n; i++)
        ans += (long long) deg[i] * (deg[i] - 1) / 2;
    
    for (auto [u, v] : e) {
        ans += (long long) (deg[u] - 1) * (deg[v] - 1);
    }
    cout << ans << '\n';
}
0