結果

問題 No.277 根掘り葉掘り
ユーザー TawaraTawara
提出日時 2015-09-05 02:42:33
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 215 ms / 3,000 ms
コード長 801 bytes
コンパイル時間 591 ms
コンパイル使用メモリ 74,508 KB
実行使用メモリ 10,992 KB
最終ジャッジ日時 2023-09-26 08:32:18
合計ジャッジ時間 4,131 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,504 KB
testcase_09 AC 210 ms
8,856 KB
testcase_10 AC 190 ms
10,992 KB
testcase_11 AC 210 ms
9,744 KB
testcase_12 AC 210 ms
10,232 KB
testcase_13 AC 215 ms
10,448 KB
testcase_14 AC 208 ms
10,204 KB
testcase_15 AC 210 ms
9,928 KB
testcase_16 AC 211 ms
10,108 KB
testcase_17 AC 209 ms
9,960 KB
testcase_18 AC 212 ms
9,756 KB
testcase_19 AC 208 ms
10,024 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <queue>
#include <map>
using namespace std;

typedef vector <int> VI;
typedef vector <VI> VVI;
typedef pair <int,int> PII;
#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,h;
	PII tmp;
	VVI e;
	VI S;
	queue <PII> n;
	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);
	}
	n.push(PII(0,0));
	rep(i,N) if(e[i].size() == 1) n.push(PII(0,i));

	while(!n.empty()){
		tmp = n.front();
		d = tmp.first; h = tmp.second;
		n.pop();
		if(d >= S[h]) continue;
		S[h] = d;
		each(nxt, e[h]) n.push(PII(d+1,*nxt));
	}
	rep(i,N) cout << S[i] << endl;
	return 0;
}
0