結果

問題 No.2638 Initial fare
ユーザー satama123satama123
提出日時 2024-02-19 22:29:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 853 bytes
コンパイル時間 778 ms
コンパイル使用メモリ 83,184 KB
実行使用メモリ 21,604 KB
最終ジャッジ日時 2024-02-19 22:29:36
合計ジャッジ時間 5,216 ms
ジャッジサーバーID
(参考情報)
judge16 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
13,480 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 1 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 174 ms
14,012 KB
testcase_05 AC 191 ms
14,508 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 TLE -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <deque>

using namespace std;

int main() {
    int N;
    cin >> N;

    vector<vector<int>> edge(N);

    for (int i = 0; i < N - 1; ++i) {
        int u, v;
        cin >> u >> v;
        u--;
        v--;
        edge[u].push_back(v);
        edge[v].push_back(u);
    }

    int ans = 0;
    deque<int> nxt;
    vector<bool> used(N, false);
    used[0] = true;
    nxt.push_back(0);

    while (!nxt.empty()) {
        int pos = nxt.back();
        nxt.pop_back();

        for (int to : edge[pos]) {
            ans++;
            for (int nt : edge[to]) {
                if (nt != pos) ans += edge[nt].size();
            }
            if (!used[to]) {
                nxt.push_back(to);
                used[to] = true;
            }
        }
    }

    cout << ans / 2 << endl;

    return 0;
}
0