結果

問題 No.277 根掘り葉掘り
ユーザー togatogatogatoga
提出日時 2015-09-05 11:54:11
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 228 ms / 3,000 ms
コード長 1,901 bytes
コンパイル時間 869 ms
コンパイル使用メモリ 96,504 KB
実行使用メモリ 11,528 KB
最終ジャッジ日時 2023-09-26 08:46:51
合計ジャッジ時間 4,782 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,500 KB
testcase_01 AC 4 ms
6,480 KB
testcase_02 AC 4 ms
6,448 KB
testcase_03 AC 4 ms
6,592 KB
testcase_04 AC 4 ms
6,452 KB
testcase_05 AC 4 ms
6,544 KB
testcase_06 AC 4 ms
6,540 KB
testcase_07 AC 4 ms
6,448 KB
testcase_08 AC 4 ms
6,492 KB
testcase_09 AC 222 ms
9,568 KB
testcase_10 AC 200 ms
11,528 KB
testcase_11 AC 217 ms
10,340 KB
testcase_12 AC 218 ms
10,860 KB
testcase_13 AC 228 ms
10,632 KB
testcase_14 AC 220 ms
10,596 KB
testcase_15 AC 218 ms
10,088 KB
testcase_16 AC 215 ms
10,336 KB
testcase_17 AC 216 ms
10,292 KB
testcase_18 AC 219 ms
10,288 KB
testcase_19 AC 217 ms
10,260 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <sstream>
#include <algorithm>
#include <functional>
#include <limits.h>
#include <bitset>

#include <tuple>
#include <unordered_map>

#define mp       make_pair
#define mt       make_tuple
#define pb       push_back
#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

typedef    long long          ll;
typedef    unsigned long long ull;
typedef    pair<int,int>      pii;

const int INF=1<<29;
const double EPS=1e-9;

const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};
int N;
vector<int> graph[100010];
queue<pii> que;
int from_child[100010];
int from_root[100010];
int main(){
  cin >> N;
  for (int i = 0; i < N - 1; i++){
    int a,b;
    cin >> a >> b;
    a--,b--;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  vector<int> child;
  for (int i = 0; i < N; i++){
    if (graph[i].size() == 1){
      que.push(mp(i, 0));
    }
  }
  memset(from_child, -1, sizeof(from_child));
  while (!que.empty()){
    pii pos = que.front();
    que.pop();
    if (from_child[pos.first] != -1)continue;
    from_child[pos.first] = pos.second;
    for (int i = 0; i < graph[pos.first].size(); i++){
      que.push(mp(graph[pos.first][i], pos.second + 1));
    }
  }
  while (!que.empty())que.pop();
  memset(from_root, -1, sizeof(from_root));
  que.push(mp(0, 0));
  while (!que.empty()){
    pii pos = que.front();
    que.pop();
    if (from_root[pos.first] != -1)continue;
    from_root[pos.first] = pos.second;
    for (int i = 0; i < graph[pos.first].size(); i++){
      que.push(mp(graph[pos.first][i], pos.second + 1));
    }
  }
  for (int i = 0; i < N; i++){
    cout << min(from_child[i], from_root[i]) << endl;
  }
  return 0;
}
0