結果

問題 No.277 根掘り葉掘り
ユーザー uenokuuenoku
提出日時 2017-01-08 15:04:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,175 bytes
コンパイル時間 1,424 ms
コンパイル使用メモリ 89,848 KB
実行使用メモリ 19,660 KB
最終ジャッジ日時 2023-08-22 17:37:44
合計ジャッジ時間 7,179 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,392 KB
testcase_01 AC 3 ms
6,264 KB
testcase_02 AC 3 ms
6,460 KB
testcase_03 AC 4 ms
6,332 KB
testcase_04 AC 5 ms
6,204 KB
testcase_05 AC 4 ms
6,188 KB
testcase_06 AC 4 ms
6,188 KB
testcase_07 AC 3 ms
6,188 KB
testcase_08 AC 4 ms
6,248 KB
testcase_09 AC 217 ms
9,492 KB
testcase_10 TLE -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
using namespace std;
typedef long long int lli;
lli MOD = 1000000007;
int main()
{
    int n;
    cin >> n;
    vector<int> e[100005];
    int a, b;
    rep(i, n - 1)
    {
        cin >> a >> b;
        a--, b--;
        e[a].push_back(b);
        e[b].push_back(a);
    }
    int ans[100005] = {};
    rep(i, n) ans[i] = 1e7;
    set<int> start;
    start.insert(0);
    rep(i, n) if (e[i].size() == 1) start.insert(i);
    for (auto s : start) {
        queue<pair<int, int>> que;  //id,depth
        que.push(make_pair(s, 0));
        bool used[100005] = {};
        while (!que.empty()) {
            int id = que.front().first;
            int d = que.front().second;
            que.pop();
            ans[id] = min(ans[id], d);
            used[id] = true;
            for (auto j : e[id]) {
                if (!used[j])
                    que.push(make_pair(j, d + 1));
            }
        }
    }
    rep(i, n) cout << ans[i] << endl;
}
0