結果

問題 No.1420 国勢調査 (Easy)
ユーザー 57tggx57tggx
提出日時 2021-01-29 15:44:56
言語 Rust
(1.77.0)
結果
AC  
実行時間 175 ms / 2,000 ms
コード長 4,558 bytes
コンパイル時間 2,764 ms
コンパイル使用メモリ 148,928 KB
実行使用メモリ 6,244 KB
最終ジャッジ日時 2023-09-09 09:48:32
合計ジャッジ時間 10,593 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 51 ms
4,488 KB
testcase_03 AC 51 ms
4,468 KB
testcase_04 AC 51 ms
4,448 KB
testcase_05 AC 50 ms
4,408 KB
testcase_06 AC 51 ms
4,448 KB
testcase_07 AC 37 ms
4,460 KB
testcase_08 AC 37 ms
4,460 KB
testcase_09 AC 37 ms
4,448 KB
testcase_10 AC 37 ms
4,480 KB
testcase_11 AC 36 ms
4,476 KB
testcase_12 AC 6 ms
4,380 KB
testcase_13 AC 132 ms
4,376 KB
testcase_14 AC 6 ms
4,380 KB
testcase_15 AC 136 ms
4,376 KB
testcase_16 AC 135 ms
4,380 KB
testcase_17 AC 135 ms
4,380 KB
testcase_18 AC 137 ms
4,376 KB
testcase_19 AC 134 ms
4,380 KB
testcase_20 AC 132 ms
4,376 KB
testcase_21 AC 133 ms
4,376 KB
testcase_22 AC 172 ms
6,220 KB
testcase_23 AC 173 ms
6,224 KB
testcase_24 AC 171 ms
6,188 KB
testcase_25 AC 174 ms
6,244 KB
testcase_26 AC 175 ms
6,204 KB
testcase_27 AC 41 ms
6,200 KB
testcase_28 AC 41 ms
6,164 KB
testcase_29 AC 40 ms
6,224 KB
testcase_30 AC 41 ms
6,220 KB
testcase_31 AC 41 ms
6,224 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

fn main() {
    let input = Input::read();
    assert!(matches!(input.n, 1..=100000));
    assert!(matches!(input.m, 1..=100000));
    let mut union_find = UnionFind::new(input.n);
    for &(a, b, y) in &input.t {
        assert!(a < b && b < input.n);
        assert!(matches!(y, 0..=0x3fffffff));
        if let Some(diff) = union_find.diff(a, b) {
            if diff != y {
                println!("-1");
                return;
            }
        } else {
            union_find.unite(a, b, y);
        }
        // dbg!(&union_find);
    }
    for i in union_find.construct() {
        println!("{}", i);
    }
}

#[derive(Debug)]
struct UnionFind {
    n: usize,
    parent: Vec<usize>,
    diff: Vec<u32>,
    size: Vec<usize>,
}

impl UnionFind {
    fn new(n: usize) -> UnionFind {
        UnionFind {
            n: n,
            parent: (0..n).collect(),
            diff: vec![0; n],
            size: vec![1; n],
        }
    }
    fn root(&mut self, index: usize) -> usize {
        if self.parent[index] == index {
            index
        } else {
            let ret = self.root(self.parent[index]);
            self.diff[index] ^= self.diff[self.parent[index]];
            self.parent[index] = ret;
            ret
        }
    }
    fn diff(&mut self, index1: usize, index2: usize) -> Option<u32> {
        let root1 = self.root(index1);
        let root2 = self.root(index2);
        if root1 == root2 {
            (self.diff[index1] ^ self.diff[index2]).into()
        } else {
            None
        }
    }
    fn unite_root(&mut self, root1: usize, root2: usize, diff: u32) {
        assert_ne!(root1, root2);
        if self.size[root1] < self.size[root2] {
            self.unite_root(root2, root1, diff)
        } else {
            self.size[root1] += self.size[root2];
            self.size[root2] = 0;
            self.parent[root2] = self.parent[root1];
            self.diff[root2] = diff;
        }
    }
    fn unite(&mut self, index1: usize, index2: usize, diff: u32) {
        let root1 = self.root(index1);
        let root2 = self.root(index2);
        self.unite_root(root1, root2, diff ^ self.diff[index1] ^ self.diff[index2])
    }
    fn construct(&mut self) -> &Vec<u32> {
        for i in 0..self.n {
            self.root(i);
        }
        &self.diff
    }
}

#[derive(Debug)]
struct Input {
    n: usize,
    m: usize,
    t: Vec<(usize, usize, u32)>,
}

impl Input {
    fn read() -> Input {
        let stdin = std::io::stdin();
        let (n, m) = {
            let mut s = String::new();
            stdin.read_line(&mut s).unwrap();
            match &split(&s).unwrap()[..] {
                [n, m] => (n.parse().unwrap(), m.parse().unwrap()),
                _ => panic!("input format error: N M"),
            }
        };
        let t = {
            let mut v = Vec::new();
            for i in 0..m {
                let (a, b) = {
                    let mut s = String::new();
                    stdin.read_line(&mut s).unwrap();
                    match &split(&s).unwrap()[..] {
                        [a, b] => (a.parse::<usize>().unwrap(), b.parse::<usize>().unwrap()),
                        _ => panic!("input format error: A_{0} B_{0}", i + 1),
                    }
                };
                let a = a - 1;
                let b = b - 1;
                let y = {
                    let mut s = String::new();
                    stdin.read_line(&mut s).unwrap();
                    match &split(&s).unwrap()[..] {
                        [y] => y.parse().unwrap(),
                        _ => panic!("input format error: Y_{0}", i + 1),
                    }
                };
                v.push((a, b, y));
            }
            v
        };
        assert_eq!(
            stdin.read_line(&mut String::new()).unwrap(),
            0,
            "input format error: something left"
        );
        Input { n: n, m: m, t: t }
    }
}

fn split(s: &str) -> Option<Vec<&str>> {
    enum State {
        Word(usize),
        Space,
        End,
    }
    let mut state = State::Word(0);
    let mut ret = Vec::new();
    for (i, c) in s.char_indices() {
        let prev = match state {
            State::End => return None,
            State::Word(i) => i,
            State::Space => {
                state = State::Word(i);
                i
            }
        };
        if c == ' ' || c == '\n' {
            ret.push(&s[prev..i]);
            state = if c == ' ' { State::Space } else { State::End };
        }
    }
    ret.into()
}
0