Submission #2541803


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 a: Vec<usize> = (0..n).map(|_| sc.read()).collect();

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

    let mut queue = VecDeque::new();
    let mut used = vec![false; n];
    let mut groups = Vec::new();
    for i in 0..n {
        if used[i] {
            continue;
        }
        queue.push_back(i);
        used[i] = true;
        let mut group = vec![a[i]];
        while let Some(v) = queue.pop_front() {
            for &to in &graph[v] {
                if used[to] {
                    continue;
                }
                used[to] = true;
                queue.push_back(to);
                group.push(a[to]);
            }
        }

        group.sort();
        groups.push(group);
    }

    let mut required = (n - 1 - m) * 2;
    if required == 0 {
        println!("0");
        return;
    }
    let mut ans = 0;
    let mut rest = Vec::new();
    for &ref v in &groups {
        ans += v[0];
        required -= 1;
        for i in 1..v.len() {
            rest.push(v[i]);
        }
    }
    if required > rest.len() {
        println!("Impossible");
        return;
    }

    rest.sort();
    for i in 0..required {
        ans += rest[i];
    }
    println!("{}", ans);
}

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 600
Code Size 3240 Byte
Status AC
Exec Time 41 ms
Memory 16636 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 3
AC × 52
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 AC 2 ms 4352 KB
1_004.txt AC 18 ms 14588 KB
1_005.txt AC 18 ms 14588 KB
1_006.txt AC 19 ms 14588 KB
1_007.txt AC 19 ms 14588 KB
1_008.txt AC 22 ms 15864 KB
1_009.txt AC 32 ms 14588 KB
1_010.txt AC 36 ms 16636 KB
1_011.txt AC 37 ms 14588 KB
1_012.txt AC 40 ms 14588 KB
1_013.txt AC 41 ms 16636 KB
1_014.txt AC 40 ms 14588 KB
1_015.txt AC 19 ms 14588 KB
1_016.txt AC 19 ms 14588 KB
1_017.txt AC 19 ms 14588 KB
1_018.txt AC 19 ms 14588 KB
1_019.txt AC 22 ms 15864 KB
1_020.txt AC 33 ms 14588 KB
1_021.txt AC 36 ms 16636 KB
1_022.txt AC 37 ms 14588 KB
1_023.txt AC 41 ms 14588 KB
1_024.txt AC 40 ms 14588 KB
1_025.txt AC 40 ms 12540 KB
1_026.txt AC 8 ms 8444 KB
1_027.txt AC 8 ms 8444 KB
1_028.txt AC 8 ms 8444 KB
1_029.txt AC 8 ms 8444 KB
1_030.txt AC 9 ms 8444 KB
1_031.txt AC 14 ms 8444 KB
1_032.txt AC 15 ms 8444 KB
1_033.txt AC 16 ms 8444 KB
1_034.txt AC 18 ms 8444 KB
1_035.txt AC 18 ms 8444 KB
1_036.txt AC 18 ms 8444 KB
1_037.txt AC 14 ms 14588 KB
1_038.txt AC 14 ms 14588 KB
1_039.txt AC 14 ms 14588 KB
1_040.txt AC 14 ms 14588 KB
1_041.txt AC 18 ms 15864 KB
1_042.txt AC 28 ms 14588 KB
1_043.txt AC 31 ms 16636 KB
1_044.txt AC 31 ms 14588 KB
1_045.txt AC 36 ms 14588 KB
1_046.txt AC 36 ms 14588 KB
1_047.txt AC 35 ms 12540 KB
1_048.txt AC 30 ms 14588 KB
1_049.txt AC 33 ms 14588 KB
1_050.txt AC 30 ms 14588 KB
1_051.txt AC 33 ms 14588 KB