結果

問題 No.277 根掘り葉掘り
ユーザー ishizuishizu
提出日時 2015-09-05 00:54:47
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,195 bytes
コンパイル時間 533 ms
コンパイル使用メモリ 72,496 KB
実行使用メモリ 8,264 KB
最終ジャッジ日時 2023-09-26 07:51:22
合計ジャッジ時間 3,474 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,712 KB
testcase_01 AC 3 ms
5,768 KB
testcase_02 AC 3 ms
5,692 KB
testcase_03 AC 4 ms
5,724 KB
testcase_04 AC 3 ms
5,692 KB
testcase_05 AC 4 ms
5,748 KB
testcase_06 AC 4 ms
5,852 KB
testcase_07 WA -
testcase_08 AC 3 ms
5,692 KB
testcase_09 WA -
testcase_10 AC 144 ms
6,712 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<iostream>
#include<cstdio>
#include<vector>
#include<queue>

#include<map>
#include<set>
#include<string>
#include<algorithm>
#include<functional>
using namespace std;
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
#define INF 1<<30
#define MP make_pair
#define mp make_pair
#define pb push_back
#define PB push_back
#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define ll long long
#define ull unsigned long long
int n;
vector<int> to[100010];
int a[100010];
int depth[100010];
int len[100010];
int dfs(int tar,int dep,int par){
	depth[tar]=dep;
	int ans=INF;
	REP(i,to[tar].size()){
		ans=min(ans,dfs( to[tar][i], dep+1,tar));
	}
	if(ans==INF) return len[tar]=0;
	len[tar]= min(len[par],1+ans);
	return len[tar];
}
void dfs2(int tar,int par){
	REP(i,to[tar].size()){
		dfs2(to[tar][i],tar);
	}
	len[tar] = min(len[par]+1,len[tar]);
	
}
int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cin>>n;
	REP(i,n-1){
		int a,b;cin>>a>>b;
		to[a].pb(b);
	}
	fill(len,len+n+1,INF);
	dfs(1,0,1);
	dfs2(1,1);
	FOR(i,1,n+1) cout<<min(len[i],depth[i])<<endl;
	 

}
0