結果

問題 No.277 根掘り葉掘り
ユーザー tnoda_tnoda_
提出日時 2015-09-08 17:57:18
言語 C++11
(gcc 13.3.0)
結果
AC  
実行時間 1,311 ms / 3,000 ms
コード長 1,771 bytes
コンパイル時間 734 ms
コンパイル使用メモリ 93,424 KB
実行使用メモリ 20,148 KB
最終ジャッジ日時 2024-07-19 04:59:37
合計ジャッジ時間 10,531 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include <cstdio>
#include <cstring>
#include <cmath>
#include <climits>
#include <iostream>
#include <iomanip>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <algorithm>
#include <numeric>
#include <functional>
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
#define ALL(a) (a).begin(),(a).end()
using namespace std;
typedef long long ll;
int N;
const int MAX_N = 500010;
vector<int> G[MAX_N];
queue<int> q;
int depth_0[MAX_N];
int depth_1[MAX_N];
bool leaves[MAX_N];
bool visited[MAX_N];
void bfs_0() {
depth_0[1] = 0;
visited[1] = true;
q.push(1);
while (!q.empty()) {
int v = q.front(); q.pop();
bool is_leaf = true;
for (int u : G[v]) {
if (visited[u]) continue;
is_leaf = false;
visited[u] = true;
depth_0[u] = depth_0[v] + 1;
q.push(u);
}
leaves[v] = is_leaf;
}
}
void bfs_1(int s) {
memset(visited, 0, sizeof(visited));
depth_1[s] = 0;
visited[s] = true;
q.push(s);
while (!q.empty()) {
int v = q.front(); q.pop();
if (depth_1[v] >= depth_0[v]) continue;
depth_0[v] = depth_1[v];
for (int u : G[v]) {
if (visited[u]) continue;
visited[u] = true;
depth_1[u] = depth_1[v] + 1;
q.push(u);
}
}
}
int main(int argc, char *argv[])
{
cin.tie(0);
ios::sync_with_stdio(false);
cin >> N;
REP(i,N-1) {
int x, y;
cin >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
}
bfs_0();
REP(i,MAX_N) if (leaves[i]) bfs_1(i);
REP(i,N) cout << depth_0[i+1] << '\n';
cout << flush;
return 0;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0