結果

問題 No.277 根掘り葉掘り
ユーザー purupuru
提出日時 2016-04-23 02:25:49
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 76 ms / 3,000 ms
コード長 1,865 bytes
コンパイル時間 1,645 ms
コンパイル使用メモリ 165,488 KB
実行使用メモリ 10,776 KB
最終ジャッジ日時 2024-04-15 04:17:21
合計ジャッジ時間 3,646 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
6,812 KB
testcase_01 AC 4 ms
6,944 KB
testcase_02 AC 4 ms
6,944 KB
testcase_03 AC 4 ms
6,940 KB
testcase_04 AC 4 ms
6,944 KB
testcase_05 AC 4 ms
6,940 KB
testcase_06 AC 5 ms
6,944 KB
testcase_07 AC 4 ms
6,940 KB
testcase_08 AC 4 ms
6,940 KB
testcase_09 AC 76 ms
9,772 KB
testcase_10 AC 50 ms
10,776 KB
testcase_11 AC 64 ms
10,368 KB
testcase_12 AC 61 ms
10,368 KB
testcase_13 AC 66 ms
10,624 KB
testcase_14 AC 65 ms
10,336 KB
testcase_15 AC 65 ms
10,200 KB
testcase_16 AC 67 ms
10,240 KB
testcase_17 AC 64 ms
10,584 KB
testcase_18 AC 65 ms
10,212 KB
testcase_19 AC 63 ms
10,496 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:42:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   42 |     scanf("%d", &N);
      |     ~~~~~^~~~~~~~~~
main.cpp:46:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   46 |         scanf("%d%d", &x, &y);
      |         ~~~~~^~~~~~~~~~~~~~~~

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define iter(c) __typeof((c).begin())
#define tr(i, c) for (iter(c) i = (c).begin(); i != (c).end(); i++)
#define REP(i, a, b) for (int i = a; i < (int)(b); i++)
#define rep(i, n) REP(i, 0, n)
#define mp make_pair

typedef vector<int> vi;
typedef pair<int, int> pii;

const int INF = 1 << 29;


template<class T>
bool chmin(T &a, const T &b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}

struct prepare {
    prepare() {
        cout.setf(ios::fixed, ios::floatfield);
        cout.precision(8);
        ios_base::sync_with_stdio(false);
    }
} _prepare;

const int MAX_N = (int) (1e5) + 1;
vi edges[MAX_N];

int R[MAX_N];
int L[MAX_N];

int main() {
    int N;
    scanf("%d", &N);

    rep(i, N - 1) {
        int x, y;
        scanf("%d%d", &x, &y);
        --x, --y;
        edges[x].push_back(y);
        edges[y].push_back(x);
    }

    memset(R, -1, sizeof(R));
    fill(L, L + MAX_N, INF);

    queue<pii> que, leaf_que;
    que.push(mp(0, 0));
    while (!que.empty()) {
        pii qp = que.front();
        que.pop();
        int v = qp.first;
        int d = qp.second;
        R[v] = d;

        bool has_child = false;
        tr(it, edges[v]) {
            if (R[*it] == -1) {
                que.push(mp(*it, d + 1));
                has_child = true;
            }
        }

        if (!has_child)
            leaf_que.push(mp(v, 0));
    }

    while (!leaf_que.empty()) {
        pii qp = leaf_que.front();
        leaf_que.pop();
        const int v = qp.first;
        const int d = qp.second;
        if (!chmin(L[v], d))
            continue;

        tr(it, edges[v]) {
            if (L[*it] > d + 1) {
                leaf_que.push(mp(*it, d + 1));
            }
        }
    }

    rep(i, N)
        printf("%d\n", min(R[i], L[i]));

    return 0;
}
0