結果

問題 No.277 根掘り葉掘り
ユーザー どららどらら
提出日時 2015-09-27 03:15:09
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,154 bytes
コンパイル時間 604 ms
コンパイル使用メモリ 76,952 KB
実行使用メモリ 12,796 KB
最終ジャッジ日時 2023-09-26 16:42:11
合計ジャッジ時間 8,822 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,092 KB
testcase_01 AC 4 ms
6,100 KB
testcase_02 AC 4 ms
6,408 KB
testcase_03 AC 4 ms
6,092 KB
testcase_04 AC 3 ms
6,112 KB
testcase_05 WA -
testcase_06 AC 4 ms
6,104 KB
testcase_07 AC 4 ms
6,144 KB
testcase_08 AC 4 ms
6,208 KB
testcase_09 AC 214 ms
12,796 KB
testcase_10 AC 186 ms
9,612 KB
testcase_11 WA -
testcase_12 AC 251 ms
10,796 KB
testcase_13 AC 300 ms
9,788 KB
testcase_14 AC 2,622 ms
9,856 KB
testcase_15 AC 838 ms
10,100 KB
testcase_16 AC 443 ms
9,800 KB
testcase_17 AC 928 ms
9,876 KB
testcase_18 AC 395 ms
9,820 KB
testcase_19 AC 281 ms
9,952 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <iostream>
#include <queue>
#include <list>
#include <stack>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;

#define INF 10000000

int num;
vector<int> G[100005];
int ret[100005];
int visited[100005];

void dfs(int x, int r){
  visited[x] = num;
  if(ret[x] < r) return;
  ret[x] = min(ret[x], r);
  
  int cnt = 0;
  rep(i,G[x].size()){
    if(visited[G[x][i]] > num-1) continue;
    dfs(G[x][i], r+1);
    cnt++;
  }
}

int main(){
  int N;
  cin >> N;
  rep(i,N-1){
    int x, y;
    cin >> x >> y;
    G[x].push_back(y);
    G[y].push_back(x);
  }

  rep(i,100005) ret[i] = INF;
  
  dfs(1,0);
  num++;
  rep(i,N+1){
    if(G[i].size() == 1){
      dfs(i,0);
      num++;
    }
  }

  REP(i,1,N+1){
    cout << ret[i] << endl;
  }
  
  return 0;
}
0