結果

問題 No.1254 補強への架け橋
ユーザー o2co2c
提出日時 2020-10-09 23:46:41
言語 Rust
(1.77.0)
結果
AC  
実行時間 177 ms / 2,000 ms
コード長 3,865 bytes
コンパイル時間 1,583 ms
コンパイル使用メモリ 160,264 KB
実行使用メモリ 38,624 KB
最終ジャッジ日時 2023-09-27 20:06:05
合計ジャッジ時間 11,412 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 0 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 0 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 0 ms
4,376 KB
testcase_33 AC 1 ms
4,376 KB
testcase_34 AC 1 ms
4,376 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 1 ms
4,376 KB
testcase_37 AC 1 ms
4,376 KB
testcase_38 AC 1 ms
4,376 KB
testcase_39 AC 1 ms
4,380 KB
testcase_40 AC 1 ms
4,376 KB
testcase_41 AC 0 ms
4,380 KB
testcase_42 AC 1 ms
4,380 KB
testcase_43 AC 1 ms
4,380 KB
testcase_44 AC 1 ms
4,380 KB
testcase_45 AC 1 ms
4,380 KB
testcase_46 AC 1 ms
4,376 KB
testcase_47 AC 2 ms
4,376 KB
testcase_48 AC 1 ms
4,380 KB
testcase_49 AC 1 ms
4,376 KB
testcase_50 AC 1 ms
4,376 KB
testcase_51 AC 1 ms
4,376 KB
testcase_52 AC 2 ms
4,380 KB
testcase_53 AC 1 ms
4,376 KB
testcase_54 AC 2 ms
4,376 KB
testcase_55 AC 1 ms
4,380 KB
testcase_56 AC 1 ms
4,376 KB
testcase_57 AC 1 ms
4,376 KB
testcase_58 AC 1 ms
4,380 KB
testcase_59 AC 0 ms
4,376 KB
testcase_60 AC 1 ms
4,380 KB
testcase_61 AC 1 ms
4,376 KB
testcase_62 AC 1 ms
4,380 KB
testcase_63 AC 6 ms
4,380 KB
testcase_64 AC 2 ms
4,380 KB
testcase_65 AC 5 ms
4,376 KB
testcase_66 AC 4 ms
4,376 KB
testcase_67 AC 2 ms
4,380 KB
testcase_68 AC 4 ms
4,376 KB
testcase_69 AC 6 ms
4,376 KB
testcase_70 AC 2 ms
4,376 KB
testcase_71 AC 2 ms
4,376 KB
testcase_72 AC 6 ms
4,380 KB
testcase_73 AC 3 ms
4,380 KB
testcase_74 AC 6 ms
4,380 KB
testcase_75 AC 4 ms
4,376 KB
testcase_76 AC 2 ms
4,380 KB
testcase_77 AC 4 ms
4,380 KB
testcase_78 AC 7 ms
4,380 KB
testcase_79 AC 6 ms
4,376 KB
testcase_80 AC 5 ms
4,380 KB
testcase_81 AC 7 ms
4,376 KB
testcase_82 AC 6 ms
4,376 KB
testcase_83 AC 102 ms
18,136 KB
testcase_84 AC 91 ms
17,592 KB
testcase_85 AC 45 ms
11,556 KB
testcase_86 AC 73 ms
14,688 KB
testcase_87 AC 72 ms
15,816 KB
testcase_88 AC 11 ms
4,376 KB
testcase_89 AC 87 ms
17,900 KB
testcase_90 AC 48 ms
12,112 KB
testcase_91 AC 40 ms
10,288 KB
testcase_92 AC 19 ms
6,280 KB
testcase_93 AC 60 ms
14,232 KB
testcase_94 AC 62 ms
13,176 KB
testcase_95 AC 61 ms
13,096 KB
testcase_96 AC 85 ms
16,344 KB
testcase_97 AC 30 ms
8,612 KB
testcase_98 AC 78 ms
17,312 KB
testcase_99 AC 47 ms
11,216 KB
testcase_100 AC 86 ms
18,436 KB
testcase_101 AC 17 ms
5,604 KB
testcase_102 AC 9 ms
4,380 KB
testcase_103 AC 20 ms
6,316 KB
testcase_104 AC 26 ms
7,636 KB
testcase_105 AC 66 ms
14,196 KB
testcase_106 AC 36 ms
9,168 KB
testcase_107 AC 92 ms
17,920 KB
testcase_108 AC 85 ms
17,912 KB
testcase_109 AC 76 ms
14,224 KB
testcase_110 AC 52 ms
12,904 KB
testcase_111 AC 66 ms
13,696 KB
testcase_112 AC 25 ms
7,320 KB
testcase_113 AC 52 ms
12,636 KB
testcase_114 AC 36 ms
8,964 KB
testcase_115 AC 12 ms
4,500 KB
testcase_116 AC 44 ms
10,540 KB
testcase_117 AC 27 ms
7,288 KB
testcase_118 AC 81 ms
15,972 KB
testcase_119 AC 51 ms
10,980 KB
testcase_120 AC 76 ms
15,816 KB
testcase_121 AC 21 ms
6,500 KB
testcase_122 AC 36 ms
10,232 KB
testcase_123 AC 1 ms
4,376 KB
testcase_124 AC 171 ms
38,624 KB
testcase_125 AC 177 ms
38,536 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(unused_imports)]
use {std::cmp::*, std::collections::*, std::io::Write, std::ops::*};

#[allow(unused_macros)]
macro_rules !dbg {($($e :expr ) ,*) =>{#[cfg (debug_assertions ) ] $({let (e ,mut err ) =(stringify !($e ) ,std ::io ::stderr () ) ;writeln !(err ,"{} = {:?}" ,e ,$e ) .unwrap () } ) *} ;}
#[allow(unused_macros)]
macro_rules !min {($a :expr $(,) *) =>{{$a } } ;($a :expr ,$b :expr $(,) *) =>{{std ::cmp ::min ($a ,$b ) } } ;($a :expr ,$($rest :expr ) ,+$(,) *) =>{{std ::cmp ::min ($a ,min !($($rest ) ,+) ) } } ;}
#[allow(unused_macros)]
macro_rules !chmin {($base :expr ,$($cmps :expr ) ,+$(,) *) =>{{let cmp_min =min !($($cmps ) ,+) ;if $base >cmp_min {$base =cmp_min ;true } else {false } } } ;}
#[allow(unused_macros)]
macro_rules !max {($a :expr $(,) *) =>{{$a } } ;($a :expr ,$b :expr $(,) *) =>{{std ::cmp ::max ($a ,$b ) } } ;($a :expr ,$($rest :expr ) ,+$(,) *) =>{{std ::cmp ::max ($a ,max !($($rest ) ,+) ) } } ;}
#[allow(unused_macros)]
macro_rules !chmax {($base :expr ,$($cmps :expr ) ,+$(,) *) =>{{let cmp_max =max !($($cmps ) ,+) ;if $base <cmp_max {$base =cmp_max ;true } else {false } } } ;}

#[allow(dead_code)]
mod scanner {
    use std;
    use std::io::Read;
    use std::str::FromStr;
    use std::str::SplitWhitespace;
    pub struct Scanner<'a> {
        it: SplitWhitespace<'a>,
    }
    impl<'a> Scanner<'a> {
        pub fn new(s: &'a String) -> Scanner<'a> {
            Scanner {
                it: s.split_whitespace(),
            }
        }
        pub fn next<T: FromStr>(&mut self) -> T {
            match self.it.next().unwrap().parse::<T>() {
                Ok(v) => v,
                _ => panic!("Scanner error"),
            }
        }
        pub fn next_chars(&mut self) -> Vec<char> {
            self.next::<String>().chars().collect()
        }
        pub fn next_vec<T: FromStr>(&mut self, len: usize) -> Vec<T> {
            (0..len).map(|_| self.next()).collect()
        }
    }
    pub fn read_string() -> String {
        let mut s = String::new();
        std::io::stdin().read_to_string(&mut s).unwrap();
        s
    }
}

fn main() {
    let sc = scanner::read_string();
    let mut sc = scanner::Scanner::new(&sc);
    let out = std::io::stdout();
    let mut out = std::io::BufWriter::new(out.lock());

    let n: usize = sc.next();
    let mut map = std::collections::BTreeMap::<(usize, usize), usize>::new();
    let mut to = vec![vec![]; n];
    for i in 0..n {
        let a = sc.next::<usize>() - 1;
        let b = sc.next::<usize>() - 1;
        to[a].push(b);
        to[b].push(a);
        map.insert((a, b), i);
        map.insert((b, a), i);
    }

    let mut stack = vec![];
    let mut node = vec![];
    let mut set = std::collections::BTreeSet::<usize>::new();
    dfs(0, 0, &to, &mut set, &mut stack, &mut node);
    let mut ans = vec![];
    for a in node.windows(2) {
        ans.push(map.get(&(a[0], a[1])).unwrap());
    }
    ans.push(map.get(&(node[0], node[node.len() - 1])).unwrap());
    writeln!(out, "{}", ans.len()).unwrap();
    for i in 0..ans.len() {
        if i != 0 {
            write!(out, " ").unwrap();
        }
        write!(out, "{}", ans[i] + 1).unwrap();
    }
    writeln!(out, "").unwrap();
}

fn dfs(
    u: usize,
    p: usize,
    to: &Vec<Vec<usize>>,
    set: &mut BTreeSet<usize>,
    stack: &mut Vec<usize>,
    ans: &mut Vec<usize>,
) -> bool {
    stack.push(u);
    set.insert(u);
    for &v in to[u].iter() {
        if v == p {
            continue;
        }
        if set.contains(&v) {
            while let Some(x) = stack.pop() {
                ans.push(x);
                if x == v {
                    break;
                }
            }
            return true;
        }
        if dfs(v, u, to, set, stack, ans) {
            return true;
        }
    }
    stack.pop();
    set.remove(&u);
    false
}
0