結果

問題 No.2319 Friends+
ユーザー hliuser1hliuser1
提出日時 2023-12-26 06:13:45
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,340 bytes
コンパイル時間 5,869 ms
コンパイル使用メモリ 308,628 KB
実行使用メモリ 58,984 KB
最終ジャッジ日時 2024-09-27 14:58:03
合計ジャッジ時間 24,252 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 20 ms
56,928 KB
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 19 ms
56,928 KB
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
testcase_40 RE -
testcase_41 RE -
testcase_42 RE -
testcase_43 RE -
testcase_44 RE -
testcase_45 RE -
testcase_46 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

// https://yukicoder.me/problems/no/2319
// really cool problem
//
// Solution 0. use bitsets and bruteforce (cheating)
//
// Solution 1. sqrt over nodes
// let K = sqrt(M)
// a person is "few" if it has < K friends and "many" if >= K
// there are at most 2M / K ~= K people that are "many"
// to solve a query where x is few, brute force it
// to solve a query where x is many, consider its friends that are:
//     a. many - you can check ALL many's for the condition
//     b. few  - store this as map f[i][j] = # of few friends of the many i in world j
// update naively, it works in O(Q*sqrt M + N*sqrt M + M)
//
// Solution 2. ft. Jerry -> sqrt over queries
// for each chunk, recompute a map of for each person i, how many friends they have in world j
// then for each query, loop over the previous queries in the chunk and simulate. if by the end,
// you still have at least one friend in world[y], ur good
// the map computations take M sqrt(Q) time total, the rest is Q sqrt(Q)
#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2")
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <typename K, typename V> 
using HashTable = gp_hash_table<K,V>;

constexpr size_t MXN = 20001;
constexpr size_t MXM = 400000;
constexpr size_t SQR = 450;

#define all(x) begin(x), end(x)

signed main() {
  cin.tie(0)->sync_with_stdio(0);
  int N, M, Q;
  cin >> N >> M;
  
  short world[MXN];
  bitset<MXN> friends[MXN];
  vector<short> friendss[MXN];
  short friendships[MXM];
  short X[MXN], Y[MXN];
  HashTable<int, short> initial_count[MXN];
  // initial_count[i][j] = # of friends of i in world j

  for (int i = 1; i <= N; ++i) 
    cin >> world[i];

  for (int i = 0; i < M; ++i) {
    short a, b;
    cin >> a >> b;
    friendss[a].push_back(b);
    friendss[b].push_back(a);
    friendships[2*i] = a, friendships[2*i+1] = b;
    friends[a][b] = true;
    friends[b][a] = true;
  }

  vector<int> whos_involved;
  auto reset_map = [&] {
    sort( all(whos_involved) );
    whos_involved.resize(unique(all(whos_involved)) - begin(whos_involved));

    for (int c : whos_involved) {
      if (!initial_count[c].empty())
        initial_count[c].clear();
      for (int f : friendss[c]) {
        ++initial_count[c][world[f]];
      }
    }
  };

  cin >> Q;
  int K = sqrt(Q);
  int q = 0;
  HashTable<int,vector<int>> succeeded_wx, succeeded_wy;

  while (q < Q) {
    reset_map();
    int last = min(Q, q + K);
    for (int k = q; k < last; ++k) {
      cin >> X[k] >> Y[k];
      whos_involved.push_back(X[k]);
      whos_involved.push_back(Y[k]);
    }
    for (int k = 0; k < K && q < Q; ++k, ++q) {
      short x = X[k], y = Y[k];
      if (world[x] == world[y]) {
        cout << "No\n";
        continue;
      }

      int start = initial_count[x][world[y]];
      for (int xl : succeeded_wx[ world[y] ]) {
        start -= (friends[x][xl]);
      }
      for (int xl : succeeded_wy[ world[y] ]) {
        start += (friends[x][xl]);
      }
      if (start > 0) {
        cout << "Yes\n";
        succeeded_wx[world[x]].push_back(x);
        succeeded_wy[world[y]].push_back(x);
        world[x] = world[y];
      }
      else cout << "No\n";
    }
    succeeded_wx.clear();
    succeeded_wy.clear();
    whos_involved.clear();
  }
  return 0;
}
0