結果

問題 No.277 根掘り葉掘り
ユーザー stack9996
提出日時 2015-09-05 00:28:17
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
TLE  
実行時間 -
コード長 1,254 bytes
コンパイル時間 613 ms
コンパイル使用メモリ 81,348 KB
実行使用メモリ 16,128 KB
最終ジャッジ日時 2024-07-19 02:42:28
合計ジャッジ時間 5,322 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 7 TLE * 1 -- * 10
権限があれば一括ダウンロードができます

ソースコード

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;

struct Node{
	int ans = inf;
	bool used = false;
	std::vector<Node*> v;
};
typedef std::pair<Node*, int> MP;

int n;
Node node[100001];

void updata(Node *n){
	std::queue<MP> que;
	que.push(MP(n, 0));
	while (!que.empty()){
		MP mp = que.front();
		Node* p = mp.first;
		que.pop();
		p->used = true;
		p->ans = std::min(p->ans, mp.second);
		int s = p->v.size();
		rep(i, s){
			if (!p->v[i]->used)que.push(MP(p->v[i], mp.second + 1));
		}
	}
}

int main(){
	std::cin >> n;
	rep(i, n - 1){
		int x, y;
		std::cin >> x >> y;
		node[x - 1].v.push_back(&node[y - 1]);
		node[y - 1].v.push_back(&node[x - 1]);
	}
	updata(&node[0]);
	rep(i, n){
		rep(j, n)node[j].used = false;
		if (node[i].v.size() == 1)updata(&node[i]);
	}

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