結果
| 問題 |
No.277 根掘り葉掘り
|
| コンテスト | |
| ユーザー |
hirokazu1020
|
| 提出日時 | 2015-09-04 23:16:47 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,253 bytes |
| コンパイル時間 | 925 ms |
| コンパイル使用メモリ | 98,000 KB |
| 実行使用メモリ | 15,616 KB |
| 最終ジャッジ日時 | 2024-07-19 01:54:24 |
| 合計ジャッジ時間 | 4,146 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 10 WA * 8 |
ソースコード
#include<sstream>
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<numeric>
#include<functional>
#include<algorithm>
#include<bitset>
#include<tuple>
#include<unordered_set>
#include<random>
using namespace std;
#define INF (1<<29)
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define all(v) v.begin(),v.end()
#define uniq(v) v.erase(unique(all(v)),v.end())
#define indexOf(v,x) (find(all(v),x)-v.begin())
int ans[100000];
int n;
vector<int> edge[100000];
int dfs1(int u,int par,int mind){
if(u!=0 && edge[u].size()==1)return ans[u]=0;
rep(i,edge[u].size()){
if(par==edge[u][i])continue;
mind=min(mind,dfs1(edge[u][i],u,mind+1)+1);
}
return ans[u]=min(ans[u],mind);
}
int dfs2(int u,int par,int mind){
if(u!=0 && edge[u].size()==1)return ans[u]=0;
for(int i=edge[u].size()-1;i>=0;i--){
if(par==edge[u][i])continue;
mind=min(mind,dfs1(edge[u][i],u,mind+1)+1);
}
return ans[u]=min(ans[u],mind);
}
int main() {
cin>>n;
rep(u,n-1){
int x,y;
cin>>x>>y;
x--;y--;
edge[x].push_back(y);
edge[y].push_back(x);
}
fill_n(ans,n,INF);
dfs1(0,-1,0);
dfs2(0,-1,0);
rep(i,n)cout<<ans[i]<<endl;
return 0;
}
hirokazu1020