結果

問題 No.1983 [Cherry 4th Tune C] 南の島のマーメイド
ユーザー akakimidoriakakimidori
提出日時 2022-03-29 20:44:56
言語 Rust
(1.77.0)
結果
AC  
実行時間 85 ms / 4,000 ms
コード長 3,718 bytes
コンパイル時間 6,015 ms
コンパイル使用メモリ 146,176 KB
実行使用メモリ 43,184 KB
最終ジャッジ日時 2024-04-17 12:31:14
合計ジャッジ時間 7,878 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 4 ms
5,376 KB
testcase_11 AC 4 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 41 ms
11,352 KB
testcase_14 AC 52 ms
15,400 KB
testcase_15 AC 50 ms
11,888 KB
testcase_16 AC 26 ms
8,820 KB
testcase_17 AC 53 ms
16,768 KB
testcase_18 AC 42 ms
10,776 KB
testcase_19 AC 59 ms
17,000 KB
testcase_20 AC 44 ms
10,780 KB
testcase_21 AC 51 ms
14,844 KB
testcase_22 AC 66 ms
20,320 KB
testcase_23 AC 84 ms
22,444 KB
testcase_24 AC 83 ms
22,444 KB
testcase_25 AC 85 ms
22,576 KB
testcase_26 AC 83 ms
22,448 KB
testcase_27 AC 82 ms
22,576 KB
testcase_28 AC 82 ms
22,444 KB
testcase_29 AC 82 ms
22,576 KB
testcase_30 AC 83 ms
22,444 KB
testcase_31 AC 82 ms
22,448 KB
testcase_32 AC 83 ms
22,444 KB
testcase_33 AC 1 ms
5,376 KB
testcase_34 AC 32 ms
10,356 KB
testcase_35 AC 76 ms
43,184 KB
testcase_36 AC 52 ms
15,020 KB
testcase_37 AC 1 ms
5,376 KB
testcase_38 AC 19 ms
6,016 KB
testcase_39 AC 77 ms
43,180 KB
testcase_40 AC 53 ms
15,148 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

fn main() {
    let (n, e, ask) = read();
    let graph = CSR::new(n, e.iter().flat_map(|e| [(e.0, e.1), (e.1, e.0)]));
    let mut ord = vec![n; n];
    let mut low = vec![n; n];
    let mut dsu = DSU::new(n);
    let mut id = 0;
    for i in 0..n {
        if id < ord[i] {
            dfs(i, n, &graph, &mut ord, &mut low, &mut id, &mut dsu);
        }
    }
    let mut out = String::new();
    use std::fmt::*;
    for (a, b) in ask {
        let mut ans = "No";
        if dsu.same(a, b) {
            ans = "Yes";
        }
        writeln!(&mut out, "{}", ans).ok();
    }
    print!("{}", out);
}

fn dfs(v: usize, p: usize, graph: &CSR, ord: &mut [usize], low: &mut [usize], id: &mut usize, dsu: &mut DSU) {
    ord[v] = *id;
    low[v] = *id;
    *id += 1;
    for u in graph.list(v) {
        if u == p {
            continue;
        }
        if ord[u] > *id {
            dfs(u, v, graph, ord, low, id, dsu);
            low[v] = low[v].min(low[u]);
            if ord[v] < low[u] {
                dsu.unite(v, u);
            }
        } else {
            low[v] = low[v].min(ord[u]);
        }
    }
}

fn read() -> (usize, Vec<(usize, usize)>, Vec<(usize, usize)>) {
    let mut s = String::new();
    use std::io::Read;
    std::io::stdin().read_to_string(&mut s).unwrap();
    let mut it = s.trim().split_whitespace().flat_map(|s| s.parse::<usize>());
    let mut next = || it.next().unwrap();
    let n = next();
    let m = next();
    let q = next();
    let e = (0..m).map(|_| (next() - 1, next() - 1)).collect();
    let ask = (0..q).map(|_| (next() - 1, next() - 1)).collect();
    (n, e, ask)
}

// ---------- begin CSR ----------
pub struct CSR {
    size: usize,
    pos: Vec<u32>,
    list: Vec<u32>,
}

impl CSR {
    pub fn new<I>(size: usize, it: I) -> Self
    where
        I: Iterator<Item = (usize, usize)> + Clone,
    {
        let mut pos = vec![0; size + 1];
        for (s, t) in it.clone() {
            assert!(s < size && t < size);
            pos[s + 1] += 1;
        }
        for i in 1..=size {
            pos[i] += pos[i - 1];
        }
        let mut x = pos[..size].to_vec();
        let mut list = vec![0; pos[size] as usize];
        for (s, t) in it {
            let x = &mut x[s];
            list[*x as usize] = t as u32;
            *x += 1;
        }
        CSR { size, pos, list }
    }
    pub fn list(&self, v: usize) -> impl Iterator<Item = usize> + '_ {
        assert!(v < self.size);
        let s = self.pos[v] as usize;
        let t = self.pos[v + 1] as usize;
        self.list[s..t].iter().map(|p| *p as usize)
    }
}
// ---------- end CSR ----------
//---------- begin union_find ----------
pub struct DSU {
    p: Vec<i32>,
}
impl DSU {
    pub fn new(n: usize) -> DSU {
        assert!(n < std::i32::MAX as usize);
        DSU { p: vec![-1; n] }
    }
    pub fn init(&mut self) {
        self.p.iter_mut().for_each(|p| *p = -1);
    }
    pub fn root(&self, mut x: usize) -> usize {
        assert!(x < self.p.len());
        while self.p[x] >= 0 {
            x = self.p[x] as usize;
        }
        x
    }
    pub fn same(&self, x: usize, y: usize) -> bool {
        assert!(x < self.p.len() && y < self.p.len());
        self.root(x) == self.root(y)
    }
    pub fn unite(&mut self, x: usize, y: usize) -> Option<(usize, usize)> {
        assert!(x < self.p.len() && y < self.p.len());
        let mut x = self.root(x);
        let mut y = self.root(y);
        if x == y {
            return None;
        }
        if self.p[x] > self.p[y] {
            std::mem::swap(&mut x, &mut y);
        }
        self.p[x] += self.p[y];
        self.p[y] = x as i32;
        Some((x, y))
    }
}
//---------- end union_find ----------
0