Submission #62548047


Source Code Expand

use ac_library::Dsu;

use itertools::Itertools;
use proconio::input;
use proconio::marker::Usize1;

fn main() {
    input!{
        n: usize,
        m: usize,
        e: [(Usize1, Usize1); m],
    }
    let mut uf = Dsu::new(n);
    let mut amari = vec![];
    for (i, (a, b)) in e.iter().enumerate() {
        if !uf.same(*a, *b) {
            uf.merge(*a, *b);
        } else {
            amari.push(i);
        }
    }

    // 0に集める
    let mut stk = vec![];
    for i in 0..n {
        if !uf.same(0, i) {
            stk.push(i);
        }
    }
    let mut ans = vec![];
    for i in amari {
        if stk.len() == 0 {
            break;
        }
        if uf.same(0, e[i].0) {
            while let Some(x) = stk.pop() {
                if !uf.same(0, x) {
                    ans.push((i, e[i].0, x));
                    uf.merge(e[i].0, x);
                    break;
                }
            }
        } else {
            ans.push((i, e[i].0, 0));
            uf.merge(0, e[i].0);
        }
    }
    println!("{}", ans.len());
    println!("{}", ans.iter().map(|(i, a, b)| format!("{} {} {}", i+1, a+1, b+1)).join("\n"));
}


Submission Info

Submission Time
Task E - Cables and Servers
User ardRiriy
Language Rust (rustc 1.70.0)
Score 450
Code Size 1209 Byte
Status AC
Exec Time 51 ms
Memory 23000 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 450 / 450
Status
AC × 3
AC × 22
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt
All random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, sample_01.txt, sample_02.txt, sample_03.txt
Case Name Status Exec Time Memory
random_01.txt AC 10 ms 5900 KiB
random_02.txt AC 7 ms 6116 KiB
random_03.txt AC 2 ms 2384 KiB
random_04.txt AC 0 ms 2020 KiB
random_05.txt AC 12 ms 7940 KiB
random_06.txt AC 8 ms 6868 KiB
random_07.txt AC 2 ms 2612 KiB
random_08.txt AC 0 ms 2008 KiB
random_09.txt AC 12 ms 6500 KiB
random_10.txt AC 5 ms 4600 KiB
random_11.txt AC 22 ms 9816 KiB
random_12.txt AC 1 ms 2080 KiB
random_13.txt AC 19 ms 8200 KiB
random_14.txt AC 19 ms 8232 KiB
random_15.txt AC 19 ms 7996 KiB
random_16.txt AC 19 ms 8220 KiB
random_17.txt AC 35 ms 14592 KiB
random_18.txt AC 51 ms 23000 KiB
random_19.txt AC 1 ms 2044 KiB
sample_01.txt AC 0 ms 1968 KiB
sample_02.txt AC 0 ms 1940 KiB
sample_03.txt AC 0 ms 2148 KiB