QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#601498#9424. Stop the Castle 2kevinshan#WA 194ms52464kbC++145.8kb2024-09-30 02:02:102024-09-30 02:02:10

Judging History

This is the latest submission verdict.

  • [2024-09-30 02:02:10]
  • Judged
  • Verdict: WA
  • Time: 194ms
  • Memory: 52464kb
  • [2024-09-30 02:02:10]
  • Submitted

answer

#include<bits/stdc++.h>
#define SZ(x) ((int)x.size())
#define FOR(i,a,b) for (int i=a;i<=b;++i)
#define FORD(i,a,b) for (int i=a;i>=b;--i)
using namespace std;

const int N =500010,M=500010;


int tmpx[500100],tmpy[500010],tmpxs,tmpys,Chengs,Cshus;
bool used[500010],solvedzuo[500100],solvedyou[500010];
int x[500010],y[500010],n,m,k,zuo[500010],you[500010],fxb[500010];
vector<int> heng[200010],shu[200010];
bool Solvablezuo[500010],Solvableyou[500010];
int Solvables;

map<pair<int,int>,int> Cheng;
map<pair<int,int>,int> Cshu;

namespace Flow{
    int tot=1,fi[N],a[M],c[M],ne[M];
    void Add(int x,int y,int z){
        a[++tot]=y;ne[tot]=fi[x];fi[x]=tot;c[tot]=z;
    }
    int Adde(int x,int y,int z){
        Add(x,y,z),Add(y,x,0);
        return tot;
    }
    int cur[N],de[N];
    queue<int> q;
    bool Bfs(int s,int t){
        //memset(de,0,sizeof(de));
        FOR(i,0,Chengs + Cshus+10) de[i]=0;
        de[s]=1;
        q.push(s);
        while (!q.empty()){
            int u=q.front();q.pop();
            for (int i=fi[u];i;i=ne[i])
                if (c[i] && !de[a[i]]) de[a[i]]=de[u]+1,q.push(a[i]);
        }
        return de[t];
    }
    int Dfs(int x,int flow,int t){
        if (x==t) return flow;
        int tmp,s=0;
        for (int &i=cur[x];i;i=ne[i])
            if (c[i] && de[x]+1 == de[a[i]] && (tmp=Dfs(a[i],min(flow,c[i]),t))){
                c[i]-=tmp;
                c[i^1]+=tmp;
                flow-=tmp;
                s+=tmp;
                if (!flow) return s;
            }
        return s;
    }
    int Solve(int s,int t){
        int maxflow=0;
        while (Bfs(s,t)) memcpy(cur,fi,sizeof fi),maxflow+=Dfs(s,1e9,t);
        return maxflow;
    }
};


void doit(){
    scanf("%d%d%d",&n,&m,&k);
    int tmpxs=0,tmpys=0;
    FOR(i,1,n){
        scanf("%d%d",&x[i],&y[i]);
        tmpx[++tmpxs]=x[i];
        tmpy[++tmpys]=y[i];
    }
    sort(tmpx+1,tmpx+tmpxs+1);
    sort(tmpy+1,tmpy+tmpys+1);
    tmpxs = unique(tmpx+1,tmpx+tmpxs+1)-tmpx-1;
    tmpys = unique(tmpy+1,tmpy+tmpys+1)-tmpy-1;
    FOR(i,1,n){
        x[i]=lower_bound(tmpx+1,tmpx+tmpxs+1,x[i])-tmpx;
        y[i]=lower_bound(tmpy+1,tmpy+tmpys+1,y[i])-tmpy;
        
    }
    FOR(i,1,tmpxs) heng[i].clear();
    FOR(i,1,tmpys) shu[i].clear();

    FOR(i,1,n){
        heng[x[i]].push_back(tmpy[y[i]]);
        shu[y[i]].push_back(tmpx[x[i]]);
    }

    FOR(i,1,tmpxs)
        sort(heng[i].begin(),heng[i].end());
    FOR(i,1,tmpys)
        sort(shu[i].begin(),shu[i].end());
    Cheng.clear();
    Cshu.clear();
    Chengs=0;
    Cshus=0;
    FOR(i,1,tmpxs)
        for (int j=1;j<heng[i].size();++j)
            Cheng[make_pair(i,j)]=++Chengs;

    FOR(i,1,tmpys)
        for (int j=1;j<shu[i].size();++j)
            Cshu[make_pair(i,j)]=++Cshus;

    FOR(i,1,m) zuo[i]=you[i]=0;
    FOR(i,1,m){
        int x,y;
        scanf("%d%d",&x,&y);
        int t=lower_bound(tmpx+1,tmpx+tmpxs+1,x)-tmpx;
        if (t<=tmpxs && tmpx[t]==x){
            
            int id = lower_bound(heng[t].begin(),heng[t].end(),y)-heng[t].begin();
            if (id<heng[t].size() && id>0){
                // if (Cheng.find(make_pair(t,id))==Cheng.end()){
                //     Cheng[make_pair(t,id)]=++Chengs;
                // }
                zuo[i]=Cheng[make_pair(t,id)];
            }

        }
        t=lower_bound(tmpy+1,tmpy+tmpys+1,y)-tmpy;
        if (t<=tmpys && tmpy[t]==y){
            
            int id = lower_bound(shu[t].begin(),shu[t].end(),x)-shu[t].begin();
            if (id<shu[t].size() && id>0){
                // if (Cshu.find(make_pair(t,id))==Cshu.end()){
                //     Cshu[make_pair(t,id)]=++Cshus;
                // }
                you[i]=Cshu[make_pair(t,id)];
            }

        }
    }

    FOR(i,1,Chengs)
        Flow::Adde(0,i,1);
    FOR(i,1,Cshus)
        Flow::Adde(i+Chengs,Chengs+Cshus+1,1);
    FOR(i,1,Chengs) Solvablezuo[i]=0;
    FOR(i,1,Cshus) Solvableyou[i]=0;
    FOR(i,1,m){
        if (zuo[i]!=0 && you[i]!=0)
            fxb[i]=Flow::Adde(zuo[i],you[i]+Chengs,1);
        else
            fxb[i]=-1;
        if (zuo[i]!=0)
            Solvablezuo[zuo[i]]=1;
        if (you[i]!=0)
            Solvableyou[you[i]]=1;
    }
    int mxflow = Flow::Solve(0,Chengs+Cshus+1);
    Solvables=0;
    FOR(i,1,Chengs) Solvables+=Solvablezuo[i];
    FOR(i,1,Cshus) Solvables+=Solvableyou[i];
    
    if (mxflow>m-k) printf("%d\n",Chengs+Cshus-(m-k)*2);
    else printf("%d\n",Chengs+Cshus-min(mxflow+m-k,Solvables));
    int tot_used=0;

    FOR(i,1,Chengs) solvedzuo[i]=0;
    FOR(i,1,Cshus) solvedyou[i]=0;
    FOR(i,1,m) used[i]=0;
    FOR(i,1,m){
        if (tot_used==m-k) break;
        if (fxb[i]!=-1){
            if (Flow::c[fxb[i]]==1){
                used[i]=1;
                tot_used++;
                solvedzuo[zuo[i]]=1;
                solvedyou[you[i]]=1;
            }
            else used[i]=0;
        }
    }
    FOR(i,1,m){
        if (tot_used >= m-k) break;
        if (used[i]!=1){
            if (zuo[i]!=0 && !solvedzuo[zuo[i]]){
                used[i]=1;
                solvedzuo[zuo[i]]=1;
                solvedyou[you[i]]=1;
                tot_used++;
            }
            else if (you[i]!=0 && !solvedyou[you[i]]){
                used[i]=1;
                solvedzuo[zuo[i]]=1;
                solvedyou[you[i]]=1;
                tot_used++;
            }
            
        }
    }

    if (tot_used<m-k){
        FOR(i,1,m) if (!used[i]){
            used[i]=1;
            ++tot_used;
            if (tot_used==m-k) break;
        }
    }

    FOR(i,1,m) if (!used[i]) printf("%d ",i);
    puts("");

    Flow::tot=1;
    FOR(i,0,Chengs+Cshus+1) Flow::fi[i]=0;
}
int main(){

    int T;
    scanf("%d",&T);
    while (T--) doit();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 38872kb

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: 194ms
memory: 38232kb

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
Wrong Answer
time: 160ms
memory: 52464kb

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...

output:

115887
1362 1940 2078 2166 2488 2874 2934 3128 3147 3291 3398 3700 3753 3756 3770 3818 3936 3989 4089 4157 4214 4382 4466 4660 4762 4897 4939 4973 4976 5081 5083 5091 5178 5190 5295 5317 5330 5345 5472 5475 5637 5642 5691 5696 5760 5831 5851 5885 6125 6133 6177 6227 6228 6289 6314 6317 6324 6343 639...

result:

wrong answer Participant states the answer to be 115887 but is actually 92558 (test case 1)