結果
問題 |
No.806 木を道に
|
ユーザー |
![]() |
提出日時 | 2019-03-22 21:27:45 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,390 bytes |
コンパイル時間 | 1,127 ms |
コンパイル使用メモリ | 125,684 KB |
最終ジャッジ日時 | 2025-01-06 23:43:43 |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 9 WA * 18 |
ソースコード
#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; }