結果

問題 No.2427 Tree Distance Two
ユーザー ずーずる
提出日時 2023-08-18 23:59:57
言語 Rust
(1.83.0 + proconio)
結果
WA  
実行時間 -
コード長 2,003 bytes
コンパイル時間 13,006 ms
コンパイル使用メモリ 377,892 KB
実行使用メモリ 25,472 KB
最終ジャッジ日時 2024-11-28 11:52:53
合計ジャッジ時間 19,714 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other WA * 35
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused import: `read_to_string`
 --> src/main.rs:1:21
  |
1 | use std::io::{self, read_to_string};
  |                     ^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: unused variable: `i`
  --> src/main.rs:17:9
   |
17 |     for i in 0..size - 1 {
   |         ^ help: if this is intentional, prefix it with an underscore: `_i`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: variable does not need to be mutable
  --> src/main.rs:29:9
   |
29 |     let mut t_node: Vec<Node> = Vec::new();
   |         ----^^^^^^
   |         |
   |         help: remove this `mut`
   |
   = note: `#[warn(unused_mut)]` on by default

ソースコード

diff #

use std::io::{self, read_to_string};

fn main() {
    let mut x = String::new();
    io::stdin().read_line(&mut x).unwrap();
    // let  mut tmp= x.split_whitespace().map(|a|a.parse::<u32>().unwrap() );

    // let size:usize=x.parse().unwrap();
    let size = x
        .split_whitespace()
        .map(|a| a.parse::<u32>().unwrap())
        .next()
        .unwrap();
    x.clear();
    let mut pairs = Vec::<(u32, u32)>::new();

    for i in 0..size - 1 {
        io::stdin().read_line(&mut x).unwrap();
        let mut tmp = x.split_whitespace().map(|a| a.parse::<u32>().unwrap());
        pairs.push((tmp.next().unwrap(), tmp.next().unwrap()));
        x.clear();
    }

    let swap = pairs
        .iter()
        .map(|(a, b)| if a <= b { (*a, *b) } else { (*b, *a) });
    let mut pairs: Vec<(u32, u32)> = swap.collect::<Vec<(u32, u32)>>();
    pairs.sort_by(|&a, &b| a.0.cmp(&b.0));
    let mut t_node: Vec<Node> = Vec::new();
    let mut last=1;
    let mut node: Vec<Node> = Vec::new();

    for i in 0..t_node.len(){
        if last+1!=t_node[i].number{
            node.push(Node{
                number:last+1,
                to:vec!(last+2)
            })

        }
        node.push(t_node[i].clone());
        last=t_node[i].number;
    }
    

    let mut lastone = 1;
    let mut tmp_to = Vec::<u32>::new();
    for i in pairs.iter() {
        if i.0 == lastone {
            tmp_to.push(i.1)
        } else {
            node.push(Node {
                number: (i.0),
                to: (tmp_to.clone()),
            });
            tmp_to.clear();
            lastone = i.0;
            tmp_to.push(i.1)
        }
    }

    for i in 0..node.len() {
        let mut count = 0;
        if i >= 2 {
            count += 1;
        }

        if i + 2 < node.len() {
            count += 1;
        }

        if i != 0 {
            count+=node[i-1].to.len()-1;
        }
        println!("{}",count);
    }
}


#[derive(Clone)]
struct Node {
    number: u32,
    to: Vec<u32>,
}
0