QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#592097 | #7181. Graph Cuts | phuong2222 | RE | 3ms | 19120kb | C++14 | 5.8kb | 2024-09-26 20:41:33 | 2024-09-26 20:41:33 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using lli = long long;
const int maxN = 2e5 + 5;
const int maxB = 1;
const int maxB1 = 448;
vector<int> type1,type0,adj[maxN];
int type[maxN],deg[maxN],n,m,val[maxN],l[maxB1],r[maxB1],id[maxN];
int sum0[maxB1],val0[maxN],id0[maxN];
int val1[maxN],id1[maxN],id11[maxN];
int id01[maxB1][maxN],sum01[2][maxB1][maxB1],sumch[2][maxB1],val01[2][maxB1][maxN];
int del[maxN];
vector<int> type01[maxB1],vex1;
struct Tedge
{
int x,y;
}
e[maxN];
void presolve()
{
int cntb = 0;
for (int i = 0;i < maxN;++i)
{
if (i % maxB == 0) l[++cntb] = i;
if (i % maxB == (maxB - 1)) r[cntb] = i;
id[i] = cntb;
//cerr << id[i] << "\n";
}
}
void finddata()
{
for (int i = 1;i <= n;++i)
{
type[i] = deg[i] > maxB;
if (type[i])
{
id11[i] = vex1.size();
vex1.push_back(i);
}
}
for (int i = 1;i <= m;++i)
{
if (type[e[i].x] && type[e[i].y])
{
id1[i] = type1.size();
type1.push_back(i);
}
if (type[e[i].x] == 0 && type[e[i].y] == 0)
{
id0[i] = type0.size();
type0.push_back(i);
}
}
for (int i = 1;i <= m;++i)
{
if (type[e[i].x] && !type[e[i].y])
{
id01[id11[e[i].x]][e[i].y] = type01[id11[e[i].x]].size();
val01[1][id11[e[i].x]][type01[id11[e[i].x]].size()] = 1;
sum01[1][id11[e[i].x]][id[type01[id11[e[i].x]].size()]]++;
sumch[1][id11[e[i].x]]++;
type01[id11[e[i].x]].push_back(i);
}
if (type[e[i].y] && !type[e[i].x])
{
id01[id11[e[i].y]][e[i].x] = type01[id11[e[i].y]].size();
val01[1][id11[e[i].y]][type01[id11[e[i].y]].size()] = 1;
sum01[1][id11[e[i].y]][id[type01[id11[e[i].y]].size()]]++;
sumch[1][id11[e[i].y]]++;
type01[id11[e[i].y]].push_back(i);
}
}
}
void input()
{
cin >> n >> m;
fill(deg + 1,deg + n + 1,0);
fill(val + 1,val + n + 1,0);
fill(id0 + 1,id0 + n + 1,0);
for (int i = 1;i <= m;++i)
{
cin >> e[i].x >> e[i].y;
deg[e[i].x]++;deg[e[i].y]++;
adj[e[i].x].push_back(i);
adj[e[i].y].push_back(i);
}
presolve();
finddata();
}
void query()
{
for (int i : type1)
if (val1[id1[i]] == 1 && del[i] == 0)
{
del[i] = 1;
cout << i << "\n";
return;
}
int cntb = (type0.size() + maxB - 1) / maxB;
//cout << cntb << "A\n";
for (int idb = 1;idb <= cntb;++idb)
{
if (sum0[idb] > 0)
{
for (int i = l[idb];i <= min(r[idb],int(type0.size()));++i)
if (val0[i] == 1)
{
del[type0[i]] = 1;
val0[i] = 0;
sum0[idb]--;
cout << type0[i] << "\n";
return;
}
}
}
for (int u : vex1)
{
if (sumch[val[u]][id11[u]] > 0)
{
int cntb = (type01[id11[u]].size() + maxB - 1) / maxB;
for (int idb = 1;idb <= cntb;++idb)
{
if (sum01[val[u]][id11[u]][idb] > 0)
{
for (int i = l[idb];i <= min(r[idb],int(type01[id11[u]].size()));++i)
if (val01[val[u]][id11[u]][i] == 1)
{
del[type01[id11[u]][i]] = 1;
val01[0][id11[u]][i] = 0;
val01[1][id11[u]][i] = 0;
cout << type01[id11[u]][i] << "\n";
return;
}
}
}
}
}
cout << 0 << "\n";
}
void update(int u)
{
//cerr << u << "a\n";
val[u] ^= 1;
if (type[u])
{
for (int i : type1)
if (e[i].x == u || e[i].y == u) val1[id1[i]] ^= 1;
}
else
{
for (int i : adj[u])
{
if (del[i] == 1) continue;
int v = e[i].x + e[i].y - u;
if (!type[v])
{
val0[id0[i]] = (val0[id0[i]] ^ 1);
if (val0[id0[i]] == 1) sum0[id[id0[i]]]++;
else sum0[id[id0[i]]]--;
}
else
{
val01[1][id11[e[i].x]][id01[id11[e[i].x]][u]] ^= 1;
val01[0][id11[e[i].x]][id01[id11[e[i].x]][u]] ^= 1;
if (val01[1][id11[e[i].x]][id01[id11[e[i].x]][u]] == 1)
{
sum01[1][id11[e[i].x]][id[id01[id11[e[i].x]][u]]]++;
sumch[1][id11[e[i].x]]++;
}
else
{
sum01[1][id11[e[i].x]][id[id01[id11[e[i].x]][u]]]--;
sumch[1][id11[e[i].x]]--;
}
if (val01[0][id11[e[i].x]][id01[id11[e[i].x]][u]] == 1)
{
sum01[0][id11[e[i].x]][id[id01[id11[e[i].x]][u]]]++;
sumch[0][id11[e[i].x]]++;
}
else
{
sum01[0][id11[e[i].x]][id[id01[id11[e[i].x]][u]]]--;
sumch[0][id11[e[i].x]]--;
}
}
}
}
}
void solve()
{
int q;
cin >> q;
while (q--)
{
char c;
int u;
cin >> c;
if (c == '+' || c == '-') {cin >> u;update(u);}
else query();
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
// freopen("C.inp","r",stdin);
// freopen("C.out","w",stdout);
input();
solve();
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 19120kb
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: 0ms
memory: 13028kb
input:
0 0 0
output:
result:
ok q=0
Test #3:
score: 0
Accepted
time: 3ms
memory: 13468kb
input:
0 0 1 ?
output:
0
result:
ok q=1
Test #4:
score: -100
Runtime Error
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...