結果
問題 | No.277 根掘り葉掘り |
ユーザー |
|
提出日時 | 2016-12-10 22:29:08 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 199 ms / 3,000 ms |
コード長 | 1,715 bytes |
コンパイル時間 | 1,674 ms |
コンパイル使用メモリ | 174,524 KB |
実行使用メモリ | 17,280 KB |
最終ジャッジ日時 | 2024-11-29 02:14:32 |
合計ジャッジ時間 | 4,823 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 18 |
ソースコード
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector< int > vi; typedef vector< vi > vvi; typedef vector< ll > vl; typedef vector< vl > vvl; typedef pair< int, int > pii; typedef vector< pii > vp; typedef vector< double > vd; typedef vector< vd > vvd; typedef vector< string > vs; template< class T1, class T2 > int upmin( T1 &x, T2 v ){ if( x > v ){ x = v; return 1; } return 0; } template< class T1, class T2 > int upmax( T1 &x, T2 v ){ if( x < v ){ x = v; return 1; } return 0; } const int INF = 0x3f3f3f3f; int N; vvi G; void init(){ cin >> N; G = vvi( N ); for( int i = 0; i < N - 1; ++i ){ int x, y; cin >> x >> y; G[ x - 1 ].emplace_back( y - 1 ); G[ y - 1 ].emplace_back( x - 1 ); } } vi rtdis, lfdis; void root_dfs( int u, int fa, int dis ){ rtdis[ u ] = dis; for( int v : G[ u ] ){ if( v == fa ) continue; root_dfs( v, u, dis + 1 ); } } void leaf_dfs( int u, int fa ){ lfdis[ u ] = INF; for( int v : G[ u ] ) if( v != fa ) leaf_dfs( v, u ), upmin( lfdis[ u ], lfdis[ v ] + 1 ); if( lfdis[ u ] == INF ) lfdis[ u ] = 0; } void neighbor_leaf_dfs( int u, int fa, int d ){ upmin( lfdis[ u ], d ); int minv = d; for( int v : G[ u ] ) if( v != fa ) upmin( minv, lfdis[ v ] + 1 ); for( int v : G[ u ] ) if( v != fa ) neighbor_leaf_dfs( v, u, minv + 1 ); } void preprocess(){ rtdis = lfdis = vi( N ); root_dfs( 0, -1, 0 ); leaf_dfs( 0, -1 ); neighbor_leaf_dfs( 0, -1, 0 ); } void solve(){ for( int i = 0; i < N; ++i ) cout << min( rtdis[ i ], lfdis[ i ] ) << endl; } signed main(){ ios::sync_with_stdio( 0 ); init(); preprocess(); solve(); return 0; }