結果

問題 No.1098 LCAs
ユーザー StrorkisStrorkis
提出日時 2020-06-26 23:05:08
言語 Rust
(1.77.0)
結果
AC  
実行時間 407 ms / 2,000 ms
コード長 1,395 bytes
コンパイル時間 1,431 ms
コンパイル使用メモリ 143,328 KB
実行使用メモリ 49,684 KB
最終ジャッジ日時 2023-09-18 07:20:44
合計ジャッジ時間 8,442 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 3 ms
4,376 KB
testcase_14 AC 3 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 3 ms
4,376 KB
testcase_18 AC 372 ms
18,200 KB
testcase_19 AC 386 ms
18,168 KB
testcase_20 AC 374 ms
18,232 KB
testcase_21 AC 357 ms
18,300 KB
testcase_22 AC 380 ms
18,132 KB
testcase_23 AC 350 ms
20,512 KB
testcase_24 AC 340 ms
20,600 KB
testcase_25 AC 334 ms
20,924 KB
testcase_26 AC 339 ms
20,824 KB
testcase_27 AC 338 ms
20,924 KB
testcase_28 AC 407 ms
47,036 KB
testcase_29 AC 400 ms
49,684 KB
testcase_30 AC 400 ms
46,820 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

struct DFS {
    g: Vec<Vec<usize>>,
    check: Vec<bool>,
    ans: Vec<usize>,
}

impl DFS {
    fn new(n: usize) -> DFS {
        DFS {
            g: vec![vec![]; n],
            check: vec![false; n],
            ans: vec![0; n],
        }
    }

    fn search(&mut self, from: usize) -> usize {
        self.check[from] = true;
        let mut sum = 0;
        for &to in self.g[from].clone().iter() {
            if self.check[to] { continue; }
            let res = self.search(to);
            self.ans[from] += sum * res;
            sum += res;
        }
        self.ans[from] *= 2;
        self.ans[from] += sum * 2 + 1;
        sum + 1
    }
}

fn main() {
    let n: usize = {
        let mut buf = String::new();
        std::io::stdin().read_line(&mut buf).unwrap();
        buf.trim_end().parse().unwrap()
    };

    let mut dfs = DFS::new(n);
    for _ in 0..(n - 1) {
        let (v, w): (usize, usize) = {
            let mut buf = String::new();
            std::io::stdin().read_line(&mut buf).unwrap();
            let mut iter = buf.split_whitespace();
            (
                iter.next().unwrap().parse::<usize>().unwrap() - 1,
                iter.next().unwrap().parse::<usize>().unwrap() - 1,
            )
        };

        dfs.g[v].push(w);
        dfs.g[w].push(v);
    }

    dfs.search(0);
    for i in 0..n {
        println!("{}", dfs.ans[i]);
    }
}
0