結果

問題 No.3113 The farthest point
コンテスト
ユーザー zer0-star
提出日時 2025-04-19 01:12:45
言語 Rust
(1.99.0 + proconio + num + itertools + ACL)
コンパイル:
/usr/bin/rustc_custom
実行:
./target/release/main
結果
WJ  
(最新)
AC  
(最初)
実行時間 -
コード長 1,306 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
最終ジャッジ日時 2026-10-17 00:17:32
ジャッジサーバーID
(参考情報)
judge4_0 / judge2_0
このコードへのチャレンジ
(要ログイン)

ソースコード

diff #
raw source code

use proconio::{fastout, input, marker::Usize1};

const DIRS: [(isize, isize); 4] = [(0, 1), (1, 0), (0, -1), (-1, 0)];

trait OrdExt {
    fn chmax(&mut self, other: Self) -> bool;
    fn chmin(&mut self, other: Self) -> bool;
}

impl<T: Ord> OrdExt for T {
    fn chmax(&mut self, other: Self) -> bool {
        if *self < other {
            *self = other;
            true
        } else {
            false
        }
    }

    fn chmin(&mut self, other: Self) -> bool {
        if *self > other {
            *self = other;
            true
        } else {
            false
        }
    }
}

#[fastout]
fn main() {
    input! {
        N: usize,
        edges: [(Usize1, Usize1, isize); N-1],
    }

    let mut graph = vec![vec![]; N];

    for (a, b, c) in edges {
        graph[a].push((b, c));
        graph[b].push((a, c));
    }

    println!("{}", rec(&graph, 0, N).1);
}

fn rec(graph: &Vec<Vec<(usize, isize)>>, v: usize, p: usize) -> (isize, isize) {
    let mut paths = vec![0, 0];
    let mut max2 = 0;

    for &(w, d) in &graph[v] {
        if w == p {
            continue;
        }
        let (d1, d2) = rec(graph, w, v);

        paths.push(d1 + d);
        max2.chmax(d2);
    }

    paths.sort_by(|a, b| b.cmp(a));

    max2.chmax(paths[0] + paths[1]);

    (paths[0], max2)
}
0