QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#628374 | #9424. Stop the Castle 2 | Idtwtei | TL | 74ms | 23824kb | C++14 | 3.2kb | 2024-10-10 19:54:27 | 2024-10-10 19:54:27 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define ar(x) array<int,x>
#define pb push_back
#define U(x) ((int)x.size())
const int N=1e5+100,INF=1e9;
#define gc getchar()
#define rd read()
inline int read(){
int x=0,f=0; char c=gc;
for(;c<'0'||c>'9';c=gc) f|=(c=='-');
for(;c>='0'&&c<='9';c=gc) x=(x<<1)+(x<<3)+(c^48);
return f?-x:x;
}
int n,m,k,s,s1,t,ans=0,to[N],ls[N],l_c=0; ar(2) a[N],b[N];
inline int get(int x){ return lower_bound(ls+1,ls+l_c+1,x)-ls; }
set<ar(2)> px[N],py[N]; vector<int> vc;
inline int findx(int x,int y){
auto it=px[x].upper_bound({y,0});
if(it==px[x].end()||it==px[x].begin()||(*it)[1]==-1||(*prev(it))[1]==-1) return 0;
return (*prev(it))[1];
}
inline int findy(int x,int y){
auto it=py[y].upper_bound({x,0});
if(it==py[y].end()||it==py[y].begin()||(*it)[1]==-1||(*prev(it))[1]==-1) return 0;
return (*prev(it))[1];
}
int head[N*2],cur[N*2],ne[N*6],v[N*6],fl[N*6],tot=1;
void add(int x,int y,int f){
ne[++tot]=head[x],head[x]=tot,v[tot]=y,fl[tot]=f;
ne[++tot]=head[y],head[y]=tot,v[tot]=x,fl[tot]=0;
}
queue<int> q; int dis[N];
int bfs(){
for(int i=1;i<=t;++i) cur[i]=head[i],dis[i]=0;
q.push(s),dis[s]=1;
while(!q.empty()){
int u=q.front(); q.pop();
for(int i=head[u];i;i=ne[i]) if(!dis[v[i]]&&fl[i]) dis[v[i]]=dis[u]+1,q.push(v[i]);
}
return dis[t]!=0;
}
int dinic(int u,int df){
if(u==t) return df;
int res=df;
for(int i=cur[u];res&&i;i=ne[i]){
cur[u]=i;
if(dis[v[i]]==dis[u]+1&&fl[i]){
int k=dinic(v[i],min(res,fl[i]));
if(!k) dis[v[i]]=0;
else res-=k,fl[i]-=k,fl[i^1]+=k;
}
}
return df-res;
}
void solve(){
n=rd,m=rd,k=m-rd,ans=0,s=2*n+1,s1=2*n+2,t=2*n+3,add(s,s1,k);
for(int i=1,x,y;i<=n;++i) a[i]={ls[++l_c]=rd,ls[++l_c]=rd};
for(int i=1,x,y;i<=m;++i) b[i]={ls[++l_c]=rd,ls[++l_c]=rd};
sort(ls+1,ls+l_c+1),l_c=unique(ls+1,ls+l_c+1)-ls-1;
for(int i=1;i<=n;++i) a[i][0]=get(a[i][0]),a[i][1]=get(a[i][1]);
for(int i=1;i<=m;++i) b[i][0]=get(b[i][0]),b[i][1]=get(b[i][1]);
for(int i=1;i<=n;++i) px[a[i][0]].insert({a[i][1],i}),py[a[i][1]].insert({a[i][0],i});
for(int i=1;i<=l_c;++i) ans+=max(0,U(px[i])-1),ans+=max(0,U(py[i])-1);
for(int i=1,l,r;i<=m;++i){
auto [x,y]=b[i]; l=findx(x,y),r=findy(x,y);
if(l&&r) to[i]=tot+1,add(l,r+n,1);
}
for(int i=1;i<=n;++i) add(s1,i,1);
for(int i=1;i<=n;++i) add(i+n,t,1);
int maxflow=0,dflow=0;
while(bfs()) while(dflow=dinic(s,INF)) maxflow+=dflow;
for(int i=1;k&&i<=m;++i){
if(!to[i]||fl[to[i]]) to[i]=0;
else ans-=2,--k,vc.pb(i),px[b[i][0]].insert({b[i][1],-1}),py[b[i][1]].insert({b[i][0],-1});
}
for(int i=1;k&&i<=m;++i){
if(to[i]||(!findx(b[i][0],b[i][1])&&!findy(b[i][0],b[i][1]))) continue;
--ans,--k,vc.pb(i),to[i]=1,px[b[i][0]].insert({b[i][1],-1}),py[b[i][1]].insert({b[i][0],-1});
}
for(int i=1;k&&i<=m;++i) if(!to[i]) vc.pb(i),to[i]=1,--k;
printf("%d\n", ans);
for(int i=1;i<=m;++i) to[i]=0;
for(int v:vc) to[v]=1; vc={};
for(int i=1;i<=m;++i) if(!to[i]) printf("%d ", i); puts("");
for(int i=1;i<=m;++i) to[i]=0;
for(int i=1;i<=t;++i) head[i]=0; tot=1;
for(int i=1;i<=l_c;++i) px[i].clear(),py[i].clear(); l_c=0;
}
int main(){
int T=rd;
while(T--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 20420kb
input:
3 8 6 4 1 3 2 1 2 6 4 1 4 7 6 1 6 3 6 6 2 3 3 1 4 3 4 6 5 2 6 4 3 2 1 10 12 10 10 10 11 1 4 1 5 1 3 2 1 1 2 1 2 2 2 3
output:
4 2 3 5 6 2 2 0 2 3
result:
ok ok 3 cases (3 test cases)
Test #2:
score: 0
Accepted
time: 74ms
memory: 23824kb
input:
1224 11 17 14 7 3 4 2 8 13 3 15 3 4 5 11 10 2 3 3 8 6 7 11 2 3 10 4 1 3 12 1 2 5 11 9 11 6 11 10 8 15 1 5 9 14 4 11 1 6 10 7 7 6 11 4 8 4 1 11 18 3 2 14 8 2 14 13 13 9 12 14 12 5 6 8 1 10 5 8 6 8 9 6 6 7 5 12 11 6 11 13 5 1 10 7 6 14 5 6 15 2 4 11 1 1 6 4 14 14 13 9 9 3 10 12 7 5 8 13 9 14 1 9 8 4 9...
output:
7 3 4 5 6 7 8 9 10 11 12 13 15 16 17 15 2 3 0 3 4 5 6 0 2 3 4 5 6 7 8 9 11 1 3 8 1 2 3 0 1 2 3 4 5 6 7 8 9 10 11 12 1 5 6 7 9 10 11 12 8 17 18 19 1 1 2 3 4 5 6 7 8 7 6 8 10 13 14 15 1 10 11 12 13 14 15 16 17 18 19 20 0 1 1 2 3 0 5 6 7 7 8 12 13 14 15 2 10 11 12 13 14 4 3 4 5 6 7 8 ...
result:
ok ok 1224 cases (1224 test cases)
Test #3:
score: -100
Time Limit Exceeded
input:
1 86289 95092 40401 911 152 1 270 135 731 347 451 283 224 338 348 166 346 12 385 590 763 939 176 232 405 122 946 397 576 795 823 546 392 33 718 444 598 954 852 185 662 732 539 172 681 386 148 76 495 163 323 711 201 278 363 531 275 66 122 823 983 234 792 102 188 985 423 804 712 419 636 318 331 693 68...