結果

問題 No.277 根掘り葉掘り
ユーザー nanasilinanasili
提出日時 2015-09-18 14:25:51
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 116 ms / 3,000 ms
コード長 1,483 bytes
コンパイル時間 705 ms
コンパイル使用メモリ 85,596 KB
実行使用メモリ 12,968 KB
最終ジャッジ日時 2023-09-26 12:25:51
合計ジャッジ時間 3,522 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,264 KB
testcase_01 AC 3 ms
6,400 KB
testcase_02 AC 4 ms
6,388 KB
testcase_03 AC 4 ms
6,504 KB
testcase_04 AC 4 ms
6,272 KB
testcase_05 AC 4 ms
6,340 KB
testcase_06 AC 4 ms
6,504 KB
testcase_07 AC 4 ms
6,336 KB
testcase_08 AC 4 ms
6,264 KB
testcase_09 AC 72 ms
12,968 KB
testcase_10 AC 55 ms
10,260 KB
testcase_11 AC 87 ms
9,964 KB
testcase_12 AC 116 ms
11,420 KB
testcase_13 AC 107 ms
10,312 KB
testcase_14 AC 100 ms
10,276 KB
testcase_15 AC 108 ms
10,284 KB
testcase_16 AC 107 ms
10,216 KB
testcase_17 AC 102 ms
10,172 KB
testcase_18 AC 109 ms
10,236 KB
testcase_19 AC 108 ms
10,060 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <vector>
#include <cfloat>
#include <string>
#include <cmath>
#include <set>
#include <cstdlib>
#include <map>
#include <ctime>
#include <iomanip>
#include <functional>
#include <deque>
#include <iostream>
#include <cstring>
#include <queue>
#include <cstdio>
#include <stack>
#include <climits>
#include <sys/time.h>
#include <cctype>

using namespace std;

typedef long long ll;

#define MAX_V 100000

int n;
vector <int> graph[MAX_V+1];
int memo1[MAX_V+1], memo2[MAX_V+1];
bool vis[MAX_V+1];
void rec1(int x, int t) {
  memo1[x] = t;
  vis[x] = true;
  for (int i = 0; i < graph[x].size(); i++) {
    int p = graph[x][i];
    if (vis[p]) continue;
    rec1(p, t+1);
  }
}

void rec2(int x, int t) {
  for (int i = 0; i < graph[x].size(); i++) {
    int p = graph[x][i];
    if (memo2[p] == -1 || memo2[p] > t+1) {
      memo2[p] = t+1;
      rec2(p, t+1);
    }
  }
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n-1; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    a--; b--;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  memset(vis, false, sizeof(vis));
  rec1(0, 0);
  for (int i = 0; i < n; i++) {
    if (graph[i].size() == 1) {
      graph[i].push_back(n);
      graph[n].push_back(i);
    }
  }

  memset(vis, false, sizeof(vis));
  memset(memo2, -1, sizeof(memo2));
  memo2[n] = 0;
  rec2(n, -1);
  for (int i = 0; i < n; i++) {
    printf("%d\n", min(memo1[i], memo2[i]));
    // printf("%d\n", memo2[i]);
  }

}
0