結果

問題 No.277 根掘り葉掘り
ユーザー koyoprokoyopro
提出日時 2020-01-27 00:09:44
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,761 bytes
コンパイル時間 1,485 ms
コンパイル使用メモリ 152,020 KB
実行使用メモリ 26,104 KB
最終ジャッジ日時 2023-10-12 12:38:23
合計ジャッジ時間 4,640 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
9,540 KB
testcase_01 AC 5 ms
9,772 KB
testcase_02 WA -
testcase_03 AC 5 ms
9,744 KB
testcase_04 AC 5 ms
9,744 KB
testcase_05 AC 6 ms
9,708 KB
testcase_06 AC 5 ms
9,540 KB
testcase_07 AC 5 ms
9,608 KB
testcase_08 AC 5 ms
9,672 KB
testcase_09 AC 99 ms
26,104 KB
testcase_10 AC 121 ms
20,020 KB
testcase_11 AC 84 ms
20,120 KB
testcase_12 AC 109 ms
23,212 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
権限があれば一括ダウンロードができます

ソースコード

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, 0);
int D[6];
#else
vector<set<int>> E(123450);
vector<int> L(123450, 0);
int D[111111];
#endif

void dfs(int parent=-1, int i=0, int depth=0) {
    D[i] = depth;
    for (auto e : E[i]) {
        if (e == parent) continue;
        dfs(i, e, depth+1);
    }
}

int to_leaf(int parent=-1, int i=0) {
    if (i != 0 && E[i].size()==1) return 0;
    if (L[i]) return L[i];
    
    int l = LONG_LONG_MAX;
    for (auto e : E[i]) {
        if (e == parent) continue;
        l = min(l, to_leaf(i, e) + 1);
    }
    return L[i] = l;
}

void setup_tree() {
    dfs();
}
/**********************************************************/


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