結果

問題 No.277 根掘り葉掘り
ユーザー kzyKTkzyKT
提出日時 2015-09-04 22:38:36
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,864 bytes
コンパイル時間 1,334 ms
コンパイル使用メモリ 151,200 KB
実行使用メモリ 13,332 KB
最終ジャッジ日時 2023-09-26 05:44:43
合計ジャッジ時間 3,265 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 WA -
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 WA -
testcase_08 AC 2 ms
4,376 KB
testcase_09 WA -
testcase_10 AC 43 ms
12,488 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define all(c) (c).begin(),(c).end()
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define REP(i,m,n) for(int i=(int)(m);i<(int)(n);i++)
#define rep(i,n) REP(i,0,n)
#define iter(c) __typeof((c).begin())
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
#define mem(a) memset(a,0,sizeof(a))
#define pd(a) printf("%.10f\n",a)
#define pb(a) push_back(a)
#define in(a) insert(a)
#define pi M_PI
#define R cin>>
#define F first
#define S second
#define C class
#define ll long long
#define ln cout<<'\n'
#define _(_1,_2,_3,N,...)N
#define pr(...) _(__VA_ARGS__,pr3,pr2,pr1)(__VA_ARGS__)
template<C T>void pr1(T a){cout<<a;ln;}
template<C T,C T2>void pr2(T a,T2 b){cout<<a<<' '<<b;ln;}
template<C T,C T2,C T3>void pr3(T a,T2 b,T3 c){cout<<a<<' '<<b<<' '<<c;ln;}
template<C T>void PR(T a,int n){rep(i,n){if(i)cout<<' ';cout<<a[i];}ln;}
bool check(int n,int m,int x,int y){return x>=0&&x<n&&y>=0&&y<m;}
const ll MAX=1000000007,MAXL=1LL<<60,dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
typedef pair<int,int> P;

void Main() {
  int n;
  cin >> n;
  vector<int> v[n],v2[n];
  int c[n];
  mem(c);
  rep(i,n-1) {
    int x,y;
    cin >> x >> y;
    x--,y--;
    v[x].pb(y);
    v2[y].pb(x);
    c[x]++;
  }
  int d[n];
  fill(d,d+n,MAX);
  queue<int> que;
  rep(i,n) {
    if(!c[i]) {
      que.push(i);
      d[i]=0;
    }
  }
  while(!que.empty()) {
    int x=que.front();que.pop();
    rep(i,v2[x].size()) {
      int y=v2[x][i];
      d[y]=min(d[y],d[x]+1);
      c[y]--;
      if(!c[y]) que.push(y);
    }
  }
  d[0]=0;
  que.push(0);
  while(!que.empty()) {
    int x=que.front();que.pop();
    rep(i,v[x].size()) {
      int y=v[x][i];
      if(d[y]>d[x]+1) {
        d[y]=d[x]+1;
        que.push(y);
      }
    }
  }
  rep(i,n) pr(d[i]);
}

int main() {
  ios::sync_with_stdio(0);cin.tie(0);
  Main();return 0;
}
0