結果

問題 No.2301 Namorientation
ユーザー koba-e964koba-e964
提出日時 2023-05-12 23:34:25
言語 Rust
(1.77.0)
結果
AC  
実行時間 292 ms / 3,000 ms
コード長 3,708 bytes
コンパイル時間 7,014 ms
コンパイル使用メモリ 167,960 KB
実行使用メモリ 60,968 KB
最終ジャッジ日時 2023-08-19 06:23:13
合計ジャッジ時間 16,129 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 159 ms
32,412 KB
testcase_13 AC 140 ms
29,420 KB
testcase_14 AC 285 ms
53,212 KB
testcase_15 AC 189 ms
35,896 KB
testcase_16 AC 245 ms
45,876 KB
testcase_17 AC 162 ms
33,772 KB
testcase_18 AC 249 ms
46,680 KB
testcase_19 AC 132 ms
27,792 KB
testcase_20 AC 248 ms
46,280 KB
testcase_21 AC 174 ms
35,368 KB
testcase_22 AC 139 ms
52,604 KB
testcase_23 AC 136 ms
51,856 KB
testcase_24 AC 114 ms
43,540 KB
testcase_25 AC 143 ms
47,160 KB
testcase_26 AC 186 ms
60,968 KB
testcase_27 AC 292 ms
53,400 KB
testcase_28 AC 276 ms
53,400 KB
testcase_29 AC 281 ms
53,516 KB
testcase_30 AC 281 ms
53,404 KB
testcase_31 AC 277 ms
53,444 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
use std::io::{Write, BufWriter};
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes.by_ref().map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr,) => {};
    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => { ($(read_value!($next, $t)),*) };
    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };
    ($next:expr, usize1) => (read_value!($next, usize) - 1);
    ($next:expr, [ $t:tt ]) => {{
        let len = read_value!($next, usize);
        read_value!($next, [$t; len])
    }};
    ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

trait Change { fn chmax(&mut self, x: Self); fn chmin(&mut self, x: Self); }
impl<T: PartialOrd> Change for T {
    fn chmax(&mut self, x: T) { if *self < x { *self = x; } }
    fn chmin(&mut self, x: T) { if *self > x { *self = x; } }
}

fn main() {
    // In order to avoid potential stack overflow, spawn a new thread.
    let stack_size = 104_857_600; // 100 MB
    let thd = std::thread::Builder::new().stack_size(stack_size);
    thd.spawn(|| solve()).unwrap().join().unwrap();
}

fn solve() {
    let out = std::io::stdout();
    let mut out = BufWriter::new(out.lock());
    macro_rules! puts {($($format:tt)*) => (let _ = write!(out,$($format)*););}
    #[allow(unused)]
    macro_rules! putvec {
        ($v:expr) => {
            for i in 0..$v.len() {
                puts!("{}{}", $v[i], if i + 1 == $v.len() {"\n"} else {" "});
            }
        }
    }
    input! {
        n: usize,
        ab: [(usize1, usize1); n],
    }
    let mut g = vec![BTreeSet::new(); n];
    let mut act = vec![true; n];
    for i in 0..n {
        let (a, b) = ab[i];
        g[a].insert((b, i));
        g[b].insert((a, n + i));
    }
    let mut que = vec![];
    for i in 0..n {
        if g[i].len() == 1 {
            que.push(i);
        }
    }
    let mut ori = vec![""; n];
    while let Some(v) = que.pop() {
        act[v] = false;
        let &(to, eidx) = g[v].iter().next().unwrap();
        ori[eidx % n] = if eidx >= n { "<-" } else { "->" };
        g[to].remove(&(v, (eidx + n) % (2 * n)));
        if g[to].len() == 1 {
            que.push(to);
        }
    }
    // One cycle remains.
    let mut st = 0;
    for i in 0..n {
        if act[i] {
            st = i;
            break;
        }
    }
    let &(to, eidx) = g[st].iter().next().unwrap();
    g[st].remove(&(to, eidx));
    g[to].remove(&(st, (eidx + n) % (2 * n)));
    ori[eidx % n] = if eidx >= n { "<-" } else { "->" };
    que.push(to);
    while let Some(v) = que.pop() {
        act[v] = false;
        let &(to, eidx) = g[v].iter().next().unwrap();
        ori[eidx % n] = if eidx >= n { "<-" } else { "->" };
        g[to].remove(&(v, (eidx + n) % (2 * n)));
        if g[to].len() == 1 {
            que.push(to);
        }
    }
    for i in 0..n {
        puts!("{}\n", ori[i]);
    }
}
0