tiom4eg

openol F 91

Jan 16th, 2024
857
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.90 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. // tiom4eg's precompiler options
  3. // POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS
  4. // IO settings
  5. #define fastIO ios_base::sync_with_stdio(false); cin.tie(0)
  6. // Quick types
  7. #define ll long long
  8. #define ld long double
  9. //#define ull unsigned long long
  10. #define pii pair <int, int>
  11. #define vi vector <int>
  12. #define mi vector <vector <int>>
  13. // Quick functions
  14. #define endl "\n"
  15. #define F first
  16. #define S second
  17. #define all(a) a.begin(), a.end()
  18. #define sz(a) (int)(a.size())
  19. #define pb push_back
  20. #define mp make_pair
  21. // Quick fors
  22. #define FOR(i, a, b) for (int i = a; i < b; ++i)
  23. #define FORS(i, a, b, c) for (int i = a; i < b; i += c)
  24. #define RFOR(i, a, b) for (int i = a; i >= b; --i)
  25. #define EACH(e, a) for (auto& e : a)
  26. // Pragmas
  27. #ifndef TIOM4EG
  28. #pragma GCC optimize("O3") // let the chaos begin!
  29. #pragma GCC target("avx,avx2,tune=native")
  30. #pragma GCC comment(linker, "/stack:200000000")
  31. #endif
  32. // PBDS
  33. #include <ext/pb_ds/assoc_container.hpp>
  34. #include <ext/pb_ds/tree_policy.hpp>
  35. #define pbds tree <int, null_type, less <int>, rb_tree_tag, tree_order_statistics_node_update>
  36. using namespace __gnu_pbds;
  37. // POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS
  38. using namespace std;
  39. mt19937 rng(chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count());
  40. //#define int long long
  41. constexpr int INF = 1e9 + 7, MD = 998244353, MAX = 100007, LG = 19, R = 1 << LG, MOD = 1000000007, MOD2 = 1e9 + 9, B = 256;
  42. const ll INFLL = 1e18 + 7;
  43. #undef endl
  44.  
  45.  
  46. int group;
  47.  
  48. struct opi {
  49.     int f, s;
  50.     opi() {}
  51.     opi(int x, int y) : f(min(x, y)), s(max(x, y)) {}
  52.     bool operator<(const opi o) const { return (f == o.f) ? (s < o.s) : (f < o.f); }
  53.     bool operator==(const opi o) const { return f == o.f && s == o.s; }
  54. };
  55.  
  56. template<>
  57. struct std::hash <opi> {
  58.     int operator() (opi p) const { return p.f << 16 + p.s; }
  59. };
  60.  
  61. /*
  62. global ideas:
  63. good vertex: vertex which doesn't have two found outgoing edges
  64. bad vertex: vertex which has 2+ outgoing edges
  65. first-priority edge: non-asked edge between two good vertices
  66. second-priority edge: non-asked edge which isn't first-priority
  67. */
  68.  
  69.  
  70. /*
  71. idea: try every first-priority edge while they exist, their number is <= 2n
  72. after that (it can be proven) we're left with no more than 3 good vertices
  73. bruteforce them in 3n
  74. */
  75. void solve_5n() {
  76.     int n; cin >> n;
  77.     vi cnt(n);
  78.     unordered_set <opi> kal;
  79.  
  80.     function <int(opi)> ask = [](opi e) {
  81.         cout << "? " << e.f + 1 << ' ' << e.s + 1 << endl;
  82.         string r; cin >> r;
  83.         return r[0] != 'f';
  84.     };
  85.  
  86.     FOR(i, 0, n) {
  87.         FOR(j, i + 1, n) {
  88.             if (cnt[i] > 1) break;
  89.             if (cnt[j] > 1) continue;
  90.             opi cur(i, j);
  91.             kal.insert(cur);
  92.             int d = ask(cur);
  93.             ++cnt[d ? cur.s : cur.f];
  94.         }
  95.     }
  96.  
  97.     FOR(v, 0, n) if (cnt[v] < 2) {
  98.         bool ok = 1;
  99.         FOR(u, 0, n) if (u != v && kal.find({u, v}) == kal.end()) {
  100.             opi cur(v, u);
  101.             kal.insert(cur);
  102.             int d = ask(cur);
  103.             if (v == (d ? cur.s : cur.f)) {
  104.                 if (++cnt[v] == 2) {
  105.                     ok = 0;
  106.                     break;
  107.                 }
  108.             }
  109.         }
  110.         if (ok) {
  111.             cout << "! " << v + 1 << endl;
  112.             string verdict; cin >> verdict;
  113.             return;
  114.         }
  115.     }
  116.     cout << "! -1" << endl;
  117.     string verdict; cin >> verdict;
  118. }
  119.  
  120. void solve_kukarek() {
  121.     int n; cin >> n;
  122.     bitset <250000> used;
  123.     vi cnt(n);
  124.     set <pii> q;
  125.     FOR(i, 0, n) q.insert({cnt[i], i});
  126.  
  127.     function <int(opi)> ask = [](opi e) {
  128.         cout << "? " << e.f + 1 << ' ' << e.s + 1 << endl;
  129.         string r; cin >> r;
  130.         return r[0] != 'f';
  131.     };
  132.  
  133.     FOR(iter, 0, 2000) {
  134.         pii cur = *q.begin(), now;
  135.         if (cur.F > 1) break;
  136.         q.erase(q.begin());
  137.         bool found = 0;
  138.         for (auto it = q.begin(); it != q.end(); ++it) {
  139.             if (!used[cur.S * n + it->S]) {
  140.                 now = *it;
  141.                 cout << "? " << cur.S + 1 << ' ' << now.S + 1 << endl;
  142.                 string r; cin >> r;
  143.                 int d = r[0] != 'f';
  144.                 if (d) {
  145.                     q.erase(it);
  146.                     cnt[now.S]++;
  147.                     q.insert({cnt[now.S], now.S});
  148.                 }
  149.                 else ++cnt[cur.S];
  150.                 q.insert({cnt[cur.S], cur.S});
  151.                 used[cur.S * n + now.S] = used[now.S * n + cur.S] = 1;
  152.                 found = 1;
  153.                 break;
  154.             }
  155.         }
  156.         if (!found) {
  157.             cout << "! " << cur.S + 1 << endl;
  158.             string verdict; cin >> verdict;
  159.             return;
  160.         }
  161.     }
  162.     cout << "! -1" << endl;
  163.     string verdict; cin >> verdict;
  164. }
  165.  
  166. signed main() {
  167.     fastIO;
  168.     int tc; cin >> group >> tc;
  169.     while (tc--) solve_kukarek();
  170. }
  171.  
Advertisement
Add Comment
Please, Sign In to add comment