結果

問題 No.1477 Lamps on Graph
ユーザー fukafukatanifukafukatani
提出日時 2021-04-16 23:48:21
言語 Rust
(1.77.0)
結果
AC  
実行時間 176 ms / 2,000 ms
コード長 2,445 bytes
コンパイル時間 947 ms
コンパイル使用メモリ 154,740 KB
実行使用メモリ 19,808 KB
最終ジャッジ日時 2023-09-16 03:33:06
合計ジャッジ時間 7,030 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,384 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 73 ms
9,688 KB
testcase_13 AC 74 ms
8,976 KB
testcase_14 AC 90 ms
11,024 KB
testcase_15 AC 33 ms
6,116 KB
testcase_16 AC 23 ms
5,880 KB
testcase_17 AC 26 ms
5,424 KB
testcase_18 AC 110 ms
10,280 KB
testcase_19 AC 72 ms
9,284 KB
testcase_20 AC 24 ms
4,468 KB
testcase_21 AC 96 ms
8,648 KB
testcase_22 AC 14 ms
4,376 KB
testcase_23 AC 45 ms
6,720 KB
testcase_24 AC 115 ms
12,100 KB
testcase_25 AC 32 ms
5,268 KB
testcase_26 AC 109 ms
12,640 KB
testcase_27 AC 38 ms
6,728 KB
testcase_28 AC 53 ms
8,964 KB
testcase_29 AC 52 ms
7,220 KB
testcase_30 AC 64 ms
5,768 KB
testcase_31 AC 37 ms
5,632 KB
testcase_32 AC 176 ms
19,808 KB
testcase_33 AC 129 ms
17,360 KB
testcase_34 AC 156 ms
9,436 KB
testcase_35 AC 155 ms
16,384 KB
testcase_36 AC 146 ms
16,780 KB
testcase_37 AC 114 ms
16,784 KB
testcase_38 AC 127 ms
16,792 KB
testcase_39 AC 146 ms
16,784 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused variable: `i`
  --> Main.rs:24:9
   |
24 |     for i in 0..m {
   |         ^ help: if this is intentional, prefix it with an underscore: `_i`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: unused variable: `k`
  --> Main.rs:35:9
   |
35 |     let k = read::<usize>();
   |         ^ help: if this is intentional, prefix it with an underscore: `_k`

warning: 2 warnings emitted

ソースコード

diff #

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

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}

fn main() {
    let v = read_vec::<usize>();
    let (n, m) = (v[0], v[1]);
    let a = read_vec::<i64>();
    let mut edges = vec![];
    let mut g = vec![vec![]; n];
    for i in 0..m {
        let v = read_vec::<usize>();
        let (u, v) = (v[0] - 1, v[1] - 1);
        if a[u] < a[v] {
            edges.push((u, v));
            g[u].push(v);
        } else if a[u] > a[v] {
            edges.push((v, u));
            g[v].push(u);
        }
    }
    let k = read::<usize>();
    let v = read_vec::<usize>();
    let mut light = vec![false; n];
    for i in v {
        light[i - 1] = true;
    }
    let indexes = topological_sort(&edges, n).unwrap();

    let mut answers = vec![];
    for i in indexes {
        if light[i] {
            for &to in g[i].iter() {
                light[to] = !light[to];
            }
            answers.push(i);
        }
    }
    println!("{}", answers.len());
    for ans in answers {
        println!("{}", ans + 1);
    }
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}

fn topological_sort(edges: &Vec<(usize, usize)>, v: usize) -> Option<Vec<usize>> {
    let mut h = vec![0; v];
    let mut g = vec![Vec::new(); v];
    for &(s, t) in edges {
        g[s].push(t);
        h[t] += 1;
    }

    let mut st: std::collections::VecDeque<usize> = std::collections::VecDeque::new();
    for i in 0..v {
        if h[i] == 0 {
            st.push_back(i);
        }
    }

    let mut sorted_indexes = Vec::new();
    while !st.is_empty() {
        let i = st.pop_back().unwrap();
        sorted_indexes.push(i);
        for &j in g[i].iter() {
            h[j] -= 1;
            if h[j] == 0 {
                st.push_back(j);
            }
        }
    }
    if sorted_indexes.len() == v {
        Some(sorted_indexes)
    } else {
        None
    }
}
0