結果
問題 |
No.3113 The farthest point
|
ユーザー |
|
提出日時 | 2025-04-20 15:54:02 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 990 bytes |
コンパイル時間 | 886 ms |
コンパイル使用メモリ | 84,364 KB |
実行使用メモリ | 18,136 KB |
最終ジャッジ日時 | 2025-04-20 15:54:08 |
合計ジャッジ時間 | 4,078 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 20 WA * 13 |
ソースコード
#include <iostream> #include <vector> #include <algorithm> using namespace std; using ll = long long; using P = pair<int, ll>; int N; vector<vector<P>> G; vector<bool> visited; ll max_dist = 0; int far_node = 0; void dfs(int node, ll dist) { visited[node] = true; if (dist > max_dist) { max_dist = dist; far_node = node; } for (auto [to, w] : G[node]) { if (!visited[to]) { dfs(to, dist + w); } } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> N; G.resize(N + 1); // 1-indexed for (int i = 0; i < N - 1; ++i) { int u, v; ll w; cin >> u >> v >> w; G[u].emplace_back(v, w); G[v].emplace_back(u, w); } // 1回目のDFS visited.assign(N + 1, false); max_dist = 0; dfs(1, 0); // 2回目のDFS visited.assign(N + 1, false); max_dist = 0; dfs(far_node, 0); cout << max_dist << '\n'; return 0; }