結果

問題 No.277 根掘り葉掘り
ユーザー ishizuishizu
提出日時 2015-09-05 01:23:39
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 195 ms / 3,000 ms
コード長 1,337 bytes
コンパイル時間 1,293 ms
コンパイル使用メモリ 72,340 KB
実行使用メモリ 14,444 KB
最終ジャッジ日時 2023-09-26 07:56:04
合計ジャッジ時間 4,047 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
5,764 KB
testcase_01 AC 3 ms
5,896 KB
testcase_02 AC 3 ms
5,716 KB
testcase_03 AC 3 ms
5,720 KB
testcase_04 AC 4 ms
5,704 KB
testcase_05 AC 3 ms
5,856 KB
testcase_06 AC 4 ms
5,724 KB
testcase_07 AC 4 ms
5,796 KB
testcase_08 AC 4 ms
5,768 KB
testcase_09 AC 194 ms
14,444 KB
testcase_10 AC 167 ms
9,788 KB
testcase_11 AC 174 ms
9,700 KB
testcase_12 AC 195 ms
12,092 KB
testcase_13 AC 189 ms
9,868 KB
testcase_14 AC 183 ms
10,016 KB
testcase_15 AC 189 ms
10,380 KB
testcase_16 AC 185 ms
10,112 KB
testcase_17 AC 183 ms
9,988 KB
testcase_18 AC 186 ms
9,968 KB
testcase_19 AC 182 ms
9,968 KB
権限があれば一括ダウンロードができます

ソースコード

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];
bool a[100010];
int depth[100010];
int len[100010];
int dfs(int tar,int dep){
	depth[tar]=dep;
	a[tar]=true;
	int ans=INF;
	REP(i,to[tar].size()){
		if(!a[to[tar][i]]) ans=min(ans,dfs( to[tar][i], dep+1));
	}
	if(ans==INF) return len[tar]=0;
	return len[tar]=1+ans;
}
void dfs2(int tar,int par){
	a[tar]=true;
	len[tar] = min(len[par]+1,len[tar]);
	REP(i,to[tar].size()){
		if(!a[to[tar][i]]) 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);
		to[b].pb(a);
	}
// 	fill(len,len+n+1,INF);

	dfs(1,0);
// 	FOR(i,1,n+1) cout<<len[i]<<endl;
	fill(a,a+n+1,0);
	dfs2(1,1);
	FOR(i,1,n+1) cout<<min(len[i],depth[i])<<endl;
	 

}
0