結果

問題 No.1752 Up-Down Tree
ユーザー tnakao0123tnakao0123
提出日時 2021-11-26 16:39:51
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,223 bytes
コンパイル時間 371 ms
コンパイル使用メモリ 51,872 KB
実行使用メモリ 10,892 KB
最終ジャッジ日時 2023-09-11 23:37:51
合計ジャッジ時間 4,390 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 68 ms
10,792 KB
testcase_02 AC 39 ms
10,036 KB
testcase_03 AC 38 ms
10,040 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 51 ms
10,828 KB
testcase_07 AC 52 ms
10,824 KB
testcase_08 AC 34 ms
9,100 KB
testcase_09 AC 54 ms
10,668 KB
testcase_10 AC 2 ms
5,676 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 3 ms
5,672 KB
testcase_21 AC 2 ms
5,644 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 1752.cc:  No.1752 Up-Down Tree - yukicoder
 */

#include<cstdio>
#include<vector>
#include<algorithm>
 
using namespace std;

/* constant */

const int MAX_N = 100000;

/* typedef */

typedef vector<int> vi;

/* global variables */

vi nbrs[MAX_N];
int ps[MAX_N], cis[MAX_N], ss[MAX_N];
int x0[MAX_N], x1[MAX_N], dp[MAX_N];

/* subroutines */

/* main */

int main() {
  int n;
  scanf("%d", &n);

  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    u--, v--;
    nbrs[u].push_back(v);
    nbrs[v].push_back(u);
  }

  ps[0] = -1;
  for (int u = 0; u >= 0;) {
    vi &nbru = nbrs[u];
    int up = ps[u];

    if (cis[u] < nbru.size()) {
      int v = nbru[cis[u]++];
      if (v != up) {
	ps[v] = u;
	u = v;
      }
    }
    else {
      ss[u]++;

      int ux = 1;
      if (x0[u] > 0) {
	ux = x0[u];
	if (x1[u] > 0) ux += 1 + x1[u];
	else if (ss[u] - x0[u] >= 2) ux += 2;
      }
      dp[u] = ux;

      if (up >= 0) {
	ss[up] += ss[u];
	if (x0[up] < ux) x1[up] = x0[up], x0[up] = ux;
	else if (x1[up] < ux) x1[up] = ux;
      }

      u = up;
    }
  }

  printf("%d\n", dp[0]);
  //for (int u = 0; u < n; u++) printf("%d ", dp[u]); putchar('\n');
  return 0;
}
0