結果
| 問題 |
No.3113 The farthest point
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-04-20 15:51:08 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,021 bytes |
| コンパイル時間 | 981 ms |
| コンパイル使用メモリ | 84,468 KB |
| 実行使用メモリ | 18,212 KB |
| 最終ジャッジ日時 | 2025-04-20 15:51:16 |
| 合計ジャッジ時間 | 6,350 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| 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>> tree;
vector<bool> visited;
ll max_dist = 0;
int farthest_node = 0;
void dfs(int node, ll dist) {
visited[node] = true;
if (dist > max_dist) {
max_dist = dist;
farthest_node = node;
}
for (auto [next, weight] : tree[node]) {
if (!visited[next]) {
dfs(next, dist + weight);
}
}
}
int main() {
cin >> n;
tree.resize(n + 1);
for (int i = 0; i < n - 1; ++i) {
int u, v;
ll w;
cin >> u >> v >> w;
tree[u].emplace_back(v, w);
tree[v].emplace_back(u, w);
}
visited.assign(n + 1, false);
dfs(1, 0); // 適当な頂点(1)から最も遠い点を見つける
visited.assign(n + 1, false);
max_dist = 0;
dfs(farthest_node, 0); // その点からもう一度DFS
cout << max_dist << endl; // 木の直径
return 0;
}