結果

問題 No.277 根掘り葉掘り
ユーザー 158b158b
提出日時 2015-09-05 01:16:32
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 225 ms / 3,000 ms
コード長 1,158 bytes
コンパイル時間 962 ms
コンパイル使用メモリ 82,976 KB
実行使用メモリ 9,880 KB
最終ジャッジ日時 2023-09-26 07:55:11
合計ジャッジ時間 4,995 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
5,680 KB
testcase_01 AC 4 ms
5,760 KB
testcase_02 AC 3 ms
5,608 KB
testcase_03 AC 3 ms
5,664 KB
testcase_04 AC 4 ms
5,612 KB
testcase_05 AC 4 ms
5,936 KB
testcase_06 AC 4 ms
5,612 KB
testcase_07 AC 4 ms
5,664 KB
testcase_08 AC 3 ms
5,948 KB
testcase_09 AC 216 ms
9,196 KB
testcase_10 AC 196 ms
9,880 KB
testcase_11 AC 220 ms
9,564 KB
testcase_12 AC 219 ms
9,368 KB
testcase_13 AC 225 ms
9,680 KB
testcase_14 AC 215 ms
9,540 KB
testcase_15 AC 217 ms
9,480 KB
testcase_16 AC 217 ms
9,408 KB
testcase_17 AC 213 ms
9,420 KB
testcase_18 AC 213 ms
9,480 KB
testcase_19 AC 214 ms
9,648 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <functional>
#include <string>
#include <climits>
#include <vector>
#include <numeric>
#include <complex>
#include <map>
#include <bitset>
#include <stack>
#include <queue>
using namespace std;

//#define __int64 long long
#define long __int64
#define REP(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)
const int Vecy[4] = {0,-1,0,1};
const int Vecx[4] = {1,0,-1,0};
const int modd = 1000000007;

int kyori[100050];
vector<int> node[100050];

int main(){
	int n;
	int in1,in2;
	int op;
	queue<int> q;
	
	cin >> n;
	
	//初期化
	for(int i=1; i<=n; i++){
		kyori[i] = INT_MAX * 0.99;
	}
	kyori[1] = 0;
	q.push(1);
	
	rep(i,n-1){
		cin >> in1 >> in2;
		node[in1].push_back(in2);
		node[in2].push_back(in1);
	}
	
	for(int i=1; i<=n; i++){
		if(node[i].size() == 1){
			kyori[i] = 0;
			q.push(i);
		}
	}
	
	while(!q.empty()){
		for(int i=0; i<node[ q.front() ].size(); i++){
			op = node[q.front()][i];
			if(kyori[op] > kyori[ q.front() ] + 1){
				kyori[op] = kyori[ q.front() ] + 1;
				q.push(op);
			}
		}
		q.pop();
	}
	
	for(int i=1; i<=n; i++){
		cout << kyori[i] << endl;
	}
	
    return 0;
}
0