結果

問題 No.2293 無向辺 2-SAT
ユーザー koba-e964koba-e964
提出日時 2023-07-03 10:07:32
言語 Rust
(1.77.0)
結果
AC  
実行時間 390 ms / 4,000 ms
コード長 3,537 bytes
コンパイル時間 8,047 ms
コンパイル使用メモリ 140,200 KB
実行使用メモリ 12,716 KB
最終ジャッジ日時 2023-09-24 07:05:10
合計ジャッジ時間 40,067 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 7 ms
9,724 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 354 ms
12,716 KB
testcase_04 AC 365 ms
9,748 KB
testcase_05 AC 347 ms
5,880 KB
testcase_06 AC 341 ms
5,892 KB
testcase_07 AC 264 ms
9,788 KB
testcase_08 AC 357 ms
9,720 KB
testcase_09 AC 344 ms
9,772 KB
testcase_10 AC 337 ms
9,776 KB
testcase_11 AC 365 ms
9,776 KB
testcase_12 AC 356 ms
9,764 KB
testcase_13 AC 313 ms
4,376 KB
testcase_14 AC 316 ms
4,376 KB
testcase_15 AC 304 ms
4,376 KB
testcase_16 AC 364 ms
9,788 KB
testcase_17 AC 381 ms
10,064 KB
testcase_18 AC 360 ms
9,696 KB
testcase_19 AC 176 ms
5,896 KB
testcase_20 AC 362 ms
9,792 KB
testcase_21 AC 357 ms
9,784 KB
testcase_22 AC 370 ms
9,976 KB
testcase_23 AC 388 ms
10,012 KB
testcase_24 AC 372 ms
10,020 KB
testcase_25 AC 379 ms
10,028 KB
testcase_26 AC 375 ms
9,992 KB
testcase_27 AC 375 ms
10,048 KB
testcase_28 AC 378 ms
9,980 KB
testcase_29 AC 376 ms
10,048 KB
testcase_30 AC 379 ms
10,028 KB
testcase_31 AC 386 ms
9,968 KB
testcase_32 AC 341 ms
9,736 KB
testcase_33 AC 341 ms
9,780 KB
testcase_34 AC 350 ms
9,776 KB
testcase_35 AC 349 ms
9,720 KB
testcase_36 AC 350 ms
9,732 KB
testcase_37 AC 340 ms
9,772 KB
testcase_38 AC 341 ms
9,748 KB
testcase_39 AC 348 ms
9,772 KB
testcase_40 AC 340 ms
9,736 KB
testcase_41 AC 336 ms
9,776 KB
testcase_42 AC 334 ms
9,764 KB
testcase_43 AC 338 ms
9,748 KB
testcase_44 AC 343 ms
9,784 KB
testcase_45 AC 351 ms
9,764 KB
testcase_46 AC 353 ms
9,736 KB
testcase_47 AC 360 ms
9,696 KB
testcase_48 AC 375 ms
9,724 KB
testcase_49 AC 357 ms
9,716 KB
testcase_50 AC 363 ms
9,768 KB
testcase_51 AC 368 ms
9,696 KB
testcase_52 AC 379 ms
9,856 KB
testcase_53 AC 382 ms
10,020 KB
testcase_54 AC 380 ms
10,172 KB
testcase_55 AC 390 ms
10,396 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
use std::io::Read;

fn get_word() -> String {
    let stdin = std::io::stdin();
    let mut stdin=stdin.lock();
    let mut u8b: [u8; 1] = [0];
    loop {
        let mut buf: Vec<u8> = Vec::with_capacity(16);
        loop {
            let res = stdin.read(&mut u8b);
            if res.unwrap_or(0) == 0 || u8b[0] <= b' ' {
                break;
            } else {
                buf.push(u8b[0]);
            }
        }
        if buf.len() >= 1 {
            let ret = String::from_utf8(buf).unwrap();
            return ret;
        }
    }
}

fn get<T: std::str::FromStr>() -> T { get_word().parse().ok().unwrap() }

// Union-Find tree.
// Verified by https://atcoder.jp/contests/pakencamp-2019-day3/submissions/9253305
struct UnionFind { disj: Vec<usize>, rank: Vec<usize> }

impl UnionFind {
    fn new(n: usize) -> Self {
        let disj = (0..n).collect();
        UnionFind { disj: disj, rank: vec![1; n] }
    }
    fn root(&mut self, x: usize) -> usize {
        if x != self.disj[x] {
            let par = self.disj[x];
            let r = self.root(par);
            self.disj[x] = r;
        }
        self.disj[x]
    }
    fn unite(&mut self, x: usize, y: usize) {
        let mut x = self.root(x);
        let mut y = self.root(y);
        if x == y { return }
        if self.rank[x] > self.rank[y] {
            std::mem::swap(&mut x, &mut y);
        }
        self.disj[x] = y;
        self.rank[y] += self.rank[x];
    }
    #[allow(unused)]
    fn is_same_set(&mut self, x: usize, y: usize) -> bool {
        self.root(x) == self.root(y)
    }
    #[allow(unused)]
    fn size(&mut self, x: usize) -> usize {
        let x = self.root(x);
        self.rank[x]
    }
}

// https://yukicoder.me/problems/no/2293 (3)
// クリアなしであれば UnionFind でできる。クリアする時、UnionFind を初期状態に戻す必要があるが、
// そのとき変更すべき場所は今まで UnionFind に unite の引数として与えた頂点だけである。
// よって、クエリ 1 個につき高々 1 回そのようなクリアを行う必要があるため、合計 O(Q alpha(N))-time でできる。
fn main() {
    let n: usize = get();
    let q: usize = get();
    let mut pw2 = vec![0; n + 1];
    pw2[0] = 1;
    const MOD: i64 = 998_244_353;
    for i in 1..n + 1 {
        pw2[i] = pw2[i - 1] * 2 % MOD;
    }
    let mut uf = UnionFind::new(2 * n);
    let mut conn = n;
    let mut contra = false;
    let mut seen = vec![];
    for _ in 0..q {
        let ty: i32 = get();
        if ty == 3 {
            for &v in &seen {
                uf.disj[v] = v;
                uf.rank[v] = 1;
            }
            conn = n;
            contra = false;
            seen.clear();
            println!("{}", pw2[n]);
            continue;
        }
        let u = get::<usize>() - 1;
        let v = get::<usize>() - 1;
        if !contra {
            let x = (ty - 1) as usize;
            if !uf.is_same_set(2 * u, 2 * v + x) {
                uf.unite(2 * u, 2 * v + x);
                uf.unite(2 * u + 1, 2 * v + 1 - x);
                seen.push(2 * u);
                seen.push(2 * u + 1);
                seen.push(2 * v);
                seen.push(2 * v + 1);
                conn -= 1;
            }
            if uf.is_same_set(2 * u, 2 * u + 1) {
                contra = true;
            }
        }
        println!("{}", if contra { 0 } else { pw2[conn] });
    }
}
0