結果

問題 No.277 根掘り葉掘り
ユーザー stack9996
提出日時 2015-09-05 00:45:04
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 212 ms / 3,000 ms
コード長 1,101 bytes
コンパイル時間 1,300 ms
コンパイル使用メモリ 79,812 KB
実行使用メモリ 9,724 KB
最終ジャッジ日時 2024-07-19 02:50:55
合計ジャッジ時間 4,246 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <numeric>
#include <algorithm>
#include <string>
#include <vector>
#include <functional>
#include <cmath>
#include <queue>
#include <set>

#define rep(i, a) REP(i, 0, a)
#define REP(i, a, b) for(int i = a; i < b; ++i)

typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int, int> P;
typedef std::pair<P, int> PP;
struct edge{ int to, time, cost; };
const double esp = 1e-9;
const int inf = (int)1e+9;

int n;
std::vector<int> node[100001];
int cost[100001];

int main(){
	std::cin >> n;
	rep(i, n)cost[i] = inf;
	rep(i, n - 1){
		int x, y;
		std::cin >> x >> y;
		node[x - 1].push_back(y - 1);
		node[y - 1].push_back(x - 1);
	}

	std::queue<int> que;
	que.push(0);
	cost[0] = 0;
	REP(i, 1, n){
		if (node[i].size() == 1){
			cost[i] = 0;
			que.push(i);
		}
	}

	while (!que.empty()){
		int p = que.front();
		que.pop();
		int s = node[p].size();
		rep(i, s){
			int to = node[p][i];
			if (cost[to] > cost[p] + 1){
				cost[to] = cost[p] + 1;
				que.push(to);
			}
		}
	}

	rep(i, n)std::cout << cost[i] << std::endl;
	return 0;
}
0