結果

問題 No.806 木を道に
ユーザー 👑 emthrmemthrm
提出日時 2019-03-22 21:27:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,390 bytes
コンパイル時間 1,317 ms
コンパイル使用メモリ 130,416 KB
実行使用メモリ 17,944 KB
最終ジャッジ日時 2023-10-19 06:26:44
合計ジャッジ時間 3,178 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 2 ms
4,348 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 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 25 ms
12,928 KB
testcase_25 AC 38 ms
17,944 KB
testcase_26 AC 14 ms
7,424 KB
testcase_27 AC 41 ms
16,860 KB
testcase_28 AC 3 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cassert>
#include <cctype>
#include <chrono>
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()

const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const int MOD = 1000000007; // 998244353
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
/*-------------------------------------------------*/
using CostType = long long;
struct Edge {
  int src, dst;
  CostType cost;
  Edge(int src_, int dst_, CostType cost_ = 0) : src(src_), dst(dst_), cost(cost_) {}
  inline bool operator<(const Edge &rhs) const {
    return cost != rhs.cost ? cost < rhs.cost : dst != rhs.dst ? dst < rhs.dst : src < rhs.src;
  }
  inline bool operator<=(const Edge &rhs) const { return cost <= rhs.cost; }
  inline bool operator>(const Edge &rhs) const {
    return cost != rhs.cost ? cost > rhs.cost : dst != rhs.dst ? dst > rhs.dst : src > rhs.src;
  }
  inline bool operator>=(const Edge &rhs) const { return cost >= rhs.cost; }
};

struct DoubleSweep {
  using Pci = pair<CostType, int>;

  int s, t;
  CostType diameter;

  DoubleSweep(const vector<vector<Edge> > &graph_) : graph(graph_) {
    Pci tmp1 = dfs(-1, 0);
    s = tmp1.second;
    Pci tmp2 = dfs(-1, tmp1.second);
    t = tmp2.second;
    diameter = tmp2.first;
  }

private:
  vector<vector<Edge> > graph;

  Pci dfs(int par, int ver) {
    Pci res = {0, ver};
    for (Edge e : graph[ver]) if (e.dst != par) {
      Pci result = dfs(ver, e.dst);
      result.first += e.cost;
      if (result.first > res.first) res = result;
    }
    return res;
  }
};

int main() {
  cin.tie(0); ios::sync_with_stdio(false);
  // freopen("input.txt", "r", stdin);

  int n; cin >> n;
  vector<vector<Edge> > graph(n);
  REP(i, n - 1) {
    int a, b; cin >> a >> b; --a; --b;
    graph[a].emplace_back(Edge(a, b, 1));
    graph[b].emplace_back(Edge(b, a, 1));
  }
  DoubleSweep ds(graph);
  cout << n - 1 - ds.diameter << '\n';
  return 0;
}
0