結果

問題 No.806 木を道に
ユーザー tonegawatonegawa
提出日時 2020-08-16 15:40:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,038 bytes
コンパイル時間 1,033 ms
コンパイル使用メモリ 123,108 KB
実行使用メモリ 35,328 KB
最終ジャッジ日時 2024-04-19 18:21:32
合計ジャッジ時間 3,096 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
26,752 KB
testcase_01 AC 9 ms
26,604 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 9 ms
26,624 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 10 ms
26,752 KB
testcase_08 AC 9 ms
26,496 KB
testcase_09 AC 10 ms
26,624 KB
testcase_10 WA -
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 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 47 ms
32,480 KB
testcase_25 AC 65 ms
35,328 KB
testcase_26 AC 27 ms
27,904 KB
testcase_27 AC 69 ms
30,428 KB
testcase_28 AC 14 ms
26,752 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <algorithm>
#include <set>
#include <map>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#define vll vector<ll>
#define vvvl vector<vvl>
#define vvl vector<vector<ll>>
#define VV(a, b, c, d) vector<vector<d>>(a, vector<d>(b, c))
#define VVV(a, b, c, d) vector<vvl>(a, vvl(b, vll (c, d)));
#define re(c, b) for(ll c=0;c<b;c++)
#define rep(a,b,c) for(ll a=b;a<c;a++)
#define all(obj) (obj).begin(), (obj).end()
typedef long long int ll;
typedef long double ld;
using namespace std;

void get(vll &a){re(i,a.size()) scanf("%lld",&a[i]);}
void get(vvl &a){re(i,a.size()) re(j,a[0].size()) scanf("%lld",&a[i][j]);}
void print(vll &a){re(i,a.size()) cout<<a[i]<<(i==a.size()-1?"\n":" ");}
void print(vvl &a){re(i,a.size()) re(j,a[0].size())cout<<a[i][j]<<(j==a[0].size()-1?"\n":" ");}
//_____________________________________________________________
const int MAX_NODE=1000000;

vvl G = VV(MAX_NODE, 0, 0, ll);
void dfs(int from, int now){
  //std::cout << now << '\n';
  for(int i=0;i<G[now].size();i++) {
    if(from == G[now][i]) continue;
    dfs(now, G[now][i]);
  }
}
void treeDFS(int from, int current, int dist, int &maxDist, int &maxVertex) {
    if (dist > maxDist) maxDist = dist, maxVertex = current;
    for (auto to : G[current]) {
        if (to == from) continue;
        treeDFS(current, to, dist + 1, maxDist, maxVertex);
    }
}
int getTreeDiameter(int s) {
    int start = s, end = 0, maxDist = 0;
    treeDFS(-1, start, 0, maxDist, end);
    start = end, end = 0, maxDist = 0;
    treeDFS(-1, start, 0, maxDist, end);
    //printf("start: %d, end: %d, diameter: %d\n", start, end, maxDist);
    return maxDist;
}
//_____________________________________________________________
int main(int argc, char const *argv[]) {
  ll n;std::cin >> n;
  re(i, n-1){
    ll a, b;std::cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  cout << n - 1 - getTreeDiameter(1) << "\n";
  return 0;
}
0