結果

問題 No.277 根掘り葉掘り
ユーザー koyoprokoyopro
提出日時 2020-01-27 00:27:13
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 110 ms / 3,000 ms
コード長 1,636 bytes
コンパイル時間 1,355 ms
コンパイル使用メモリ 155,520 KB
実行使用メモリ 20,224 KB
最終ジャッジ日時 2023-10-12 12:38:56
合計ジャッジ時間 3,454 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
9,672 KB
testcase_01 AC 5 ms
9,744 KB
testcase_02 AC 5 ms
9,540 KB
testcase_03 AC 6 ms
9,772 KB
testcase_04 AC 5 ms
9,764 KB
testcase_05 AC 5 ms
9,664 KB
testcase_06 AC 5 ms
9,696 KB
testcase_07 AC 5 ms
9,632 KB
testcase_08 AC 5 ms
9,828 KB
testcase_09 AC 76 ms
19,292 KB
testcase_10 AC 110 ms
20,224 KB
testcase_11 AC 77 ms
19,568 KB
testcase_12 AC 75 ms
19,576 KB
testcase_13 AC 87 ms
19,688 KB
testcase_14 AC 82 ms
19,912 KB
testcase_15 AC 81 ms
19,560 KB
testcase_16 AC 79 ms
19,500 KB
testcase_17 AC 77 ms
19,552 KB
testcase_18 AC 77 ms
19,588 KB
testcase_19 AC 76 ms
19,532 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define int long long
#define FOR(i, a, b) for(int i=(a);i<(b);i++)
#define RFOR(i, a, b) for(int i=(b-1);i>=(a);i--)
#define REP(i, n) for(int i=0; i<(n); i++)
#define RREP(i, n) for(int i=(n-1); i>=0; i--)
#define ALL(a) (a).begin(),(a).end()
#define UNIQUE_SORT(l) sort(ALL(l)); l.erase(unique(ALL(l)), l.end());
#define CONTAIN(a, b) find(ALL(a), (b)) != (a).end()
#define array2(type, x, y) array<array<type, y>, x>
#define vector2(type) vector<vector<type> >
#define out(...) printf(__VA_ARGS__)

int dxy[] = {0, 1, 0, -1, 0};

void solve();
signed main()
{
#if DEBUG
    std::ifstream in("input.txt");
    std::cin.rdbuf(in.rdbuf());
#endif
    cin.tie(0);
    ios::sync_with_stdio(false);
    solve();
    return 0;
}

/*================================*/

int N,M,Q;

#if DEBUG
vector<set<int>> E(6);
vector<int> L(6, INT_MAX);
#else
vector<set<int>> E(123450);
vector<int> L(123450, INT_MAX);
#endif


void bfs() {
    queue<int> que;
    L[0]=0;
    que.push(0);
    REP(i,N) if (E[i].size()==1) {
        L[i]=0;
        que.push(i);
    }
    while(que.size()) {
        int i = que.front();
        que.pop();
        for (auto e : E[i]) {
            int cost = L[i] + 1;
            if (L[e] > cost) {
                L[e] = cost;
                que.push(e);
            }
        }
    }
}

/**********************************************************/


void solve() {
    cin>>N;
    int u,v;
    REP(_,N-1) {
        cin>>u>>v;
        u--;
        v--;
        E[u].insert(v);
        E[v].insert(u);
    }
    bfs();
    REP(i,N) {
        out("%lld\n", L[i]);
    }
}
0