結果

問題 No.277 根掘り葉掘り
ユーザー 158b
提出日時 2015-09-05 01:16:32
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 226 ms / 3,000 ms
コード長 1,158 bytes
コンパイル時間 766 ms
コンパイル使用メモリ 88,372 KB
実行使用メモリ 9,860 KB
最終ジャッジ日時 2024-07-19 03:01:21
合計ジャッジ時間 4,253 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

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