結果
問題 | No.277 根掘り葉掘り |
ユーザー |
|
提出日時 | 2016-03-06 22:46:17 |
言語 | C++11 (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 69 ms / 3,000 ms |
コード長 | 1,240 bytes |
コンパイル時間 | 1,464 ms |
コンパイル使用メモリ | 165,764 KB |
実行使用メモリ | 10,592 KB |
最終ジャッジ日時 | 2024-09-24 14:56:24 |
合計ジャッジ時間 | 4,201 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 18 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:22:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 22 | scanf(" %d",&n); | ~~~~~^~~~~~~~~~ main.cpp:26:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 26 | scanf(" %d %d",&x,&y); | ~~~~~^~~~~~~~~~~~~~~~
ソースコード
#include <bits/stdc++.h>using namespace std;typedef long long ll;#define rep(i,n) for(i=0;i<n;++i)#define each(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); ++itr)#define mp make_pair#define pb push_back#define fi first#define sc secondconst int N=100000;const int INF=12345678;vector<int> G[N];int main(){int i;int n;scanf(" %d",&n);rep(i,n-1){int x,y;scanf(" %d %d",&x,&y);--x;--y;G[x].pb(y);G[y].pb(x);}int r[N],l[N];fill(r,r+n,INF);fill(l,l+n,INF);bool leaf[N];fill(leaf,leaf+n,false);queue<int> q;//まず根からBFSq.push(0);r[0]=0;while(!q.empty()){int v=q.front();q.pop();bool isLeaf=true;rep(i,G[v].size()){if(r[G[v][i]]>r[v]+1){r[G[v][i]]=r[v]+1;q.push(G[v][i]);isLeaf=false;}}leaf[v]=isLeaf;}/*rep(i,n) printf(" r[%d] = %d\n",i,r[i]);rep(i,n) printf(" leaf[%d] = %d\n",i,leaf[i]);*///次に葉からBFSrep(i,n){if(leaf[i]){q.push(i);l[i]=0;}}while(!q.empty()){int v=q.front();q.pop();rep(i,G[v].size()){if(l[G[v][i]]>l[v]+1){l[G[v][i]]=l[v]+1;q.push(G[v][i]);}}}rep(i,n) printf("%d\n", min(r[i],l[i]));}