結果

問題 No.2319 Friends+
ユーザー hliuser1hliuser1
提出日時 2023-12-25 16:41:57
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,993 bytes
コンパイル時間 2,813 ms
コンパイル使用メモリ 162,880 KB
実行使用メモリ 72,064 KB
最終ジャッジ日時 2023-12-25 16:42:35
合計ジャッジ時間 35,438 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 1,205 ms
72,064 KB
testcase_03 AC 1,218 ms
72,064 KB
testcase_04 AC 1,211 ms
72,064 KB
testcase_05 AC 1,240 ms
72,064 KB
testcase_06 AC 1,231 ms
72,064 KB
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 AC 2 ms
6,676 KB
testcase_18 AC 757 ms
22,016 KB
testcase_19 AC 708 ms
21,888 KB
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 AC 733 ms
21,888 KB
testcase_24 AC 719 ms
21,888 KB
testcase_25 AC 756 ms
21,888 KB
testcase_26 AC 748 ms
21,888 KB
testcase_27 AC 726 ms
21,888 KB
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 AC 847 ms
42,368 KB
testcase_38 RE -
testcase_39 RE -
testcase_40 RE -
testcase_41 RE -
testcase_42 RE -
testcase_43 RE -
testcase_44 RE -
testcase_45 AC 981 ms
41,728 KB
testcase_46 AC 605 ms
37,632 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
using namespace std;

using HashSet = unordered_set<int>;
using FewCounter = unordered_map<int, vector<int>>;

signed main() {
  int N, M, Q;
  cin >> N >> M;
  int K = sqrt(M);    
  vector<int>  world(N+1);      // world[i] = the world that person i is in
  FewCounter   num_few_of;  // num_few_of[i].at(w) = # friends of i in world j with < K friends
  vector<int>  many;            // many = [those with >= K friends]
  vector<HashSet> friends(N+1);

  auto is_many = [&] (int i) { 
    return friends[i].size() >= K;
  };

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

  for (int i = 1; i <= M; ++i) {
    int a, b; 
    cin >> a >> b;
    friends[a].insert(b);
    friends[b].insert(a);
  }

  for (int i = 1; i <= N; ++i) {
    if (!is_many(i)) continue;
    many.push_back(i);
    num_few_of[i].assign(N+1, 0);
    for (int f : friends[i]) 
      if (!is_many(f)) {
        int w = world[f];
        ++num_few_of[i].at(w);
      }
  }

  auto solve_many = [&] (int x, int y) {
    int new_world = world[y];
    if (num_few_of[x].at(new_world) > 0)
      goto success;

    for (int m : many) 
      if (friends[x].count(m) && world[m] == new_world) 
        goto success;
      
    return "No\n";

    success:
    world[x] = new_world;
    return "Yes\n";
  };

  auto solve_few = [&] (int x, int y) {
    for (int f : friends[x]) 
      if (world[f] == world[y]) 
        goto success;

    return "No\n";

    success:
    int old_world = world[x];
    int new_world = world[y];
    for (int f : friends[x]) {
      if (is_many(f)) continue;
      --num_few_of[f].at(old_world);
      ++num_few_of[f].at(new_world);
    }
    world[x] = new_world;
    return "Yes\n";
  };

  cin >> Q;
  while (Q--) {
    int x, y;
    cin >> x >> y;
    if (world[x] == world[y]) {
      cout << "No\n";
      continue;
    }
    auto ans = is_many(x) ? solve_many(x,y) : solve_few(x,y);
    cout << ans;
  }
}
0