結果

問題 No.1103 Directed Length Sum
ユーザー StrorkisStrorkis
提出日時 2020-07-03 22:55:13
言語 Rust
(1.77.0)
結果
WA  
実行時間 -
コード長 1,410 bytes
コンパイル時間 1,553 ms
コンパイル使用メモリ 168,252 KB
実行使用メモリ 182,504 KB
最終ジャッジ日時 2023-10-17 05:58:32
合計ジャッジ時間 6,742 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 WA -
testcase_03 AC 135 ms
41,884 KB
testcase_04 AC 316 ms
29,448 KB
testcase_05 AC 573 ms
49,504 KB
testcase_06 AC 186 ms
19,884 KB
testcase_07 AC 31 ms
6,096 KB
testcase_08 AC 51 ms
8,420 KB
testcase_09 AC 17 ms
4,512 KB
testcase_10 AC 78 ms
10,808 KB
testcase_11 AC 332 ms
31,824 KB
testcase_12 AC 182 ms
19,884 KB
testcase_13 AC 84 ms
11,064 KB
testcase_14 AC 13 ms
4,348 KB
testcase_15 AC 140 ms
15,992 KB
testcase_16 AC 386 ms
35,716 KB
testcase_17 AC 401 ms
37,036 KB
testcase_18 AC 78 ms
10,780 KB
testcase_19 AC 336 ms
32,616 KB
testcase_20 AC 21 ms
5,040 KB
testcase_21 AC 43 ms
7,876 KB
testcase_22 AC 275 ms
26,876 KB
testcase_23 AC 152 ms
16,980 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

struct DFS {
    graph: Vec<Vec<usize>>,
}

impl DFS {
    fn new(n: usize) -> DFS {
        DFS {
            graph: vec![vec![]; n],
        }
    }

    fn add_edge(&mut self, from: usize, to: usize) {
        self.graph[from].push(to);
    }

    fn search(&mut self, from: usize, d: usize) -> usize {
        let mut res = 0;
        for &to in self.graph[from].clone().iter() {
            res += self.search(to, d + 1);
        }
        res + (1 + d) * d / 2
    }
}

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);
    let mut is_root = vec![true; n];
    for _ in 0..(n - 1) {
        let (a, b): (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.add_edge(a, b);
        is_root[b] = false;
    }

    let root = {
        is_root.iter()
            .enumerate()
            .filter(|&(_, &b)| b)
            .map(|(i, _)| i)
            .next()
            .unwrap()
    };

    let ans = dfs.search(root, 0);
    println!("{}", ans);
}
0