結果

問題 No.277 根掘り葉掘り
ユーザー nebukuro09nebukuro09
提出日時 2016-10-03 16:09:11
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,132 bytes
コンパイル時間 629 ms
コンパイル使用メモリ 70,480 KB
実行使用メモリ 14,016 KB
最終ジャッジ日時 2024-05-01 11:20:28
合計ジャッジ時間 5,642 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
13,888 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 1 ms
6,944 KB
testcase_09 AC 232 ms
8,960 KB
testcase_10 TLE -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <stack>
using namespace std;

struct treenode {
  int num;
  int depth;
  int parent;
  treenode(int num, int depth, int parent) : num(num), depth(depth), parent(parent) {}
};

int N, x, y;
vector<vector<int>> rinsetsu;
vector<int> leaves;
int ans[100001] = {};

void dfs(int start, bool happa) {
  stack<treenode> st;
  st.emplace(start, 0, -1);
  while (!st.empty()) {
    treenode n = st.top();
    st.pop();
    if (ans[n.num] > n.depth)
      ans[n.num] = n.depth;
    if (happa && (int)rinsetsu[n.num].size() == 1 && n.parent > -1) {
      leaves.push_back(n.num);
      continue;
    }
    for (auto &nn:rinsetsu[n.num]) {
      if (nn != n.parent)
	st.emplace(nn, n.depth+1, n.num);
    }
  }
}

int main() {
  cin >> N;
  rinsetsu = vector<vector<int>>(N);
  for (int i = 0; i < N-1; i++) {
    cin >> x >> y;
    rinsetsu[x-1].push_back(y-1);
    rinsetsu[y-1].push_back(x-1);
  }
  for (int i = 0; i < N; i++) {
    ans[i] = 200000;
  }

  dfs(0, true);
  for (auto &leaf:leaves)
    dfs(leaf, false);
  for (int i = 0; i < N; i++)
    cout << ans[i] << endl;
  return 0;
}
0