結果

問題 No.2418 情報通だよ!Nafmoくん
ユーザー 👑 zeronosu77108zeronosu77108
提出日時 2023-08-12 13:51:45
言語 Rust
(1.77.0)
結果
AC  
実行時間 36 ms / 2,000 ms
コード長 3,297 bytes
コンパイル時間 13,235 ms
コンパイル使用メモリ 378,808 KB
実行使用メモリ 13,940 KB
最終ジャッジ日時 2024-04-30 04:17:08
合計ジャッジ時間 15,103 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 33 ms
12,632 KB
testcase_04 AC 9 ms
6,144 KB
testcase_05 AC 23 ms
10,124 KB
testcase_06 AC 22 ms
9,876 KB
testcase_07 AC 24 ms
11,132 KB
testcase_08 AC 21 ms
10,016 KB
testcase_09 AC 28 ms
12,696 KB
testcase_10 AC 31 ms
12,788 KB
testcase_11 AC 26 ms
11,200 KB
testcase_12 AC 19 ms
8,064 KB
testcase_13 AC 10 ms
5,376 KB
testcase_14 AC 18 ms
7,780 KB
testcase_15 AC 21 ms
11,632 KB
testcase_16 AC 36 ms
13,940 KB
testcase_17 AC 11 ms
6,364 KB
testcase_18 AC 17 ms
8,200 KB
testcase_19 AC 7 ms
5,632 KB
testcase_20 AC 24 ms
12,408 KB
testcase_21 AC 11 ms
5,744 KB
testcase_22 AC 13 ms
6,756 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

fn main() {
    let mut sc = Scanner::new();
    let n = sc.usize();
    let m = sc.usize();
    let n = 2*n;

    let edges = sc.edges(m);

    let mut uni = UnionFind::new(n);
    for (a, b) in edges { uni.merge(a, b); }

    let mut cnt = 0;
    for i in 0..n {
        if uni.root(i) != i { continue }
        if uni.size(i) % 2 == 1 { cnt += 1; }
    }

    println!("{}", cnt / 2);
}

struct UnionFind { per : Vec<Option<usize>>, size : Vec<usize> } #[allow(dead_code)] impl UnionFind { fn new (n : usize) -> Self { Self { per : vec![None; n+1], size: vec![1; n+1]} } fn root (&mut self, x : usize) -> usize { if let Some(y) = self.per[x] { let res = self.root(y); self.per[x] = Some(res); res } else { x } } fn merge(&mut self, mut x : usize, mut y : usize) -> bool { x = self.root(x); y = self.root(y); if x == y { return false; } if self.size[x] < self.size[y] { std::mem::swap(&mut x, &mut y); } self.per[y] = Some(x); self.size[x] += self.size[y]; true } fn same(&mut self, x : usize, y : usize) -> bool { self.root(x) == self.root(y) } fn size(&mut self, x : usize) -> usize { let x = self.root(x); self.size[x] } }
struct Scanner { s : std::collections::VecDeque<String> } #[allow(unused)] impl Scanner { fn new() -> Self { use std::io::Read; let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); Self { s : s.split_whitespace().map(|s| s.to_string()).collect() } } fn reload(&mut self) -> () { use std::io::Read; let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); self.s = s.split_whitespace().map(|s| s.to_string()).collect(); } fn usize(&mut self) -> usize { self.input() } fn usize1(&mut self) -> usize { self.input::<usize>() - 1 } fn isize(&mut self) -> isize { self.input() } fn i32(&mut self) -> i32 { self.input() } fn i64(&mut self) -> i64 { self.input() } fn i128(&mut self) -> i128 { self.input() } fn u8(&mut self) -> u8 { self.input() } fn u32(&mut self) -> u32 { self.input() } fn u64(&mut self) -> u64 { self.input() } fn u128(&mut self) -> u128 { self.input() } fn edge(&mut self) -> (usize, usize) { (self.usize1(), self.usize1()) } fn edges(&mut self, m : usize) -> Vec<(usize, usize)> { let mut e = Vec::with_capacity(m); for _ in 0..m { e.push(self.edge()); } e } fn wedge<T:std::str::FromStr>(&mut self) -> (usize, usize, T) { (self.usize1(), self.usize1(), self.input()) } fn wedges<T:std::str::FromStr>(&mut self, m : usize) -> Vec<(usize, usize, T)> { let mut e = Vec::with_capacity(m); for _ in 0..m { e.push(self.wedge()); } e } fn input<T>(&mut self) -> T where T: std::str::FromStr { if self.s.is_empty() { self.reload(); } if let Some(head) = self.s.pop_front() { head.parse::<T>().ok().unwrap() } else { panic!() } } fn tuple<T, U>(&mut self) -> (T, U) where T: std::str::FromStr, U: std::str::FromStr { (self.input(), self.input()) } fn vec<T>(&mut self, n: usize) -> Vec<T> where T: std::str::FromStr { if self.s.is_empty() { self.reload(); } self.s.drain(..n).map(|s| s.parse::<T>().ok().unwrap() ).collect::<Vec<T>>() } fn nvec<T>(&mut self) -> Vec<T> where T: std::str::FromStr { let n : usize = self.input(); self.vec(n) } fn chars(&mut self) -> Vec<char> { let s : String = self.input(); s.chars().collect() } fn bytes(&mut self) -> Vec<u8> { let s : String = self.input(); s.bytes().collect() } }
0