結果

問題 No.1484 木に数を書き込む問題 / Just Write Numbers! 2
ユーザー umezoumezo
提出日時 2021-04-19 04:29:51
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 228 ms / 2,000 ms
コード長 697 bytes
コンパイル時間 2,628 ms
コンパイル使用メモリ 201,772 KB
実行使用メモリ 20,220 KB
最終ジャッジ日時 2023-09-17 08:13:11
合計ジャッジ時間 7,304 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
8,028 KB
testcase_01 AC 5 ms
8,020 KB
testcase_02 AC 4 ms
8,184 KB
testcase_03 AC 5 ms
8,032 KB
testcase_04 AC 7 ms
8,204 KB
testcase_05 AC 15 ms
8,720 KB
testcase_06 AC 16 ms
8,656 KB
testcase_07 AC 10 ms
8,376 KB
testcase_08 AC 18 ms
8,752 KB
testcase_09 AC 16 ms
8,692 KB
testcase_10 AC 16 ms
8,656 KB
testcase_11 AC 14 ms
8,544 KB
testcase_12 AC 12 ms
8,500 KB
testcase_13 AC 6 ms
8,100 KB
testcase_14 AC 161 ms
13,756 KB
testcase_15 AC 153 ms
13,476 KB
testcase_16 AC 138 ms
13,232 KB
testcase_17 AC 95 ms
11,852 KB
testcase_18 AC 184 ms
14,544 KB
testcase_19 AC 228 ms
15,304 KB
testcase_20 AC 224 ms
15,324 KB
testcase_21 AC 222 ms
15,324 KB
testcase_22 AC 211 ms
15,376 KB
testcase_23 AC 225 ms
15,296 KB
testcase_24 AC 224 ms
15,268 KB
testcase_25 AC 227 ms
15,280 KB
testcase_26 AC 225 ms
15,296 KB
testcase_27 AC 226 ms
15,292 KB
testcase_28 AC 222 ms
15,300 KB
testcase_29 AC 143 ms
20,220 KB
testcase_30 AC 156 ms
16,992 KB
testcase_31 AC 108 ms
15,648 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

#include <bits/stdc++.h>
using namespace std;

vector<int> G[200020];
int dist[200020];

void dfs(int v,int p,int d){
  dist[v]=d;
  for(auto nv:G[v]){
    if(nv==p) continue;
    dfs(nv,v,d+1);
  }
}

int main(){
  int n;
  cin>>n;
  rep(i,n-1){
    int a,b;
    cin>>a>>b;
    a--,b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  
  dfs(0,-1,0);
  int p=0;
  int ma=0;
  rep(i,n){
    if(dist[i]>ma){
      ma=dist[i];
      p=i;
    }
  }
  
  rep(i,n) dist[i]=0;
  dfs(p,-1,0);
  ma=0;
  rep(i,n){
    if(dist[i]>ma) ma=dist[i];
  }
  cout<<2*(n-1)-ma<<endl;
  
  return 0;
}
0