Submission #2541629


Source Code Expand

use std::collections::VecDeque;

fn main() {
    let mut sc = Scanner::new();
    let n: usize = sc.read();
    let m: usize = sc.read();
    let mut graph = vec![vec![]; n];
    let mut a: Vec<(u64, usize)> = (0..n).map(|i| (sc.read(), i)).collect();
    a.sort();

    let mut uf = UnionFind::new(n);

    for _ in 0..m {
        let x: usize = sc.read();
        let y: usize = sc.read();
        graph[x].push(y);
        graph[y].push(x);
        uf.unite(x, y);
    }

    let mut ans = 0;
    let mut queue = VecDeque::new();
    for &(a, i) in &a {
        if queue.is_empty() {
            queue.push_back((a, i));
            continue;
        }

        let (head_a, head_i) = queue.pop_front().unwrap();
        if uf.find(head_i) == uf.find(i) {
            queue.push_front((head_a, head_i));
            queue.push_back((a, i));
        } else {
            ans += head_a + a;
            uf.unite(i, head_i);
        }
    }
    for i in 0..n {
        if uf.find(i) != uf.find(0) {
            println!("Impossible");
            return;
        }
    }

    println!("{}", ans);
}

struct UnionFind {
    parent: Vec<usize>,
    sizes: Vec<usize>,
    size: usize,
}

impl UnionFind {
    fn new(n: usize) -> UnionFind {
        UnionFind {
            parent: (0..n).map(|i| i).collect::<Vec<usize>>(),
            sizes: vec![1; n],
            size: n,
        }
    }

    fn find(&mut self, x: usize) -> usize {
        if x == self.parent[x] {
            x
        } else {
            let px = self.parent[x];
            self.parent[x] = self.find(px);
            self.parent[x]
        }
    }

    fn unite(&mut self, x: usize, y: usize) -> bool {
        let parent_x = self.find(x);
        let parent_y = self.find(y);
        if parent_x == parent_y {
            return false;
        }

        let (large, small) = if self.sizes[parent_x] < self.sizes[parent_y] {
            (parent_y, parent_x)
        } else {
            (parent_x, parent_y)
        };

        self.parent[small] = large;
        self.sizes[large] += self.sizes[small];
        self.sizes[small] = 0;
        self.size -= 1;
        return true;
    }
}

struct Scanner {
    ptr: usize,
    length: usize,
    buf: Vec<u8>,
    small_cache: Vec<u8>,
}

impl Scanner {
    fn new() -> Scanner {
        Scanner {
            ptr: 0,
            length: 0,
            buf: vec![0; 1024],
            small_cache: vec![0; 1024],
        }
    }

    fn load(&mut self) {
        use std::io::Read;
        let mut s = std::io::stdin();
        self.length = s.read(&mut self.buf).unwrap();
    }

    fn byte(&mut self) -> u8 {
        if self.ptr >= self.length {
            self.ptr = 0;
            self.load();
            if self.length == 0 {
                self.buf[0] = b'\n';
                self.length = 1;
            }
        }

        self.ptr += 1;
        return self.buf[self.ptr - 1];
    }

    fn is_space(b: u8) -> bool {
        b == b'\n' || b == b'\r' || b == b'\t' || b == b' '
    }

    fn read<T>(&mut self) -> T
    where
        T: std::str::FromStr,
        T::Err: std::fmt::Debug,
    {
        let mut b = self.byte();
        while Scanner::is_space(b) {
            b = self.byte();
        }

        for pos in 0..self.small_cache.len() {
            self.small_cache[pos] = b;
            b = self.byte();
            if Scanner::is_space(b) {
                return String::from_utf8_lossy(&self.small_cache[0..(pos + 1)])
                    .parse()
                    .unwrap();
            }
        }

        let mut v = self.small_cache.clone();
        while !Scanner::is_space(b) {
            v.push(b);
            b = self.byte();
        }
        return String::from_utf8_lossy(&v).parse().unwrap();
    }
}

Submission Info

Submission Time
Task D - Forest
User kenkoooo
Language Rust (1.15.1)
Score 0
Code Size 3938 Byte
Status WA
Exec Time 50 ms
Memory 18684 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 600
Status
AC × 3
AC × 39
WA × 13
Set Name Test Cases
Sample 0_000.txt, 0_001.txt, 0_002.txt
All 0_000.txt, 0_001.txt, 0_002.txt, 1_003.txt, 1_004.txt, 1_005.txt, 1_006.txt, 1_007.txt, 1_008.txt, 1_009.txt, 1_010.txt, 1_011.txt, 1_012.txt, 1_013.txt, 1_014.txt, 1_015.txt, 1_016.txt, 1_017.txt, 1_018.txt, 1_019.txt, 1_020.txt, 1_021.txt, 1_022.txt, 1_023.txt, 1_024.txt, 1_025.txt, 1_026.txt, 1_027.txt, 1_028.txt, 1_029.txt, 1_030.txt, 1_031.txt, 1_032.txt, 1_033.txt, 1_034.txt, 1_035.txt, 1_036.txt, 1_037.txt, 1_038.txt, 1_039.txt, 1_040.txt, 1_041.txt, 1_042.txt, 1_043.txt, 1_044.txt, 1_045.txt, 1_046.txt, 1_047.txt, 1_048.txt, 1_049.txt, 1_050.txt, 1_051.txt
Case Name Status Exec Time Memory
0_000.txt AC 2 ms 4352 KB
0_001.txt AC 2 ms 4352 KB
0_002.txt AC 2 ms 4352 KB
1_003.txt WA 2 ms 4352 KB
1_004.txt AC 24 ms 12668 KB
1_005.txt AC 24 ms 12540 KB
1_006.txt AC 24 ms 12540 KB
1_007.txt AC 24 ms 12540 KB
1_008.txt AC 26 ms 12540 KB
1_009.txt WA 37 ms 12540 KB
1_010.txt WA 42 ms 14588 KB
1_011.txt WA 45 ms 14588 KB
1_012.txt AC 49 ms 16636 KB
1_013.txt AC 48 ms 16636 KB
1_014.txt AC 48 ms 16636 KB
1_015.txt AC 24 ms 12540 KB
1_016.txt AC 24 ms 12540 KB
1_017.txt AC 24 ms 12540 KB
1_018.txt AC 24 ms 12540 KB
1_019.txt AC 27 ms 12540 KB
1_020.txt WA 38 ms 12540 KB
1_021.txt WA 43 ms 14588 KB
1_022.txt AC 45 ms 14588 KB
1_023.txt AC 50 ms 18684 KB
1_024.txt AC 50 ms 18684 KB
1_025.txt AC 49 ms 18684 KB
1_026.txt AC 8 ms 6396 KB
1_027.txt AC 8 ms 6396 KB
1_028.txt AC 8 ms 6396 KB
1_029.txt AC 8 ms 6396 KB
1_030.txt AC 9 ms 6396 KB
1_031.txt WA 14 ms 8444 KB
1_032.txt AC 17 ms 8444 KB
1_033.txt WA 17 ms 8444 KB
1_034.txt AC 19 ms 8444 KB
1_035.txt AC 19 ms 8444 KB
1_036.txt AC 19 ms 8444 KB
1_037.txt AC 15 ms 12540 KB
1_038.txt AC 15 ms 12540 KB
1_039.txt AC 15 ms 12540 KB
1_040.txt AC 15 ms 12540 KB
1_041.txt AC 18 ms 12540 KB
1_042.txt WA 28 ms 12540 KB
1_043.txt WA 32 ms 14588 KB
1_044.txt WA 34 ms 14588 KB
1_045.txt AC 40 ms 18684 KB
1_046.txt AC 40 ms 18684 KB
1_047.txt AC 40 ms 18684 KB
1_048.txt AC 38 ms 12540 KB
1_049.txt WA 37 ms 12540 KB
1_050.txt AC 37 ms 12540 KB
1_051.txt WA 38 ms 12540 KB