結果

問題 No.2301 Namorientation
ユーザー haihamabossuhaihamabossu
提出日時 2023-05-12 22:36:02
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 487 ms / 3,000 ms
コード長 4,547 bytes
コンパイル時間 14,174 ms
コンパイル使用メモリ 379,212 KB
実行使用メモリ 51,456 KB
最終ジャッジ日時 2024-11-28 19:09:31
合計ジャッジ時間 28,881 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 1 ms
5,248 KB
testcase_03 AC 1 ms
5,248 KB
testcase_04 AC 1 ms
5,248 KB
testcase_05 AC 1 ms
5,248 KB
testcase_06 AC 1 ms
5,248 KB
testcase_07 AC 1 ms
5,248 KB
testcase_08 AC 1 ms
5,248 KB
testcase_09 AC 1 ms
5,248 KB
testcase_10 AC 1 ms
5,248 KB
testcase_11 AC 1 ms
5,248 KB
testcase_12 AC 258 ms
28,288 KB
testcase_13 AC 235 ms
25,984 KB
testcase_14 AC 455 ms
45,848 KB
testcase_15 AC 290 ms
31,232 KB
testcase_16 AC 385 ms
39,936 KB
testcase_17 AC 274 ms
29,440 KB
testcase_18 AC 399 ms
40,704 KB
testcase_19 AC 212 ms
24,448 KB
testcase_20 AC 403 ms
40,320 KB
testcase_21 AC 289 ms
30,636 KB
testcase_22 AC 326 ms
43,904 KB
testcase_23 AC 324 ms
43,392 KB
testcase_24 AC 271 ms
36,420 KB
testcase_25 AC 253 ms
40,064 KB
testcase_26 AC 330 ms
51,456 KB
testcase_27 AC 469 ms
46,208 KB
testcase_28 AC 474 ms
46,336 KB
testcase_29 AC 472 ms
46,208 KB
testcase_30 AC 487 ms
46,088 KB
testcase_31 AC 474 ms
46,208 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

pub mod scanner {

    pub struct Scanner {
        buf: Vec<String>,
    }

    impl Scanner {
        pub fn new() -> Self {
            Self { buf: vec![] }
        }

        pub fn new_from(source: &str) -> Self {
            let source = String::from(source);
            let buf = Self::split(source);
            Self { buf }
        }

        pub fn next<T: std::str::FromStr>(&mut self) -> T {
            loop {
                if let Some(x) = self.buf.pop() {
                    return x.parse().ok().expect("");
                }
                let mut source = String::new();
                std::io::stdin().read_line(&mut source).expect("");
                self.buf = Self::split(source);
            }
        }

        fn split(source: String) -> Vec<String> {
            source
                .split_whitespace()
                .rev()
                .map(String::from)
                .collect::<Vec<_>>()
        }
    }
}

pub mod directed_graph {

    type Weight = isize;

    #[derive(Clone)]
    pub struct Edge {
        pub from: usize,
        pub to: usize,
        pub index: usize,
        pub weight: Weight,
    }

    pub struct DirectedGraph {
        pub n: usize,
        pub m: usize,
        pub edges: Vec<Vec<Edge>>,
        pub orig_edges: Vec<Edge>,
        pub degree: Vec<usize>,
    }

    impl DirectedGraph {
        pub fn new(n: usize, m: usize) -> Self {
            let edges = vec![vec![]; n];
            let orig_edges = vec![];
            let degree = vec![0; n];
            Self {
                n,
                m,
                edges,
                orig_edges,
                degree,
            }
        }

        pub fn init(scanner: &mut crate::scanner::Scanner, indexed: usize) -> Self {
            let n: usize = scanner.next();
            let m: usize = n;
            let mut graph = Self::new(n, m);
            for index in 0..m {
                let from: usize = scanner.next::<usize>() - indexed;
                let to: usize = scanner.next::<usize>() - indexed;
                let weight: Weight = 1;
                graph.orig_edges.push(Edge {
                    from,
                    to,
                    index,
                    weight,
                });
                graph.add_edge(from, to, index, weight);
                graph.add_edge(to, from, index, weight);
                graph.degree[from] += 1;
                graph.degree[to] += 1;
            }
            graph
        }

        pub fn add_edge(&mut self, from: usize, to: usize, index: usize, weight: Weight) {
            let edge = Edge {
                from,
                to,
                index,
                weight,
            };
            self.edges[from].push(edge);
        }
    }
}

use crate::{directed_graph::DirectedGraph, scanner::Scanner};

fn main() {
    let mut scanner = Scanner::new();
    let t: usize = 1;
    for _ in 0..t {
        solve(&mut scanner);
    }
}

fn solve(scanner: &mut Scanner) {
    let mut graph = DirectedGraph::init(scanner, 1);
    let n = graph.n;
    let mut dir = vec![!0; n];
    let mut que = std::collections::VecDeque::new();
    for u in 0..n {
        if graph.degree[u] == 1 {
            que.push_back(u);
        }
    }
    while let Some(u) = que.pop_front() {
        for edge in graph.edges[u].iter_mut() {
            if dir[edge.index] == !0 {
                dir[edge.index] = u;
                graph.degree[edge.from] -= 1;
                graph.degree[edge.to] -= 1;
                if graph.degree[edge.to] == 1 {
                    que.push_back(edge.to);
                }
            }
        }
    }
    let mut cur = 0;
    for u in 0..n {
        if graph.degree[u] == 2 {
            cur = u;
            break;
        }
    }
    loop {
        let mut updated = false;
        for edge in graph.edges[cur].iter_mut() {
            if graph.degree[edge.to] == 2 {
                dir[edge.index] = cur;
                graph.degree[edge.from] -= 1;
                graph.degree[edge.to] -= 1;
                cur = edge.to;
                updated = true;
                break;
            }
        }
        if !updated {
            break;
        }
    }
    for edge in graph.edges[cur].iter_mut() {
        if dir[edge.index] == !0 {
            dir[edge.index] = cur;
            break;
        }
    }
    for i in 0..n {
        if dir[i] == graph.orig_edges[i].from {
            println!("->");
        } else {
            println!("<-");
        }
    }
}
0