結果

問題 No.1817 Reversed Edges
ユーザー phsplsphspls
提出日時 2022-10-14 00:06:47
言語 Rust
(1.77.0)
結果
WA  
実行時間 -
コード長 1,818 bytes
コンパイル時間 12,764 ms
コンパイル使用メモリ 379,312 KB
実行使用メモリ 19,200 KB
最終ジャッジ日時 2024-06-26 12:14:15
合計ジャッジ時間 18,179 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 1 ms
5,376 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 150 ms
15,360 KB
testcase_23 AC 152 ms
15,104 KB
testcase_24 AC 160 ms
19,200 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::collections::VecDeque;


fn dfs(cur: usize, prev: usize, paths: &Vec<Vec<usize>>, parents: &mut Vec<usize>) {
    parents[cur] = prev;
    for &v in paths[cur].iter() {
        if v == prev { continue; }
        dfs(v, cur, paths, parents);
    }
}

fn main() {
    let mut n = String::new();
    std::io::stdin().read_line(&mut n).ok();
    let n: usize = n.trim().parse().unwrap();
    let mut paths = vec![vec![]; n];
    let mut lines = Vec::with_capacity(n-1);
    for _ in 0..n-1 {
        let mut ab = String::new();
        std::io::stdin().read_line(&mut ab).ok();
        let ab: Vec<usize> = ab.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
        let a = ab[0]-1;
        let b = ab[1]-1;
        paths[a].push(b);
        paths[b].push(a);
        lines.push((a, b));
    }

    let mut parents = vec![0usize; n];
    dfs(0, 0, &paths, &mut parents);
    let mut total = 0isize;
    let mut vals = vec![0isize; n];
    for &(a, b) in lines.iter() {
        if a < b {
            if parents[b] == a {
                vals[b] += 1;
            } else {
                vals[b] -= 1;
                total += 1;
            }
        } else {
            if parents[a] == b {
                vals[a] += 1;
            } else {
                vals[a] -= 1;
                total += 1;
            }
        }
    }
    let mut result = vec![0isize; n];
    result[0] = vals[0];
    let mut deque = VecDeque::new();
    deque.push_back((0, vals[0]));
    while let Some((u, val)) = deque.pop_front() {
        for &v in paths[u].iter() {
            if v == parents[u] { continue; }
            let nval = val + vals[v];
            result[v] = nval;
            deque.push_back((v, nval));
        }
    }
    for &v in result.iter() {
        println!("{}", v + total);
    }
}
0