結果

問題 No.277 根掘り葉掘り
ユーザー CleyLCleyL
提出日時 2024-02-12 10:45:08
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,139 bytes
コンパイル時間 1,657 ms
コンパイル使用メモリ 136,320 KB
実行使用メモリ 17,792 KB
最終ジャッジ日時 2024-02-12 10:45:14
合計ジャッジ時間 5,709 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 1 ms
6,548 KB
testcase_02 WA -
testcase_03 AC 2 ms
6,548 KB
testcase_04 AC 2 ms
6,548 KB
testcase_05 AC 2 ms
6,548 KB
testcase_06 AC 2 ms
6,548 KB
testcase_07 AC 2 ms
6,548 KB
testcase_08 AC 2 ms
6,548 KB
testcase_09 AC 230 ms
17,792 KB
testcase_10 AC 186 ms
10,380 KB
testcase_11 AC 205 ms
9,984 KB
testcase_12 AC 210 ms
13,824 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #


//yukicoder@cpp17

#include <iostream>
#include <iomanip>

#include <algorithm>

#include <cmath>
#include <cctype>
#include <climits>
#include <cassert>

#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <map>

#include <random>

#include <bitset>

#include <complex>

#include <utility>

#include <numeric>

#include <functional>


using namespace std;
using ll = long long;
using P = pair<ll,ll>;

const ll MOD = 998244353;
const ll MODx = 1000000007;
const int INF = (1<<30)-1;
const ll LINF = (1LL<<62LL)-1;
const double EPS = (1e-10);

P ar4[4] = {{0,1},{0,-1},{1,0},{-1,0}};
P ar8[8] = {{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};




template <typename T> vector<T> make_vector(size_t a, T b) { return vector<T>(a, b); }
template <typename... Ts> auto make_vector(size_t a, Ts... ts) { return vector<decltype(make_vector(ts...))>(a, make_vector(ts...)); }
 
/*
確認ポイント
cout << fixed << setprecision(n) << 小数計算//n桁の小数表記になる

計算量は変わらないが楽できるシリーズ
min(max)_element(iter,iter)で一番小さい(大きい)値のポインタが帰ってくる
count(iter,iter,int)でintがiterからiterの間にいくつあったかを取得できる
*/

/* comment outed because can cause bugs
__attribute__((constructor))
void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}
*/

vector<vector<int>> A;
vector<int> vis;
vector<pair<int,int>> ans;
int dfs(int n, int r = 0){
  int fromLeaf = 0;
  for(int i = 0; A[n].size() > i; i++){
    if(!vis[A[n][i]]){
      vis[A[n][i]] = 1;
      if(fromLeaf) fromLeaf = min(fromLeaf, dfs(A[n][i], r+1));
      else fromLeaf = dfs(A[n][i], r+1);
    }
  }
  ans[n].first = r;
  ans[n].second = fromLeaf;
  return fromLeaf+1;

}

int main(){
  int n;cin>>n;
  A.assign(n, vector<int>());
  vis.assign(n, 0);
  ans.assign(n, {-1,-1});
  for(int i = 0; n-1 > i; i++){
    int u,v;cin>>u>>v;u--;v--;
    A[u].push_back(v);
    A[v].push_back(u);
  }

  vis[0] = 1;
  dfs(0);
  for(int i = 0; n > i; i++){
    cout << min(ans[i].first, ans[i].second)<< endl;
  }
  return 0;
}
0