1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
| #include <bits/stdc++.h> #include "explore.h" using namespace std; typedef pair<int,int> pii; #define FI first #define SE second #define mkp make_pair const int N = 2e5 + 5,M = 3e5 + 5; mt19937 Rnd(1919810); inline int randint(int l,int r) { return Rnd() % (r - l + 1) + l;} int n,m,mcnt; vector<int> G[N]; bool die[N]; set<pair<int,int> > reped; inline void addedge(int x,int y) { if(x > y) swap(x,y); if(x == y || reped.count(mkp(x,y))) return; reped.insert(mkp(x,y)); report(x,y);++mcnt; G[x].push_back(y);G[y].push_back(x); die[x] = check(x);die[y] = check(y); }
namespace Brute { int col[N]; inline void explore(int N,int M) { n = N;m = M; for(int i = 0;i < n - 1;i++) { modify(i);col[i] ^= 1; for(int j = i + 1;j < n;j++) if(query(j) != col[j]) col[j] ^= 1,report(i,j); } } } namespace A { int nxor[N],col[N]; inline void explore(int N,int M) { for(int i = 0;i <= __lg(n);i++) { for(int j = 0;j < n;j++) if((j >> i) & 1) modify(j); for(int j = 0;j < n;j++) if(col[j] != query(j)) nxor[j] |= (1 << i),col[j] ^= 1; } for(int i = 0;i < n;i++) nxor[i] ^= i; for(int i = 0;i < n;i++) if(i < nxor[i]) report(i,nxor[i]); } } namespace TreeB { int a[N]; void erfen(int l,int r,int ql,int qr) { if(l == r) { for(int i = ql;i <= qr;i++) report(l,a[i]);return;} int mid = l + r >> 1; for(int i = l;i <= mid;i++) modify(i); static int a1[N],a2[N]; int c1 = 0,c2 = 0; for(int i = ql;i <= qr;i++) if(a[i] <= mid || query(a[i])) a1[++c1] = a[i]; else a2[++c2] = a[i]; for(int i = l;i <= mid;i++) modify(i); for(int i = 1;i <= c1;i++) a[ql + i - 1] = a1[i]; for(int i = 1;i <= c2;i++) a[ql + c1 + i - 1] = a2[i]; erfen(l,mid,ql,ql + c1 - 1); erfen(mid + 1,r,ql + c1,qr); } void explore(int n,int m) { for(int i = 0;i < n;i++) a[i] = i; erfen(0,n - 1,1,n - 1); } } namespace Chain { int nxor[N],col[N]; inline void explore(int N,int M) { for(int i = 0;i < n;i++) modify(i); int p0; for(int i = 0;i < n;i++) if(query(i)) col[i] = 1; for(int i = 0;i < n;i++) if(!col[i]) p0 = i; for(int i = 0;i <= __lg(n);i++) { for(int j = 0;j < n;j++) if((j >> i) & 1) modify(j); for(int j = 0;j < n;j++) if(col[j] != query(j)) nxor[j] |= (1 << i),col[j] ^= 1; } for(int i = 0;i < n;i++) nxor[i] ^= i; int las = 0,nxt; for(int _ = 1;_ < n;_++) { nxt = nxor[p0] ^ las; report(p0,nxt);las = p0;p0 = nxt; } } } namespace Sol { int S[N],tot,col[N]; bool usd[N]; inline void modify2(int x) { col[x] ^= 1; for(auto y : G[x]) col[y] ^= 1; modify(x); } int a[N],b[N]; void erfen(int l,int r,int ql,int qr) { if(l == r) { for(int i = ql;i <= qr;i++) addedge(b[i],a[l]);return;} int mid = l + r >> 1; for(int i = l;i <= mid;i++) if(!die[a[i]]) modify2(a[i]),usd[a[i]] = true; static int a1[N],a2[N]; int c1 = 0,c2 = 0; for(int i = ql;i <= qr;i++) if(usd[b[i]] || query(b[i]) != col[b[i]]) a1[++c1] = b[i]; else a2[++c2] = b[i]; for(int i = l;i <= mid;i++) if(!die[a[i]]) modify2(a[i]),usd[a[i]] = false; for(int i = 1;i <= c1;i++) b[ql + i - 1] = a1[i]; for(int i = 1;i <= c2;i++) b[ql + c1 + i - 1] = a2[i]; erfen(l,mid,ql,ql + c1 - 1); erfen(mid + 1,r,ql + c1,qr); } inline void explore(int N,int M) { n = N;m = M; while(mcnt < m) { tot = 0; for(int i = 0;i < n;i++) if(!die[i]) a[++tot] = i; shuffle(a + 1,a + tot + 1,Rnd); for(int i = 1;i <= tot;i++) b[i] = a[i]; erfen(1,tot,2,tot); } } }
void explore(int N,int M) { n = N;m = M; if(n <= 500) return Brute::explore(n,m); if(n % 10 == 8) return A::explore(n,m); if(n % 10 == 7) return TreeB::explore(n,m); if(n % 10 == 6) return Chain::explore(n,m); Sol::explore(n,m); }
|