結果

問題 No.2319 Friends+
ユーザー koba-e964koba-e964
提出日時 2023-05-29 23:56:02
言語 Rust
(1.77.0)
結果
TLE  
実行時間 -
コード長 3,621 bytes
コンパイル時間 1,911 ms
コンパイル使用メモリ 160,048 KB
実行使用メモリ 17,912 KB
最終ジャッジ日時 2023-08-28 00:13:27
合計ジャッジ時間 28,011 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2,892 ms
16,824 KB
testcase_03 AC 2,738 ms
16,848 KB
testcase_04 AC 2,405 ms
16,796 KB
testcase_05 AC 2,419 ms
16,860 KB
testcase_06 AC 2,635 ms
16,820 KB
testcase_07 AC 200 ms
17,324 KB
testcase_08 AC 205 ms
17,380 KB
testcase_09 AC 202 ms
17,388 KB
testcase_10 AC 216 ms
17,352 KB
testcase_11 AC 202 ms
17,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 156 ms
15,268 KB
testcase_19 AC 164 ms
17,348 KB
testcase_20 AC 169 ms
17,052 KB
testcase_21 AC 290 ms
16,864 KB
testcase_22 AC 170 ms
17,084 KB
testcase_23 AC 172 ms
17,316 KB
testcase_24 AC 165 ms
17,380 KB
testcase_25 AC 167 ms
17,316 KB
testcase_26 AC 166 ms
17,392 KB
testcase_27 AC 166 ms
17,380 KB
testcase_28 AC 166 ms
17,392 KB
testcase_29 AC 181 ms
17,348 KB
testcase_30 AC 167 ms
17,128 KB
testcase_31 AC 163 ms
17,128 KB
testcase_32 AC 352 ms
16,560 KB
testcase_33 AC 318 ms
16,328 KB
testcase_34 AC 226 ms
15,732 KB
testcase_35 AC 195 ms
15,280 KB
testcase_36 AC 293 ms
17,912 KB
testcase_37 TLE -
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 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 as u32);
            g[b].push(a as u32);
        }
    }
    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 as usize].len(), w as usize) {
                push[i].push(w);
                add(&mut pushed[w as usize], 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 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