結果

問題 No.277 根掘り葉掘り
ユーザー izryt(趣味)izryt(趣味)
提出日時 2016-08-09 06:06:50
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,347 bytes
コンパイル時間 1,266 ms
コンパイル使用メモリ 98,208 KB
実行使用メモリ 15,872 KB
最終ジャッジ日時 2024-04-24 22:33:30
合計ジャッジ時間 8,113 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'long long int get_d_root(long long int)':
main.cpp:83:1: warning: no return statement in function returning non-void [-Wreturn-type]
   83 | }
      | ^

ソースコード

diff #

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <functional>

using namespace std;

#define fst first
#define scd second
#define PB push_back
#define MP make_pair
#define int long long
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define omajinai ios::sync_with_stdio(false);cin.tie(0)

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;

template<typename T>T& max(T&a,T&b){if(a>=b)return a;return b;}
template<typename T>T& min(T&a,T&b){if(a<b)return a;return b;}
template<typename T>bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<typename T>bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<typename T>T get(){T a;cin>>a;return a;}
template<typename T>T rev(T a){reverse(all(a));return a;}
template<typename T>vector<T>&sort(vector<T>&a){sort(all(a));return a;}

const int inf = 1e9;
const ll linf = 3e18;
const double eps = 1e-9;

int par[100010];
vi ch[100010];
int d_leaf[100010];
bool used_d_leaf[100010];
int d_root[100010];

int get_d_leaf(int root)
{
    if (ch[root].size() == 1 && ch[root][0] == par[root]) return d_leaf[root] = 0;

    for (int u : ch[root]) {
        if (par[root] == u) continue;
        par[u] = root;
        chmin(d_leaf[root], get_d_leaf(u) + 1);
    }

    return d_leaf[root];
}

int get_d_root(int root)
{
    queue<int> q;
    q.push(root);
    d_root[root] = 0;

    while (q.size()) {
        int now = q.front(); q.pop();

        for (int u : ch[now]) {
            if (~d_root[u]) continue;
            d_root[u] = d_root[now] + 1;
            q.push(u);
        }
    }
}

signed main()
{
    int N; cin >> N;

    fill_n(par, N + 10, -1);
    fill_n(d_leaf, N + 10, inf);
    fill_n(d_root, N + 10, -1);

    for (int i = 0; i < N - 1; ++i) {
        int u, v; cin >> u >> v;
        --u, --v;
        ch[u].push_back(v);
        ch[v].push_back(u);
    }

    get_d_leaf(0);
    get_d_root(0);

    for (int i = 0; i < N; ++i) {
        cout << min(d_leaf[i], d_root[i]) << endl;
    }
}
0