結果

問題 No.277 根掘り葉掘り
ユーザー 158b158b
提出日時 2015-09-05 01:02:42
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,292 bytes
コンパイル時間 840 ms
コンパイル使用メモリ 81,296 KB
実行使用メモリ 9,132 KB
最終ジャッジ日時 2023-09-26 07:51:40
合計ジャッジ時間 5,762 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
9,132 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 WA -
testcase_08 AC 2 ms
4,380 KB
testcase_09 TLE -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

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];
int from[100050];
int to[100050];
bool leaf[100050];


int main(){
	int n;
	queue<int> q;
	cin >> n;
	
	//初期化
	for(int i=1; i<=n; i++){
		kyori[i] = INT_MAX / 2;
		leaf[i] = true;
	}
	kyori[1] = 0;
	q.push(1);
	
	rep(i,n-1){
		cin >> from[i] >> to[i];
		leaf[from[i]] = false;
	}
	
	for(int i=1; i<=n; i++){
		if(leaf[i]){
			kyori[i] = 0;
			q.push(i);
		}
	}
	
	while(!q.empty()){
		for(int i=0; i<n-1; i++){
			if(to[i] == q.front()){
				if(kyori[from[i]] > kyori[ q.front() ] + 1){
					kyori[from[i]] = kyori[ q.front() ] + 1;
					q.push(from[i]);
				}
			}
			if(from[i] == q.front()){
				if(kyori[to[i]] > kyori[ q.front() ] + 1){
					kyori[to[i]] = kyori[ q.front() ] + 1;
					q.push(to[i]);
				}
			}
		}
		q.pop();
	}
	
	for(int i=1; i<=n; i++){
		cout << kyori[i] << endl;
	}
	
    return 0;
}
0