QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#587032 | #7181. Graph Cuts | tien_noob | RE | 16ms | 261036kb | C++14 | 4.9kb | 2024-09-24 17:11:30 | 2024-09-24 17:11:32 |
Judging History
answer
//Make CSP great again
//Vengeance
#include <bits/stdc++.h>
#define TASK "TESTCODE"
using namespace std;
const int N = 1e5, block = 310;
bool del[N + 1];
struct controller
{
int sum[(N + block - 1)/block + 1];
int cur[N + 1], total;
controller()
{
memset(sum, 0, sizeof(sum));
memset(cur, 0, sizeof(cur));
total = 0;
}
void toggle(int i)
{
if (del[i])
{
return ;
}
int b = (i + block - 1)/block;
if (cur[i])
{
cur[i] = 0;
--total;
--sum[b];
}
else
{
cur[i] = 1;
++total;
++sum[b];
}
}
int ans()
{
if (total == 0)
{
return 0;
}
for (int l = 1, b = 1; l <= N; l += block, ++ b)
{
if (sum[b] == 0)
{
continue;
}
for (int r = l; r <= min(N, l + block - 1); ++ r)
{
if (cur[r])
{
return r;
}
}
assert(false);
}
}
};
int x[N + 1], y[N + 1];
vector<int> adj[N + 1];
int n, m;
vector<int> big;
controller s, b[2][(N + block - 1)/block + 1];
bool in_v[N + 1];
int indx[N + 1];
void read()
{
cin >> n >> m;
for (int i = 1; i <= m; ++ i)
{
int u, v;
cin >> u >> v;
x[i] = u;
y[i] = v;
adj[u].push_back(i);
adj[v].push_back(i);
}
}
void solve()
{
for (int u = 1, id = 0; u <= n; ++ u)
{
if (adj[u].size() > block)
{
indx[u] = id;
big.push_back(u);
vector<int> t1, t2;
for (int i : adj[u])
{
int v = x[i] ^ y[i] ^ u;
b[1][id].toggle(i);
if (adj[v].size() > block)
{
t1.push_back(i);
}
else
{
t2.push_back(i);
}
}
adj[u].clear();
adj[u].insert(end(adj[u]), begin(t1), end(t1));
adj[u].insert(end(adj[u]), begin(t2), end(t2));
// sort big child first
++id;
}
}
int q;
cin >> q;
while(q--)
{
char c;
cin >> c;
if (c != '?')
{
int u;
cin >> u;
if (adj[u].size() <= block) // small
{
for (int i : adj[u])
{
int v = x[i] ^ y[i] ^ u;
if (adj[v].size() <= block) // small - small
{
s.toggle(i);
}
else // big - small
{
int id_v = indx[v];
b[0][id_v].toggle(i);
b[1][id_v].toggle(i);
}
}
}
else //big
{
for (int i : adj[u])
{
int v = x[i] ^ y[i] ^ u;
if (adj[v].size() <= block) // at most sqrt big child
{
break;
}
int id_v = indx[v];
b[0][id_v].toggle(i);
b[1][id_v].toggle(i);
}
}
in_v[u] ^= 1;
}
else
{
int res = s.ans();
if (res != 0)
{
s.toggle(res);
del[res] = true;
cout << res << '\n';
continue;
}
for (int u : big)
{
int id_u = indx[u], cur_u = in_v[u];
res = b[cur_u][id_u].ans();
if (res != 0)
{
int v = x[res] ^ y[res] ^ u;
assert(in_v[v] != in_v[u]);
b[cur_u][id_u].toggle(res);
if (adj[v].size() > block)
{
int id_v = indx[v], cur_v = in_v[v];
b[cur_v][id_v].toggle(res);
}
del[res] = true;
break;
}
}
cout << res << '\n';
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
if (fopen(TASK".INP", "r"))
{
freopen(TASK".INP", "r", stdin);
//freopen(TASK".OUT", "w", stdout);
}
int t = 1;
bool typetest = false;
if (typetest)
{
cin >> t;
}
for (int __ = 1; __ <= t; ++ __)
{
//cout << "Case " << __ << ": ";
read();
solve();
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 16ms
memory: 260252kb
input:
4 5 1 2 1 3 1 4 2 3 2 4 10 + 1 + 2 ? ? ? ? ? - 2 ? ?
output:
2 3 4 5 0 1 0
result:
ok q=10
Test #2:
score: 0
Accepted
time: 7ms
memory: 260292kb
input:
0 0 0
output:
result:
ok q=0
Test #3:
score: 0
Accepted
time: 15ms
memory: 260500kb
input:
0 0 1 ?
output:
0
result:
ok q=1
Test #4:
score: 0
Accepted
time: 7ms
memory: 261036kb
input:
1000 2000 1 50 1 88 331 1 1 352 1 497 2 32 2 282 550 2 989 2 334 3 3 665 4 38 4 69 4 343 4 451 589 4 917 4 89 5 5 162 675 5 681 6 7 22 127 7 7 592 7 672 787 7 8 310 107 9 9 137 184 9 9 244 378 9 446 9 9 658 883 9 65 10 75 10 414 10 10 468 686 10 245 11 269 11 11 386 403 11 493 11 394 12 493 12 565 1...
output:
1 4 5 8 9 10 12 14 16 18 19 25 27 29 33 38 39 40 42 47 48 49 50 56 58 59 62 63 67 69 70 71 73 75 79 81 82 83 84 87 89 91 94 97 101 103 104 106 107 108 109 110 113 114 115 118 120 121 122 125 126 129 130 131 132 133 134 135 137 145 147 148 34 149 152 153 154 155 156 157 159 160 163 167 171 105 173 17...
result:
ok q=100000
Test #5:
score: -100
Runtime Error
input:
447 99681 2 1 1 3 4 1 1 5 1 6 1 7 1 8 9 1 10 1 1 11 1 12 1 13 1 14 1 15 1 16 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 1 1 26 27 1 28 1 1 29 30 1 31 1 1 32 33 1 1 34 1 35 36 1 37 1 38 1 39 1 40 1 1 41 1 42 43 1 44 1 45 1 46 1 1 47 48 1 49 1 1 50 1 51 1 52 53 1 54 1 55 1 1 56 57 1 1 58 59 1 60 1 1 6...