結果

問題 No.2319 Friends+
ユーザー koba-e964koba-e964
提出日時 2023-05-30 01:06:00
言語 Rust
(1.77.0)
結果
AC  
実行時間 563 ms / 3,000 ms
コード長 6,030 bytes
コンパイル時間 5,313 ms
コンパイル使用メモリ 157,828 KB
実行使用メモリ 111,432 KB
最終ジャッジ日時 2023-08-28 00:17:15
合計ジャッジ時間 21,536 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 1 ms
4,388 KB
testcase_02 AC 248 ms
110,720 KB
testcase_03 AC 247 ms
110,668 KB
testcase_04 AC 248 ms
110,712 KB
testcase_05 AC 249 ms
110,724 KB
testcase_06 AC 247 ms
110,728 KB
testcase_07 AC 520 ms
111,080 KB
testcase_08 AC 520 ms
111,156 KB
testcase_09 AC 522 ms
111,172 KB
testcase_10 AC 563 ms
111,160 KB
testcase_11 AC 531 ms
111,160 KB
testcase_12 AC 2 ms
4,504 KB
testcase_13 AC 2 ms
4,388 KB
testcase_14 AC 3 ms
4,384 KB
testcase_15 AC 2 ms
4,384 KB
testcase_16 AC 2 ms
4,384 KB
testcase_17 AC 1 ms
4,508 KB
testcase_18 AC 354 ms
111,088 KB
testcase_19 AC 317 ms
111,156 KB
testcase_20 AC 286 ms
111,144 KB
testcase_21 AC 280 ms
111,168 KB
testcase_22 AC 284 ms
111,164 KB
testcase_23 AC 305 ms
111,156 KB
testcase_24 AC 304 ms
111,164 KB
testcase_25 AC 288 ms
111,172 KB
testcase_26 AC 289 ms
111,084 KB
testcase_27 AC 284 ms
111,164 KB
testcase_28 AC 283 ms
111,152 KB
testcase_29 AC 283 ms
111,080 KB
testcase_30 AC 282 ms
111,088 KB
testcase_31 AC 282 ms
111,156 KB
testcase_32 AC 289 ms
111,160 KB
testcase_33 AC 282 ms
111,144 KB
testcase_34 AC 272 ms
111,088 KB
testcase_35 AC 258 ms
111,160 KB
testcase_36 AC 300 ms
110,900 KB
testcase_37 AC 171 ms
110,444 KB
testcase_38 AC 212 ms
111,160 KB
testcase_39 AC 211 ms
111,148 KB
testcase_40 AC 213 ms
111,160 KB
testcase_41 AC 222 ms
111,164 KB
testcase_42 AC 214 ms
111,088 KB
testcase_43 AC 212 ms
111,152 KB
testcase_44 AC 211 ms
111,164 KB
testcase_45 AC 197 ms
111,432 KB
testcase_46 AC 175 ms
110,756 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::{Write, BufWriter};
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes.by_ref().map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

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

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => { ($(read_value!($next, $t)),*) };
    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
    ($next:expr, usize1) => (read_value!($next, usize) - 1);
    ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

// Verified by https://atcoder.jp/contests/arc084/submissions/3935443
#[derive(Clone)]
struct BitSet {
    size: usize,
    buf: Vec<usize>,
}

impl BitSet {
    // size should be a multiple of bit-size of usize.
    fn new(size: usize) -> Self {
        let w = 8 * std::mem::size_of::<usize>();
        assert_eq!(size & (w - 1), 0);
        let count = size / w;
        BitSet {
            size: size,
            buf: vec![0; count],
        }
    }
    #[allow(unused)]
    fn set(&mut self, idx: usize, val: bool) {
        debug_assert!(idx < self.size);
        let w = 8 * std::mem::size_of::<usize>();
        let idx0 = idx / w;
        let idx1 = idx & (w - 1);
        if val {
            self.buf[idx0] |= 1 << idx1;
        } else {
            self.buf[idx0] &= !(1 << idx1);
        }
    }
    #[allow(unused)]
    fn get(&self, idx: usize) -> bool {
        let w = 8 * std::mem::size_of::<usize>();
        debug_assert!(idx < self.size);
        let idx0 = idx / w;
        let idx1 = idx & (w - 1);
        (self.buf[idx0] >> idx1 & 1) == 1
    }
    #[allow(unused)]
    fn shl(&self, val: usize) -> Self {
        if val >= self.size { return Self::new(self.size); }
        let w = 8 * std::mem::size_of::<usize>();
        let count = self.size / w;
        let sh0 = val / w;
        let sh1 = val & (w - 1);
        let mut ans = Self::new(self.size);
        if sh1 == 0 {
            for i in 0..count - sh0 {
                ans.buf[i + sh0] = self.buf[i];
            }
        } else {
            ans.buf[sh0] = self.buf[0] << sh1;
            for i in 1..count - sh0 {
                ans.buf[i + sh0] = self.buf[i] << sh1
                    | self.buf[i - 1] >> (w - sh1);
            }
        }
        ans
    }
    // Verified by: https://www.hackerrank.com/contests/yfkpo4/challenges/e-strange-clock/submissions/code/1357435235
    #[allow(unused)]
    fn shr(&self, val: usize) -> Self {
        if val >= self.size { return Self::new(self.size); }
        let w = 8 * std::mem::size_of::<usize>();
        let count = self.size / w;
        let sh0 = val / w;
        let sh1 = val & (w - 1);
        let mut ans = Self::new(self.size);
        if sh1 == 0 {
            for i in 0..count - sh0 {
                ans.buf[i] = self.buf[i + sh0];
            }
        } else {
            for i in 0..count - sh0 - 1 {
                ans.buf[i] = self.buf[i + sh0] >> sh1
                    | self.buf[i + sh0 + 1] << (w - sh1);
            }
            ans.buf[count - sh0 - 1] = self.buf[count - 1] >> sh1;
        }
        ans
    }
    #[allow(unused)]
    fn msb(&self) -> Option<usize> {
        let w = 8 * std::mem::size_of::<usize>();
        let count = self.size / w;
        for i in (0..count).rev() {
            let v = self.buf[i];
            if v != 0 {
                return Some(w * i + w - 1 - v.leading_zeros() as usize);
            }
        }
        None
    }
}

// https://yukicoder.me/problems/no/2319 (3.5)
// 次数の大きい頂点を偉い頂点と呼ぶことにする。辺それぞれにたいして、偉くない側が偉い側に情報を push することにする。
// 拡張点に対して自分より偉い頂点は高々 sqrt(2M) 個であることに注意すると、各操作について以下のようにデータが移動することになる。
// 位置の変更: 自分の位置を変更後、自分より偉い頂点に情報を push
// フレンドがいるかどうかの取得: 自分が持っている偉くない頂点の情報と、自分より偉い頂点の情報を取得
// 計算量は O(Q sqrt(M) log(N)) である。
// -> TLE。各頂点が持つデータを木構造のマップからハッシュマップにしても TLE。
fn main() {
    let out = std::io::stdout();
    let mut out = BufWriter::new(out.lock());
    macro_rules! puts {($($format:tt)*) => (let _ = write!(out,$($format)*););}
    input! {
        n: usize, m: usize,
        p: [usize1; n],
        ab: [(usize1, usize1); m],
        q: usize,
        xy: [(usize1, usize1); q],
    }
    let mut p = p;
    const W: usize = 20_032;
    let mut pb = vec![BitSet::new(W); n];
    for i in 0..n {
        pb[p[i]].set(i, true);
    }
    let mut g = vec![vec![]; n];
    let mut gb = vec![BitSet::new(W); n];
    for &(a, b) in &ab {
        if a != b {
            g[a].push(b as u32);
            g[b].push(a as u32);
            gb[a].set(b, true);
            gb[b].set(a, true);
        }
    }
    for (x, y) in xy {
        let mut found = false;
        for i in 0..gb[x].buf.len() {
            if (gb[x].buf[i] & pb[p[y]].buf[i]) != 0 {
                found = true;
                break;
            }
        }
        if !found || p[x] == p[y] {
            puts!("No\n");
            continue;
        }
        puts!("Yes\n");
        pb[p[x]].set(x, false);
        p[x] = p[y];
        pb[p[x]].set(x, true);
    }
}
0