QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#591966 | #7181. Graph Cuts | sitablechair | RE | 1ms | 7712kb | C++23 | 4.0kb | 2024-09-26 19:32:48 | 2024-09-26 19:32:49 |
Judging History
answer
#include <bits/stdc++.h>
#define ll long long
#define ldb long double
#define endl '\n'
#define For(i,l,r) for(int i=l;i<=r;i++)
#define ForD(i,r,l) for(int i=r;i>=l;i--)
#define REP(i,l,r) For(i,l,r-1)
#define PER(i,r,l) ForD(i,r-1,l)
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
#define All(x,n) x+1,x+1+n
#define Alll(x,n) x,x+n
#define sz(x) (signed)x.size()
#define unq(x) x.resize(unique(all(x))-x.begin())
#define mpa make_pair
#ifdef NCGM
#include"debug.h"
#else
#define debug(...) "fr";
#endif
using namespace std;
const int SQRT=2,N=1e5+3;
int n,m,q,sm[2][400][400],sum[2][400];
int small[400],smSum=0;
int id[N],id1[N],deg[N],cnt=0;
int idx[400][400];
pair<int,int> ed[N];
bool del[N],state[N];
vector<int> g[N];
int find_edge(bool st,int v) {
if (sum[st][v]==0) return -1;
For(i,0,(m-1)/SQRT)
if (sm[st][v][i]>0)
For(j,i*SQRT+1,min((i+1)*SQRT,m))
if (!del[j] && state[ed[j].ff]!=state[ed[j].ss]) return j;
// assert(false);
return -1;
}
int find_edge_small() {
if (smSum==0) return -1;
For(i,0,(m-1)/SQRT)
if (small[i]>0)
For(j,i*SQRT+1,min((i+1)*SQRT,m))
if (!del[j] && state[ed[j].ff]!=state[ed[j].ss]) return j;
// assert(false);
return -1;
}
void dele(int rem) {
del[rem]=1;
if (deg[ed[rem].ff]>SQRT) {
sm[!state[ed[rem].ss]][id[ed[rem].ff]][(rem-1)/SQRT]--;
sum[!state[ed[rem].ss]][id[ed[rem].ff]]--;
}
if (deg[ed[rem].ss]>SQRT) {
sm[!state[ed[rem].ff]][id[ed[rem].ss]][(rem-1)/SQRT]--;
sum[!state[ed[rem].ff]][id[ed[rem].ss]]--;
} else if (deg[ed[rem].ff]<=SQRT) {
smSum--;
small[(rem-1)/SQRT]--;
}
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m;
For(i,1,m) {
cin >> ed[i].ff >> ed[i].ss;
deg[ed[i].ff]++;
deg[ed[i].ss]++;
g[ed[i].ff].pb(i);
g[ed[i].ss].pb(i);
}
cin >> q;
For(i,1,n)
if (deg[i]>SQRT) {
id[i]=++cnt;
id1[cnt]=i;
For(j,1,m)
if (ed[j].ff==i ||ed[j].ss==i) sm[1][cnt][(j-1)/SQRT]++,sum[1][cnt]++;
}
For(i,1,m)
if (deg[ed[i].ff]>SQRT && deg[ed[i].ss]>SQRT)
idx[id[ed[i].ff]][id[ed[i].ss]]=idx[id[ed[i].ss]][id[ed[i].ff]]=i;
For(qr,1,q) {
char c;
int u;
cin >> c;
if (c!='?') {
cin >> u;
bool ki=(c=='+');
state[u]=ki;
if (deg[u]>SQRT) {
For(i,1,cnt) {
if (i==id[u] || idx[id[u]][i]==0) continue;
if (del[idx[id[u]][i]]) continue;
sum[!state[u]][i]++;
sum[state[u]][i]--;
sm[!state[u]][i][(idx[id[u]][i]-1)/SQRT]++;
sm[state[u]][i][(idx[id[u]][i]-1)/SQRT]--;
}
} else {
for(auto ski: g[u]) {
if (del[ski]) continue;
int v=(ed[ski].ff==u?ed[ski].ss:ed[ski].ff);
if (deg[v]>SQRT) {
sum[!state[u]][id[v]]++;
sum[state[u]][id[v]]--;
sm[!state[u]][id[v]][(ski-1)/SQRT]++;
sm[state[u]][id[v]][(ski-1)/SQRT]--;
} else {
int emra=(state[u]==state[v]?-1:1);
smSum+=emra;
small[(ski-1)/SQRT]+=emra;
}
}
}
} else {
int rem=find_edge_small();
if (rem==-1) {
For(i,1,cnt) {
rem=find_edge(state[id1[i]],i);
if (rem!=-1) break;
}
}
if(rem!=-1) {
cout << rem << endl;
dele(rem);
} else cout << 0 << endl;
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 7712kb
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: 1ms
memory: 3696kb
input:
0 0 0
output:
result:
ok q=0
Test #3:
score: 0
Accepted
time: 1ms
memory: 3692kb
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...