結果

問題 No.1484 木に数を書き込む問題 / Just Write Numbers! 2
ユーザー 👑 NachiaNachia
提出日時 2021-04-19 01:03:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 143 ms / 2,000 ms
コード長 1,050 bytes
コンパイル時間 716 ms
コンパイル使用メモリ 80,428 KB
実行使用メモリ 25,012 KB
最終ジャッジ日時 2023-09-17 08:07:12
合計ジャッジ時間 4,428 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,500 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 8 ms
4,376 KB
testcase_06 AC 8 ms
4,376 KB
testcase_07 AC 5 ms
4,376 KB
testcase_08 AC 9 ms
4,480 KB
testcase_09 AC 9 ms
4,376 KB
testcase_10 AC 8 ms
4,380 KB
testcase_11 AC 7 ms
4,380 KB
testcase_12 AC 6 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 96 ms
13,072 KB
testcase_15 AC 81 ms
12,488 KB
testcase_16 AC 75 ms
12,012 KB
testcase_17 AC 52 ms
9,644 KB
testcase_18 AC 108 ms
14,188 KB
testcase_19 AC 133 ms
15,580 KB
testcase_20 AC 124 ms
15,740 KB
testcase_21 AC 125 ms
15,720 KB
testcase_22 AC 130 ms
15,776 KB
testcase_23 AC 136 ms
15,648 KB
testcase_24 AC 129 ms
15,656 KB
testcase_25 AC 133 ms
15,800 KB
testcase_26 AC 143 ms
15,764 KB
testcase_27 AC 137 ms
15,868 KB
testcase_28 AC 131 ms
15,660 KB
testcase_29 AC 77 ms
25,012 KB
testcase_30 AC 85 ms
19,404 KB
testcase_31 AC 55 ms
16,484 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <string>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using ull = unsigned long long;
using ll = long long;
#define rep(i,n) for(int i=0; i<(n); i++)

int N;
vector<vector<int>> E;
vector<int> D;
vector<int> P;
vector<int> ans2;
vector<int> ans;

void dfs(int p){
  for(int e:E[p]) if(D[e]==-1){
    P[e] = p;
    D[e] = D[p]+1;
    dfs(e);
  }
}

void dfs2(int p){
  if(D[P[p]]==-1){
    int e=P[p];
    D[e]=0;
    ans2[e] = ans2[p] + 1;
    dfs2(e);
    ans2[p] = ans[e] + 1;
  }
  for(int e:E[p]) if(D[e]==-1){
    D[e]=0;
    ans2[e] = ans2[p] + 1;
    dfs2(e);
    ans2[p] = ans[e] + 1;
  }
  ans[p] = ans2[p];
}

int main(){
  scanf("%d",&N);
  E.resize(N);
  rep(i,N-1){
    int u,v; scanf("%d%d",&u,&v); u--; v--;
    E[u].push_back(v);
    E[v].push_back(u);
  }

  D.assign(N,-1);
  P.assign(N,-1);
  D[0]=0; dfs(0);
  int s=0; rep(i,N) if(D[s]<D[i]) s=i;
  D.assign(N,-1);
  P.assign(N,-1);
  D[s]=0; dfs(s);
  int t=0; rep(i,N) if(D[t]<D[i]) t=i;
  printf("%d\n",(N-1)*2-D[t]);
  return 0;
}

0