結果

問題 No.277 根掘り葉掘り
ユーザー TawaraTawara
提出日時 2015-09-05 02:15:30
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 224 ms / 3,000 ms
コード長 838 bytes
コンパイル時間 514 ms
コンパイル使用メモリ 66,816 KB
実行使用メモリ 12,760 KB
最終ジャッジ日時 2023-09-26 08:30:42
合計ジャッジ時間 4,261 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
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,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 214 ms
8,880 KB
testcase_10 AC 196 ms
12,760 KB
testcase_11 AC 215 ms
9,768 KB
testcase_12 AC 217 ms
10,860 KB
testcase_13 AC 222 ms
11,016 KB
testcase_14 AC 214 ms
10,736 KB
testcase_15 AC 214 ms
10,508 KB
testcase_16 AC 224 ms
10,584 KB
testcase_17 AC 223 ms
10,816 KB
testcase_18 AC 221 ms
10,548 KB
testcase_19 AC 214 ms
10,724 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>

using namespace std;

typedef vector <int> VI;
typedef vector <VI> VVI;

#define range(i,a,b) for(int i=(a); i < (b); i++)
#define rep(i,n) range(i,0,n)
#define each(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)

int main(){
	int N,x,y,d=0,len;
	VVI e;
	VI S,b,h,nb,nh;
	cin >> N;
	e = VVI(N);
	S = VI(N,N);
	rep(i,N-1){
		cin >> x >> y;
		x--; y--;
		e[x].push_back(y);
		e[y].push_back(x);
	}
	b.push_back(0); h.push_back(0);
	rep(i,N) if(e[i].size() == 1){b.push_back(i); h.push_back(i);}
	
	while ((len=b.size()) > 0){
		nb.clear(); nh.clear();
		rep(i,len){
			if(d >= S[h[i]]) continue;
			S[h[i]] = d;
			each(nxt, e[h[i]]){
				if(*nxt == b[i]) continue;
				nb.push_back(h[i]); nh.push_back(*nxt);
			}
		}
		d++; b=nb; h=nh;
	}
	rep(i,N) cout << S[i] << endl;
	return 0;
}
0