結果

問題 No.2319 Friends+
ユーザー 👑 amentorimaruamentorimaru
提出日時 2023-03-14 19:57:12
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,285 bytes
コンパイル時間 1,281 ms
コンパイル使用メモリ 118,164 KB
実行使用メモリ 26,300 KB
最終ジャッジ日時 2023-10-18 13:23:44
合計ジャッジ時間 25,102 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 478 ms
14,004 KB
testcase_03 AC 479 ms
14,004 KB
testcase_04 AC 484 ms
14,004 KB
testcase_05 AC 476 ms
14,004 KB
testcase_06 AC 478 ms
14,004 KB
testcase_07 AC 533 ms
14,268 KB
testcase_08 AC 530 ms
14,268 KB
testcase_09 AC 527 ms
14,268 KB
testcase_10 AC 528 ms
14,268 KB
testcase_11 AC 535 ms
14,268 KB
testcase_12 AC 3 ms
4,348 KB
testcase_13 AC 3 ms
4,348 KB
testcase_14 AC 3 ms
4,348 KB
testcase_15 AC 3 ms
4,348 KB
testcase_16 AC 3 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 474 ms
14,796 KB
testcase_19 AC 479 ms
15,060 KB
testcase_20 AC 466 ms
14,268 KB
testcase_21 AC 467 ms
14,268 KB
testcase_22 AC 474 ms
14,268 KB
testcase_23 AC 478 ms
14,532 KB
testcase_24 AC 468 ms
14,532 KB
testcase_25 AC 466 ms
14,268 KB
testcase_26 AC 465 ms
14,268 KB
testcase_27 AC 471 ms
14,268 KB
testcase_28 AC 507 ms
14,268 KB
testcase_29 AC 490 ms
14,268 KB
testcase_30 AC 479 ms
14,268 KB
testcase_31 AC 471 ms
14,268 KB
testcase_32 AC 469 ms
14,268 KB
testcase_33 AC 465 ms
14,268 KB
testcase_34 AC 462 ms
14,268 KB
testcase_35 AC 460 ms
14,268 KB
testcase_36 AC 484 ms
14,268 KB
testcase_37 TLE -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<algorithm>
#include<vector>
#include <iterator>
#include <unordered_set>
#include <math.h>
using namespace std;

using ll = long long;
#define FOR(i, a, b) for(ll i=(a); i<(b);i++)
#define REP(i, n) for(ll i=0; i<(n);i++)
#define INC(a) for(auto& v:a)v++;
#define DEC(a) for(auto& v:a)v--;
#define VL vector<ll>
#define VVL vector<vector<ll>>
template<typename T = ll>
vector<T> read(size_t n) {
  vector<T> ts(n);
  for (size_t i = 0; i < n; i++) cin >> ts[i];
  return ts;
}

template<typename TV, const ll N> void read_tuple_impl(TV&) {}
template<typename TV, const ll N, typename Head, typename... Tail>
void read_tuple_impl(TV& ts) {
  get<N>(ts).emplace_back(*(istream_iterator<Head>(cin)));
  read_tuple_impl<TV, N + 1, Tail...>(ts);
}
template<typename... Ts> decltype(auto) read_tuple(size_t n) {
  tuple<vector<Ts>...> ts;
  for (size_t i = 0; i < n; i++) read_tuple_impl<decltype(ts), 0, Ts...>(ts);
  return ts;
}


int main() {
  ll N, M;
  cin >> N >> M;
  auto P = read(N);
  DEC(P);
  auto [A, B] = read_tuple<ll, ll>(M);
  DEC(A);
  DEC(B);
  VL C(N);
  REP(i, M) {
    C[A[i]]++;
    C[B[i]]++;
  }
  VVL few(N), many(N);
  ll sqm = sqrt(M);
  REP(i, M) {
    if (C[B[i]] < sqm)
      few[A[i]].push_back(B[i]);
    else
      many[A[i]].push_back(B[i]);
    if (C[A[i]] < sqm)
      few[B[i]].push_back(A[i]);
    else
      many[B[i]].push_back(A[i]);
  }
  vector<unordered_multiset<ll>> s(N);
  REP(i, N) {
    if (C[i] < sqm)
      continue;
    for (auto& v : few[i])
      s[i].insert(P[v]);
  }
  ll Q;
  cin >> Q;
  while (Q--) {
    ll X, Y;
    cin >> X >> Y;
    X--; Y--;
    if (P[X] == P[Y]) {
      cout << "No\n";
      continue;
    }
    ll cnt = 0;
    for (auto& v : many[X]) {
      cnt += P[v] == P[Y];
      if (cnt)
        break;
    }
    if (C[X] < sqm) {
      for (auto& v : few[X]) {
        cnt += P[v] == P[Y];
        if (cnt)
          break;
      }
    }
    else if (cnt == 0) {      
      cnt += s[X].find(P[Y]) != s[X].end();
    }
    if (cnt) {
      cout << "Yes\n";
      if (C[X] < sqm) {
        for (auto& v : many[X]) {
          s[v].erase(s[v].find(P[X]));
          s[v].insert(P[Y]);
        }
      }
      P[X] = P[Y];
    }
    else {
      cout << "No\n";
    }
  }

  return 0;
}

0