結果
問題 | No.277 根掘り葉掘り |
ユーザー |
![]() |
提出日時 | 2015-09-05 11:54:11 |
言語 | C++11 (gcc 8.5.0) |
結果 |
AC
|
実行時間 | 212 ms / 3,000 ms |
コード長 | 1,901 bytes |
コンパイル時間 | 779 ms |
使用メモリ | 11,328 KB |
最終ジャッジ日時 | 2023-02-17 06:02:16 |
合計ジャッジ時間 | 4,650 ms |
ジャッジサーバーID (参考情報) |
judge15 / judge14 |
テストケース
テストケース表示入力 | 結果 | 実行時間 使用メモリ |
---|---|---|
testcase_00 | AC | 3 ms
9,672 KB |
testcase_01 | AC | 3 ms
9,680 KB |
testcase_02 | AC | 3 ms
7,592 KB |
testcase_03 | AC | 4 ms
7,608 KB |
testcase_04 | AC | 3 ms
7,596 KB |
testcase_05 | AC | 4 ms
7,652 KB |
testcase_06 | AC | 4 ms
7,684 KB |
testcase_07 | AC | 4 ms
9,692 KB |
testcase_08 | AC | 3 ms
9,704 KB |
testcase_09 | AC | 206 ms
9,780 KB |
testcase_10 | AC | 187 ms
11,328 KB |
testcase_11 | AC | 208 ms
10,348 KB |
testcase_12 | AC | 206 ms
10,988 KB |
testcase_13 | AC | 212 ms
10,756 KB |
testcase_14 | AC | 205 ms
10,668 KB |
testcase_15 | AC | 203 ms
10,268 KB |
testcase_16 | AC | 208 ms
10,368 KB |
testcase_17 | AC | 206 ms
10,180 KB |
testcase_18 | AC | 201 ms
10,176 KB |
testcase_19 | AC | 203 ms
10,424 KB |
ソースコード
#include <iostream> #include <map> #include <set> #include <list> #include <cmath> #include <queue> #include <stack> #include <cstdio> #include <string> #include <vector> #include <complex> #include <cstdlib> #include <cstring> #include <numeric> #include <sstream> #include <algorithm> #include <functional> #include <limits.h> #include <bitset> #include <tuple> #include <unordered_map> #define mp make_pair #define mt make_tuple #define pb push_back #define rep(i,n) for(int i=0;i<(n);i++) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> pii; const int INF=1<<29; const double EPS=1e-9; const int dx[]={1,0,-1,0},dy[]={0,-1,0,1}; int N; vector<int> graph[100010]; queue<pii> que; int from_child[100010]; int from_root[100010]; int main(){ cin >> N; for (int i = 0; i < N - 1; i++){ int a,b; cin >> a >> b; a--,b--; graph[a].push_back(b); graph[b].push_back(a); } vector<int> child; for (int i = 0; i < N; i++){ if (graph[i].size() == 1){ que.push(mp(i, 0)); } } memset(from_child, -1, sizeof(from_child)); while (!que.empty()){ pii pos = que.front(); que.pop(); if (from_child[pos.first] != -1)continue; from_child[pos.first] = pos.second; for (int i = 0; i < graph[pos.first].size(); i++){ que.push(mp(graph[pos.first][i], pos.second + 1)); } } while (!que.empty())que.pop(); memset(from_root, -1, sizeof(from_root)); que.push(mp(0, 0)); while (!que.empty()){ pii pos = que.front(); que.pop(); if (from_root[pos.first] != -1)continue; from_root[pos.first] = pos.second; for (int i = 0; i < graph[pos.first].size(); i++){ que.push(mp(graph[pos.first][i], pos.second + 1)); } } for (int i = 0; i < N; i++){ cout << min(from_child[i], from_root[i]) << endl; } return 0; }