Submission #5499526


Source Code Expand

Copy
// https://atcoder.jp/contests/agc027/tasks/agc027_c
//
#![allow(unused_imports)]
use std::cmp::*;
use std::collections::*;
use std::fmt::*;
use std::io::*;
use std::str::*;

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

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

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        println!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}


fn has_cycle(graph: Vec<Vec<usize>>) -> bool {
    let n = graph.len();
    let mut indeg = vec![0; n];
    for i in 0..n {
        for &j in graph[i].iter() {
            indeg[j] += 1;
        }
    }

    let mut deq = VecDeque::new();
    let mut cnt = 0;
    for i in 0..n {
        if indeg[i] == 0 {
            deq.push_back(i);
            cnt += 1;
        }
    }

    while deq.len() >= 1 {
        let u = deq.pop_front().unwrap();
        for &j in graph[u].iter() {
            indeg[j] -= 1;
            if indeg[j] == 0 {
                deq.push_back(j);
                cnt += 1;
            }
        }
    }
    return cnt != n;
}

fn main() {
    input! {
        n: usize, m: usize,
        s: chars,
        edges: [(usize1, usize1); m]
    };


    let mut snum = vec![0; n];
    for i in 0..n {
        if s[i] == 'B' {
            snum[i] = 1;
        }
    }

    let mut graph = vec![vec![]; n];
    for &(u, v) in edges.iter() {
        if u == v {
            graph[u].push(v);
        } else {
            graph[u].push(v);
            graph[v].push(u);
        }
    }

    let mut dgraph = vec![vec![]; 2*n];
    for i in 0..n {
        for &t in graph[i].iter() {
            // i*2(R,R) -> t*2(R,B)
            // i*2+1(B,R) -> t*2(R,B)
            let tt = t*2 + (snum[i] ^ snum[t] ^ 1);
            if snum[t] == snum[i] {
                dgraph[i*2].push(tt);
            }
            if snum[t] != snum[i] {
                dgraph[i*2+1].push(tt);
            }
        }
    }

    if has_cycle(dgraph) {
        println!("Yes");
    } else {
        println!("No");
    }
}

Submission Info

Submission Time
Task C - ABland Yard
User hamadu
Language Rust (1.15.1)
Score 900
Code Size 3255 Byte
Status
Exec Time 93 ms
Memory 49336 KB

Test Cases

Set Name Score / Max Score Test Cases
Sample 0 / 0 sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt
All 900 / 900 sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt, test_01.txt, test_02.txt, test_03.txt, test_04.txt, test_05.txt, test_06.txt, test_07.txt, test_08.txt, test_09.txt, test_10.txt, test_11.txt, test_12.txt, test_13.txt, test_14.txt, test_15.txt, test_16.txt, test_17.txt, test_18.txt, test_19.txt, test_20.txt, test_21.txt, test_22.txt, test_23.txt, test_24.txt, test_25.txt, test_26.txt, test_27.txt, test_28.txt, test_29.txt, test_30.txt, test_31.txt, test_32.txt, test_33.txt, test_34.txt, test_35.txt, test_36.txt, test_37.txt, test_38.txt, test_39.txt, test_40.txt, test_41.txt, test_42.txt, test_43.txt, test_44.txt, test_45.txt, test_46.txt, test_47.txt, test_48.txt, test_49.txt, test_50.txt, test_51.txt, test_52.txt, test_53.txt, test_54.txt, test_55.txt
Case Name Status Exec Time Memory
sample_01.txt 2 ms 4352 KB
sample_02.txt 2 ms 4352 KB
sample_03.txt 2 ms 4352 KB
sample_04.txt 2 ms 4352 KB
test_01.txt 74 ms 39164 KB
test_02.txt 80 ms 39164 KB
test_03.txt 44 ms 22780 KB
test_04.txt 57 ms 33020 KB
test_05.txt 53 ms 30972 KB
test_06.txt 37 ms 22780 KB
test_07.txt 22 ms 16636 KB
test_08.txt 36 ms 22780 KB
test_09.txt 31 ms 20732 KB
test_10.txt 22 ms 16636 KB
test_11.txt 69 ms 39164 KB
test_12.txt 71 ms 39164 KB
test_13.txt 40 ms 22780 KB
test_14.txt 57 ms 33020 KB
test_15.txt 51 ms 30972 KB
test_16.txt 15 ms 10492 KB
test_17.txt 22 ms 16636 KB
test_18.txt 16 ms 10492 KB
test_19.txt 26 ms 16636 KB
test_20.txt 23 ms 18684 KB
test_21.txt 28 ms 14588 KB
test_22.txt 66 ms 37116 KB
test_23.txt 45 ms 26876 KB
test_24.txt 58 ms 28832 KB
test_25.txt 43 ms 22780 KB
test_26.txt 85 ms 41040 KB
test_27.txt 50 ms 28924 KB
test_28.txt 33 ms 26876 KB
test_29.txt 48 ms 26876 KB
test_30.txt 13 ms 8444 KB
test_31.txt 77 ms 39164 KB
test_32.txt 56 ms 35068 KB
test_33.txt 66 ms 35076 KB
test_34.txt 19 ms 14588 KB
test_35.txt 93 ms 49336 KB
test_36.txt 2 ms 4352 KB
test_37.txt 2 ms 4352 KB
test_38.txt 2 ms 4352 KB
test_39.txt 2 ms 4352 KB
test_40.txt 2 ms 4352 KB
test_41.txt 2 ms 4352 KB
test_42.txt 2 ms 4352 KB
test_43.txt 2 ms 4352 KB
test_44.txt 2 ms 4352 KB
test_45.txt 2 ms 4352 KB
test_46.txt 2 ms 4476 KB
test_47.txt 2 ms 4352 KB
test_48.txt 2 ms 4352 KB
test_49.txt 2 ms 4352 KB
test_50.txt 2 ms 4352 KB
test_51.txt 2 ms 4352 KB
test_52.txt 2 ms 4352 KB
test_53.txt 2 ms 4352 KB
test_54.txt 2 ms 4352 KB
test_55.txt 2 ms 4352 KB