結果
| 問題 |
No.277 根掘り葉掘り
|
| コンテスト | |
| ユーザー |
airis
|
| 提出日時 | 2015-09-05 00:30:28 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,661 bytes |
| コンパイル時間 | 633 ms |
| コンパイル使用メモリ | 83,188 KB |
| 実行使用メモリ | 17,536 KB |
| 最終ジャッジ日時 | 2024-07-19 02:47:13 |
| 合計ジャッジ時間 | 5,714 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 8 TLE * 1 -- * 9 |
ソースコード
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <numeric>
#include <bitset>
#include <complex>
#define rep(x, to) for (int x = 0; x < (to); x++)
#define REP(x, a, to) for (int x = (a); x < (to); x++)
#define foreach(itr, x) for (typeof((x).begin()) itr = (x).begin(); itr != (x).end(); itr++)
#define EPS (1e-14)
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef complex<double> Complex;
typedef vector< vector<int> > Mat;
const int INF = (int)(1e+9 + 7);
const int MAX_V = 100005;
int N;
int L[MAX_V], R[MAX_V];
bool leaf[MAX_V];
vector<int> edge[MAX_V];
int dfs(int u, int prev, int depth) {
R[u] = depth;
rep(i, edge[u].size()) {
int v = edge[u][i];
if (v == prev) continue;
L[u] = min(L[u], 1 + dfs(v, u, depth + 1));
}
if (INF == L[u]) {
L[u] = 0;
leaf[u] = true;
}
return L[u];
}
int dfs2(int u, int prev, int depth) {
bool is_leaf = true;
rep(i, edge[u].size()) {
int v = edge[u][i];
if (v == prev) continue;
L[u] = min(L[u], 1 + dfs2(v, u, depth + 1));
is_leaf = false;
}
if (is_leaf) L[u] = 0;
return L[u];
}
void solve() {
rep(i, MAX_V) L[i] = INF;
dfs(1, -1, 0);
rep(i, N) {
if (leaf[i + 1]) {
dfs2(i + 1, -1, 0);
}
}
rep(i, N) {
cout << min(R[i + 1], L[i + 1]) << endl;
//cout << R[i + 1] << " " << L[i + 1] << endl;
}
}
int main() {
cin >> N;
rep(i, N - 1) {
int x, y;
cin>> x >> y;
edge[x].push_back(y);
edge[y].push_back(x);
}
solve();
return 0;
}
airis