結果
問題 |
No.2319 Friends+
|
ユーザー |
|
提出日時 | 2023-12-25 16:33:04 |
言語 | C++17(clang) (17.0.6 + boost 1.87.0) |
結果 |
MLE
|
実行時間 | - |
コード長 | 2,414 bytes |
コンパイル時間 | 3,457 ms |
コンパイル使用メモリ | 167,296 KB |
実行使用メモリ | 814,592 KB |
最終ジャッジ日時 | 2024-09-27 14:28:48 |
合計ジャッジ時間 | 10,132 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | MLE * 1 -- * 44 |
ソースコード
#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 vector<bool> is_many(N+1); // is_many[i] = does person i have >= K friends 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); for (int i = 1; i <= N; ++i) { cin >> world[i]; num_few_of[i].assign(N+1, 0); } 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) { int num_friends = friends[i].size(); is_many[i] = (num_friends >= K); if (is_many[i]) many.push_back(i); } for (int i = 1; i <= N; ++i) { if (is_many[i]) 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; } }