結果
問題 |
No.277 根掘り葉掘り
|
ユーザー |
|
提出日時 | 2015-09-10 16:14:39 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 49 ms / 3,000 ms |
コード長 | 1,049 bytes |
コンパイル時間 | 688 ms |
コンパイル使用メモリ | 73,340 KB |
実行使用メモリ | 9,852 KB |
最終ジャッジ日時 | 2024-07-19 05:12:46 |
合計ジャッジ時間 | 2,072 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 18 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:14:15: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 14 | int n; scanf("%d", &n); | ~~~~~^~~~~~~~~~ main.cpp:17:20: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 17 | int x, y; scanf("%d%d", &x, &y); | ~~~~~^~~~~~~~~~~~~~~~
ソースコード
#include <iostream> #include <algorithm> #include <queue> using namespace std; using i64 = long long; class range {private: struct I{int x;int operator*(){return x;}bool operator!=(I& lhs){return x<lhs.x;}void operator++(){++x;}};I i,n; public:range(int n):i({0}),n({n}){}range(int i,int n):i({i}),n({n}){}I& begin(){return i;}I& end(){return n;}}; const int inf = 987654321; vector<vector<int>> G; int main(void) { int n; scanf("%d", &n); G.assign(n, vector<int>()); for(int i : range(n-1)) { int x, y; scanf("%d%d", &x, &y); x--, y--; G[x].push_back(y); G[y].push_back(x); } vector<int> dist(n, inf); dist[0] = 0; queue<int> que; que.push(0); for(int v : range(1, n)) { if(G[v].size() == 1) { dist[v] = 0; que.push(v); } } while(!que.empty()) { int v = que.front(); que.pop(); for(int vv : G[v]) { if(dist[vv] > dist[v] + 1) { dist[vv] = dist[v] + 1; que.push(vv); } } } for(int v : range(n)) { printf("%d\n", dist[v]); } return 0; }