結果

問題 No.277 根掘り葉掘り
コンテスト
ユーザー naimonon77
提出日時 2016-02-24 13:33:22
言語 C++11
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=gnu++11 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 1,543 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,204 ms
コンパイル使用メモリ 182,320 KB
実行使用メモリ 6,400 KB
最終ジャッジ日時 2026-04-10 19:08:43
合計ジャッジ時間 3,917 ms
ジャッジサーバーID
(参考情報)
judge2_1 / judge3_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample WA * 2
other AC * 2 WA * 5 RE * 11
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:72:10: warning: 'y' may be used uninitialized [-Wmaybe-uninitialized]
   72 |     scanf("%d%d",x,y);
      |     ~~~~~^~~~~~~~~~~~
main.cpp:71:11: note: 'y' declared here
   71 |     int x,y;
      |           ^
main.cpp:72:10: warning: 'x' may be used uninitialized [-Wmaybe-uninitialized]
   72 |     scanf("%d%d",x,y);
      |     ~~~~~^~~~~~~~~~~~
main.cpp:71:9: note: 'x' declared here
   71 |     int x,y;
      |         ^
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/vector:68,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/queue:69,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/x86_64-pc-linux-gnu/bits/stdc++.h:160,
                 from main.cpp:5:
In member function 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = int; _Alloc = std::allocator<int>]',
    inlined from 'int main()' at main.cpp:73:25:
/home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/stl_vector.h:1427:28: warning: 'y' may be used uninitialized [-Wmaybe-uninitialized]
 1427 |           _M_realloc_append(__x);
      |           ~~~~~~~~~~~~~~~~~^~~~~
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/vector:74:
/home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/vector.tcc: In function 'int main()':
/home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/vector.tcc:557:7: note: by argument 2 of type 'const int&' to 'void std::vector<_Tp, _Alloc>::_M_realloc_append(_Args&& ...) [with _Args = {const int&}; _Tp = int; _Alloc = std::allocator<int>]' declared here
  557 |       vector<_Tp, _Alloc>::
      |       ^~~~~~~~~~~~~~~~~~~
main.cpp:71:11: note: 'y' declared here
   71 |     int x,y;
      |           ^
In member function 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = int; _Alloc = std::allocator<int>]',
    inline

ソースコード

diff #
raw source code

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#ifdef __unix__
#include <bits/stdc++.h>
#else
#include "bits\stdc++.h"
#endif
#include <vector>
#include <set>
#include <string>
#include <queue>
#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

#define MAX 100005

int N;
vector<int> connect[MAX];
int ans[MAX] = {0,0};
bool isleaf[MAX] = {0};
int INF = 1 << 30;

void seek_depth() {
  int i;
  bool visited[MAX]={0};
  queue<int> qu;
  qu.push(1);
  visited[1] = true;
  while( not qu.empty() ) {
    int now = qu.front(); qu.pop();
    rep(i,connect[now].size()) {
      int next = connect[now][i];
      if( not visited[next] ) {
        visited[next] = true;
        ans[next] = ans[now]+1;
        qu.push(next);
      }
    }
  }
}

void seek_L(int leaf) {
  int i;
  queue<int> qu;
  ans[leaf] = 0;
  qu.push(leaf);
  while( not qu.empty() ) {
    int now = qu.front(); qu.pop();
    rep(i,connect[now].size()) {
      int next = connect[now][i];
      if(ans[next] > ans[now]+1) {
        ans[next] = ans[now]+1;
        qu.push(next);
      }
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int i,j,k;
  cin >> N;
  rep(i,N-1) {
    int x,y;
    scanf("%d%d",x,y);
    connect[x].push_back(y);
    connect[y].push_back(x);
  }
  seek_depth();
  for(i=1;i<N+1;i++) {
    if(connect[i].size() == 1) seek_L(i);
  }
  for(i=1;i<N+1;i++) {
    cout << ans[i] << endl;
  }
  
  return 0;
}
0