結果

問題 No.3390 Public or Private
コンテスト
ユーザー 👑 cologne
提出日時 2026-10-23 15:27:06
言語 Rust
(1.99.0 + proconio + num + itertools + ACL)
コンパイル:
/usr/bin/rustc_custom
実行:
./target/release/main
結果
AC  
実行時間 64 ms / 2,000 ms
+ 48µs
コード長 4,056 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 4,477 ms
コンパイル使用メモリ 185,404 KB
実行使用メモリ 39,168 KB
最終ジャッジ日時 2026-10-27 15:44:25
合計ジャッジ時間 9,857 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge7_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: constant `M` is never used
 --> src/main.rs:4:7
  |
4 | const M: u32 = 998_244_353;
  |       ^
  |
  = note: `#[warn(dead_code)]` (part of `#[warn(unused)]`) on by default

ソースコード

diff #
raw source code

use fio::*;
use std::collections::HashMap;

const M: u32 = 998_244_353;

fn main() {
    let [rn, m] = read_tuple::<usize, 2>();

    let mut uv = vec![];

    for _ in 0..m {
        let [u, v] = read_tuple::<i32, 2>();
        uv.push((u, v));
    }
    enum Q {
        Follow(usize, usize),
        Toggle(usize),
    }

    let mut hs: HashMap<i32, usize> = HashMap::new();
    let q = read::<usize>();
    let mut qs = vec![];

    let mut get = |i: i32| {
        let sz = hs.len();
        *hs.entry(i).or_insert(sz)
    };

    for _ in 0..q {
        let [q, a, b] = read_tuple::<i32, 3>();
        match q {
            1 => {
                let a = get(a);
                let b = get(b);
                qs.push(Q::Follow(a, b));
            }
            2 => {
                let a = get(a);
                assert_eq!(b, -1);
                qs.push(Q::Toggle(a));
            }
            _ => panic!(),
        }
    }

    let n = hs.len();

    let mut conn = vec![vec![false; n]; n];
    let mut open = vec![true; n];

    for (u, v) in uv {
        let Some(u) = hs.get(&u) else { continue };
        let Some(v) = hs.get(&v) else { continue };
        conn[*u][*v] = true;
    }
    for q in qs {
        match q {
            Q::Follow(a, b) => {
                conn[a][b] = !conn[a][b];
                println!(
                    "{}",
                    (0..n)
                        .filter(|&i| i != a && (open[i] || conn[a][i]))
                        .count()
                        + (rn - n)
                )
            }
            Q::Toggle(a) => {
                open[a] = !open[a];
                println!(
                    "{}",
                    (0..n)
                        .filter(|&i| i != a && (open[i] || conn[a][i]))
                        .count()
                        + (rn - n)
                )
            }
        }
    }
}

mod fio {
    use std::{
        cell::RefCell,
        convert::TryInto,
        fmt::Debug,
        io::{BufRead, BufWriter, StdinLock, StdoutLock, stdin, stdout},
        str::FromStr,
    };
    thread_local! {
        pub static STDIN: RefCell<StdinLock<'static>> = RefCell::new(stdin().lock());
        pub static STDOUT: RefCell<BufWriter<StdoutLock<'static>>> = RefCell::new(BufWriter::new(stdout().lock()));
    }

    #[allow(dead_code)]
    pub fn read<T: FromStr>() -> T
    where
        <T as FromStr>::Err: Debug,
    {
        read_line().parse().unwrap()
    }

    /// If there is a performance concern, use direct
    /// `read_line().spilt_ascii_whitespace().map(|x| x.parse().unwrap())`
    #[allow(dead_code)]
    pub fn read_vec<T: FromStr>() -> Vec<T>
    where
        <T as FromStr>::Err: Debug,
    {
        read_line()
            .split_ascii_whitespace()
            .map(|x| x.parse().unwrap())
            .collect()
    }

    /// If there is a performance concern, use direct
    /// `read_line().spilt_ascii_whitespace().map(|x| x.parse().unwrap())`
    #[allow(dead_code)]
    pub fn read_tuple<T, const N: usize>() -> [T; N]
    where
        T: FromStr + Debug,
        <T as FromStr>::Err: Debug,
    {
        read_vec::<T>().try_into().unwrap()
    }

    /// whitespace at the end of the line is ignored
    pub fn read_line() -> String {
        let mut s = String::new();
        STDIN.with(|cell| {
            cell.borrow_mut().read_line(&mut s).unwrap();
        });
        String::from_str(s.trim_end()).unwrap()
    }
}

#[macro_export]
macro_rules! print {
    ($($t:tt)*) => {
        fio::STDOUT.with(|cell|{
            use std::io::Write;
            write!(cell.borrow_mut(), $($t)*).unwrap()
        })};
}

#[macro_export]
macro_rules! println {
    ($($t:tt)*) => {
        fio::STDOUT.with(|cell| {
            use std::io::Write;
            writeln!(cell.borrow_mut(), $($t)*).unwrap()
        })
    };
}

#[macro_export]
macro_rules! flush {
    () => {
        fio::STDOUT.with(|cell| {
            use std::io::Write;
            cell.borrow_mut().flush().unwrap()
        });
    };
}
0