結果

問題 No.277 根掘り葉掘り
ユーザー Konton7Konton7
提出日時 2020-02-03 01:03:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,309 bytes
コンパイル時間 2,199 ms
コンパイル使用メモリ 207,216 KB
実行使用メモリ 16,192 KB
最終ジャッジ日時 2023-10-19 01:38:10
合計ジャッジ時間 6,894 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 WA -
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 240 ms
16,192 KB
testcase_10 AC 195 ms
10,344 KB
testcase_11 AC 219 ms
9,856 KB
testcase_12 AC 231 ms
13,024 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;
using ll = long long;
using PII = std::pair<int, int>;
using PLL = std::pair<ll, ll>;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)



vector<int> depth, height, parent;
vector<vector<int>> nodes;

void dfs1(int s, int d){
    depth[s] = d;
    for (auto z : nodes[s])
        if (z != parent[s])
        {
            parent[z] = s;
            dfs1(z, d+1);
        }
}

void dfs2(int s){

    for (auto z : nodes[s])
        if (z != parent[s])
        {
            dfs2(z);
        }

    if (nodes[s].size() == 1 && parent[s] != -1)
        height[s] = 0;
    height[parent[s]] = min(height[parent[s]], height[s] + 1);
}

int main()
{

#ifdef DEBUG
    cout << "DEBUG MODE" << endl;
    ifstream in("input.txt"); //for debug
    cin.rdbuf(in.rdbuf());    //for debug
#endif

    int n, a, b;
    cin >> n;

    depth.resize(n), height.resize(n), parent.resize(n), nodes.resize(n);
    parent[0] = -1;
    rep(i, n-1)
    {
        cin >> a >> b;
        a--, b--;
        nodes[a].push_back(b);
        nodes[b].push_back(a);
    }  
    dfs1(0, 0);
    fill(height.begin(), height.end(), n);
    dfs2(0);

    rep(i, n)
        cout << min(depth[i], height[i]) << endl;



    return 0;
}
0