結果

問題 No.3113 The farthest point
ユーザー zer0-star
提出日時 2025-04-19 01:12:45
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 99 ms / 2,000 ms
コード長 1,306 bytes
コンパイル時間 12,700 ms
コンパイル使用メモリ 390,156 KB
実行使用メモリ 30,900 KB
最終ジャッジ日時 2025-04-19 01:13:06
合計ジャッジ時間 16,343 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 33
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: constant `DIRS` is never used
 --> src/main.rs:3:7
  |
3 | const DIRS: [(isize, isize); 4] = [(0, 1), (1, 0), (0, -1), (-1, 0)];
  |       ^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: method `chmin` is never used
 --> src/main.rs:7:8
  |
5 | trait OrdExt {
  |       ------ method in this trait
6 |     fn chmax(&mut self, other: Self) -> bool;
7 |     fn chmin(&mut self, other: Self) -> bool;
  |        ^^^^^

warning: variable `N` should have a snake case name
  --> src/main.rs:33:9
   |
33 |         N: usize,
   |         ^ help: convert the identifier to snake case: `n`
   |
   = note: `#[warn(non_snake_case)]` on by default

ソースコード

diff #

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