結果
問題 | No.2319 Friends+ |
ユーザー | hliuser1 |
提出日時 | 2023-12-25 18:44:16 |
言語 | C++23 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 1,912 ms / 3,000 ms |
コード長 | 4,473 bytes |
コンパイル時間 | 4,029 ms |
コンパイル使用メモリ | 277,984 KB |
実行使用メモリ | 72,064 KB |
最終ジャッジ日時 | 2024-09-27 14:35:36 |
合計ジャッジ時間 | 34,133 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,816 KB |
testcase_02 | AC | 1,420 ms
72,064 KB |
testcase_03 | AC | 1,430 ms
72,064 KB |
testcase_04 | AC | 1,405 ms
72,064 KB |
testcase_05 | AC | 1,420 ms
72,064 KB |
testcase_06 | AC | 1,399 ms
71,936 KB |
testcase_07 | AC | 898 ms
21,760 KB |
testcase_08 | AC | 869 ms
21,888 KB |
testcase_09 | AC | 868 ms
21,632 KB |
testcase_10 | AC | 866 ms
21,760 KB |
testcase_11 | AC | 840 ms
21,760 KB |
testcase_12 | AC | 2 ms
6,940 KB |
testcase_13 | AC | 3 ms
6,944 KB |
testcase_14 | AC | 3 ms
6,944 KB |
testcase_15 | AC | 3 ms
6,940 KB |
testcase_16 | AC | 2 ms
6,940 KB |
testcase_17 | AC | 2 ms
6,940 KB |
testcase_18 | AC | 359 ms
22,016 KB |
testcase_19 | AC | 352 ms
22,016 KB |
testcase_20 | AC | 400 ms
21,888 KB |
testcase_21 | AC | 387 ms
22,016 KB |
testcase_22 | AC | 426 ms
21,888 KB |
testcase_23 | AC | 371 ms
21,888 KB |
testcase_24 | AC | 358 ms
21,888 KB |
testcase_25 | AC | 362 ms
21,888 KB |
testcase_26 | AC | 353 ms
21,888 KB |
testcase_27 | AC | 361 ms
21,888 KB |
testcase_28 | AC | 689 ms
21,888 KB |
testcase_29 | AC | 500 ms
21,888 KB |
testcase_30 | AC | 436 ms
21,888 KB |
testcase_31 | AC | 400 ms
22,016 KB |
testcase_32 | AC | 385 ms
21,888 KB |
testcase_33 | AC | 388 ms
21,888 KB |
testcase_34 | AC | 387 ms
21,888 KB |
testcase_35 | AC | 382 ms
21,888 KB |
testcase_36 | AC | 188 ms
21,632 KB |
testcase_37 | AC | 1,573 ms
42,368 KB |
testcase_38 | AC | 378 ms
21,888 KB |
testcase_39 | AC | 381 ms
21,888 KB |
testcase_40 | AC | 384 ms
21,888 KB |
testcase_41 | AC | 399 ms
21,888 KB |
testcase_42 | AC | 393 ms
22,016 KB |
testcase_43 | AC | 409 ms
21,760 KB |
testcase_44 | AC | 409 ms
21,888 KB |
testcase_45 | AC | 1,912 ms
41,728 KB |
testcase_46 | AC | 284 ms
37,632 KB |
ソースコード
// https://yukicoder.me/problems/no/2319 // really cool problem // // Solution 0. use bitsets and bruteforce // // 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. sqrt over queries #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() { cin.tie(0)->sync_with_stdio(0); // cout << "sol 1\n"; // freopen("/Users/drymerge/CLionProjects/DEBUG/a.in", "r", stdin); // freopen("/Users/drymerge/a.out", "w", stdout); 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; } } signed main2() { cout << "sol2\n"; freopen("/Users/drymerge/CLionProjects/DEBUG/a.in", "r", stdin); freopen("/Users/drymerge/b.out", "w", stdout); cin.tie(0)->sync_with_stdio(0); int N, M, Q; cin >> N >> M; vector<int> world(N+1); for (int i = 1; i <= N; ++i) cin >> world[i]; vector<unordered_set<int>> friends(N+1); vector<array<int,2>> friendships(M); unordered_map<int, unordered_map<int, int>> initial_count; // initial_count[i][j] = # of friends of i in world j for (auto& [a,b] : friendships) { cin >> a >> b; friends[a].insert(b); friends[b].insert(a); } auto reset_map = [&] { initial_count.clear(); for (auto [a,b] : friendships) { ++initial_count[a][world[b]]; ++initial_count[b][world[a]]; } }; auto is_friends = [&] (int i, int j) -> bool { if (friends[i].size() > friends[j].size()) swap(i, j); return friends[i].count(j); }; cin >> Q; int K = sqrt(Q); int q = 0; vector<array<int,4>> succeeded; while (q < Q) { reset_map(); for (int k = 0; k < K && q < Q; ++k, ++q) { int x, y; cin >> x >> y; if (world[x] == world[y]) { cout << "No\n"; continue; } int start = initial_count[x][world[y]]; for (auto [xl,yl,wx,wy] : succeeded) { if (wx == world[y] && is_friends(x, xl)) --start; if (wy == world[y] && is_friends(x, xl)) ++start; } if (start > 0) { cout << "Yes\n"; succeeded.push_back( {x,y,world[x],world[y]} ); world[x] = world[y]; } else cout << "No\n"; } succeeded.clear(); } return 0; }