結果

問題 No.2319 Friends+
ユーザー koba-e964koba-e964
提出日時 2023-05-29 23:39:44
言語 Rust
(1.77.0)
結果
TLE  
実行時間 -
コード長 3,600 bytes
コンパイル時間 2,310 ms
コンパイル使用メモリ 162,744 KB
実行使用メモリ 22,980 KB
最終ジャッジ日時 2023-08-28 00:10:08
合計ジャッジ時間 30,520 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2,792 ms
19,512 KB
testcase_03 AC 2,652 ms
19,524 KB
testcase_04 AC 2,316 ms
19,516 KB
testcase_05 AC 2,315 ms
19,428 KB
testcase_06 AC 2,562 ms
19,512 KB
testcase_07 AC 187 ms
19,772 KB
testcase_08 AC 187 ms
19,728 KB
testcase_09 AC 186 ms
19,748 KB
testcase_10 AC 186 ms
19,764 KB
testcase_11 AC 188 ms
19,772 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 148 ms
17,424 KB
testcase_19 AC 158 ms
19,628 KB
testcase_20 AC 160 ms
19,248 KB
testcase_21 AC 280 ms
18,920 KB
testcase_22 AC 159 ms
19,420 KB
testcase_23 AC 160 ms
19,760 KB
testcase_24 AC 159 ms
19,720 KB
testcase_25 AC 159 ms
19,740 KB
testcase_26 AC 159 ms
19,764 KB
testcase_27 AC 159 ms
19,732 KB
testcase_28 AC 159 ms
19,428 KB
testcase_29 AC 160 ms
19,452 KB
testcase_30 AC 159 ms
19,516 KB
testcase_31 AC 161 ms
19,232 KB
testcase_32 AC 346 ms
18,992 KB
testcase_33 AC 312 ms
18,400 KB
testcase_34 AC 222 ms
17,832 KB
testcase_35 AC 188 ms
17,392 KB
testcase_36 AC 289 ms
20,308 KB
testcase_37 TLE -
testcase_38 AC 188 ms
19,756 KB
testcase_39 AC 210 ms
19,764 KB
testcase_40 AC 261 ms
19,680 KB
testcase_41 AC 380 ms
19,744 KB
testcase_42 AC 619 ms
19,744 KB
testcase_43 AC 1,029 ms
19,776 KB
testcase_44 AC 2,031 ms
19,984 KB
testcase_45 TLE -
testcase_46 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::collections::*;
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"));
}

fn add(s: &mut HashMap<u32, i32>, col: u32, delta: i32) {
    let new = *s.get(&col).unwrap_or(&0) + delta;
    if new != 0 {
        s.insert(col, new);
    } else {
        s.remove(&col);
    }
}

// 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: [u32; n],
        ab: [(usize1, usize1); m],
        q: usize,
        xy: [(usize1, usize1); q],
    }
    let mut p = p;
    let mut g = vec![vec![]; n];
    for &(a, b) in &ab {
        if a != b {
            g[a].push(b);
            g[b].push(a);
        }
    }
    let mut push = vec![vec![]; n];
    let mut pushed = vec![Default::default(); n];
    for i in 0..n {
        for &w in &g[i] {
            if (g[i].len(), i) <= (g[w].len(), w) {
                push[i].push(w as u32);
                add(&mut pushed[w], p[i], 1);
            }
        }
    }
    drop(g);
    for (x, y) in xy {
        let mut found = false;
        if *pushed[x].get(&p[y]).unwrap_or(&0) != 0 {
            found = true;
        } else {
            for &w in &push[x] {
                if p[w as usize] == p[y] {
                    found = true;
                    break;
                }
            }
        }
        if !found || p[x] == p[y] {
            puts!("No\n");
            continue;
        }
        puts!("Yes\n");
        for &w in &push[x] {
            add(&mut pushed[w as usize], p[x], -1);
        }
        p[x] = p[y];
        for &w in &push[x] {
            add(&mut pushed[w as usize], p[x], 1);
        }
    }
}
0