結果

問題 No.277 根掘り葉掘り
ユーザー stack9996stack9996
提出日時 2015-09-05 00:28:17
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,254 bytes
コンパイル時間 656 ms
コンパイル使用メモリ 80,360 KB
実行使用メモリ 8,760 KB
最終ジャッジ日時 2023-09-26 07:34:45
合計ジャッジ時間 5,597 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,444 KB
testcase_01 AC 4 ms
6,452 KB
testcase_02 AC 4 ms
6,440 KB
testcase_03 AC 4 ms
6,728 KB
testcase_04 AC 5 ms
6,488 KB
testcase_05 AC 4 ms
6,544 KB
testcase_06 AC 4 ms
6,612 KB
testcase_07 AC 4 ms
6,556 KB
testcase_08 AC 3 ms
6,480 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 <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