結果

問題 No.2319 Friends+
ユーザー koba-e964koba-e964
提出日時 2023-05-29 22:51:03
言語 Rust
(1.77.0)
結果
TLE  
実行時間 -
コード長 3,495 bytes
コンパイル時間 2,819 ms
コンパイル使用メモリ 151,924 KB
実行使用メモリ 5,008 KB
最終ジャッジ日時 2023-08-28 00:01:20
合計ジャッジ時間 9,036 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 TLE -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
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 BTreeMap<usize, i32>, col: usize, delta: i32) {
    let new = *s.get(&col).unwrap_or(&0) + delta;
    s.insert(col, new);
}

// 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: [usize; 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);
                add(&mut pushed[w], p[i], 1);
            }
        }
    }
    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] == 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], p[x], -1);
        }
        p[x] = p[y];
        for &w in &push[x] {
            add(&mut pushed[w], p[x], 1);
        }
    }
}
0