QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#673333#9424. Stop the Castle 2ucup-team4479RE 145ms22348kbC++235.5kb2024-10-24 21:47:052024-10-24 21:47:05

Judging History

This is the latest submission verdict.

  • [2024-10-24 21:47:05]
  • Judged
  • Verdict: RE
  • Time: 145ms
  • Memory: 22348kb
  • [2024-10-24 21:47:05]
  • Submitted

answer

#include<bits/stdc++.h>
using namespace std;
constexpr int N=100005;
struct Hopcroft_Karp
{
    int n,m;
    vector<int>g[N];
    int pa[N],pb[N];
    Hopcroft_Karp(){}
    Hopcroft_Karp(int _n,int _m):n(_n),m(_m){}
    void init(int _n,int _m)
    {
        n=_n,m=_m;
        for(int i=1;i<=n;i++)
            g[i].clear();
        return;
    }
    void add_edge(int u,int v)
    {
        g[u].push_back(v);
        return;
    }
    int dis[N];
    bool bfs()
    {
        queue<int>q;
        for(int u=1;u<=n;u++)
        {
            if(!pa[u])
            {
                dis[u]=0;
                q.push(u);
            }
            else dis[u]=-1;
        }
        bool found=false;
        while(!q.empty())
        {
            int u=q.front();
            q.pop();
            for(int v:g[u])
            {
                if(pb[v]&&dis[pb[v]]==-1)
                {
                    dis[pb[v]]=dis[u]+1;
                    q.push(pb[v]);
                }
                else if(!pb[v]) found=true;
            }
        }
        return found;
    }
    bool dfs(int u)
    {
        for(int v:g[u])
            if(!pb[v]||(dis[pb[v]]==dis[u]+1&&dfs(pb[v])))
            {
                pa[u]=v,pb[v]=u;
                return true;
            }
        dis[u]=-1;
        return false;
    }
    int max_matching()
    {
        fill(pa+1,pa+n+1,0);
        fill(pb+1,pb+m+1,0);
        int res=0;
        while(bfs())
        {
            for(int u=1;u<=n;u++)
                if(!pa[u]&&dfs(u)) res++;
        }
        return res;
    }
}hk;
int n,m,k;
int r[N],c[N];
int rr[N],cc[N];
int bx[N*2],totr;
int by[N*2],totc;
int nl,nr;
vector<pair<int,int>>posr[N],posc[N];
bool visr[N],visc[N];
bool picked[N];
void solve()
{
    cin>>n>>m>>k;
    for(int i=1;i<=n;i++)
        cin>>r[i]>>c[i];
    for(int i=1;i<=m;i++)
        cin>>rr[i]>>cc[i];
    totr=totc=0;
    for(int i=1;i<=n;i++)
        bx[++totr]=r[i],by[++totc]=c[i];
    for(int i=1;i<=m;i++)
        bx[++totr]=rr[i],by[++totc]=cc[i];
    sort(bx+1,bx+totr+1);
    sort(by+1,by+totc+1);
    totr=unique(bx+1,bx+totr+1)-bx-1;
    totc=unique(by+1,by+totc+1)-by-1;
    for(int i=1;i<=n;i++)
        r[i]=lower_bound(bx+1,bx+totr+1,r[i])-bx,c[i]=lower_bound(by+1,by+totc+1,c[i])-by;
    for(int i=1;i<=m;i++)
        rr[i]=lower_bound(bx+1,bx+totr+1,rr[i])-bx,cc[i]=lower_bound(by+1,by+totc+1,cc[i])-by;
    for(int i=1;i<=totr;i++)
        posr[i].clear();
    for(int i=1;i<=totc;i++)
        posc[i].clear();
    for(int i=1;i<=n;i++)
        posr[r[i]].emplace_back(c[i],0),posc[c[i]].emplace_back(r[i],0);
    nl=nr=0;
    int ans=0;
    for(int i=1;i<=totr;i++)
    {
        sort(posr[i].begin(),posr[i].end());
        for(int j=1;j<(int)posr[i].size();j++)
            posr[i][j].second=++nl,ans++;
    }
    for(int i=1;i<=totc;i++)
    {
        sort(posc[i].begin(),posc[i].end());
        for(int j=1;j<(int)posc[i].size();j++)
            posc[i][j].second=++nr,ans++;
    }
    hk.init(nl,nr);
    map<pair<int,int>,int>edge;
//    cerr<<"nl"<<nl<<" "<<nr<<'\n';
    for(int i=1;i<=m;i++)
    {
        int atr=lower_bound(posr[rr[i]].begin(),posr[rr[i]].end(),make_pair(cc[i],0))-posr[rr[i]].begin();
        int atc=lower_bound(posc[cc[i]].begin(),posc[cc[i]].end(),make_pair(rr[i],0))-posc[cc[i]].begin();
        if(0<atr&&atr<(int)posr[rr[i]].size()&&0<atc&&atc<(int)posc[cc[i]].size())
        {
            int pr=posr[rr[i]][atr].second,pc=posc[cc[i]][atc].second;
            hk.add_edge(pr,pc);
//            cerr<<"add"<<i<<" "<<posr[rr[i]][atr].first<<" "<<posc[cc[i]][atc].first<<"\n";
//            cerr<<"pos"<<pr<<" "<<pc<<"\n";
            edge[{pr,pc}]=i;
        }
    }
    int cnt=min(hk.max_matching(),m-k);
    int ret=m-k-cnt;
    fill(visr+1,visr+nl+1,false);
    fill(visc+1,visc+nr+1,false);
    fill(picked+1,picked+m+1,false);
    for(int i=1;i<=nl&&cnt>0;i++)
        if(hk.pa[i]) picked[edge[{i,hk.pa[i]}]]=true,visr[i]=visc[hk.pa[i]]=true,cnt--,ans-=2;
//    cerr<<"ans"<<ans<<"\n";
    if(ret>0)
    {
        for(int i=1;i<=m&&ret>0;i++)
            if(!picked[i])
            {
                int atr=lower_bound(posr[rr[i]].begin(),posr[rr[i]].end(),make_pair(cc[i],0))-posr[rr[i]].begin();
                int atc=lower_bound(posc[cc[i]].begin(),posc[cc[i]].end(),make_pair(rr[i],0))-posc[cc[i]].begin();
                if(0<atr&&atr<(int)posr[rr[i]].size())
                {
                    int pr=posr[rr[i]][atr].second;
                    if(!visr[pr])
                    {
                        picked[i]=true;
                        visr[pr]=true;
                        ret--,ans--;
                    }
                }
                if(0<atc&&atc<(int)posc[cc[i]].size())
                {
                    int pc=posc[cc[i]][atc].second;
                    if(!visc[pc])
                    {
                        picked[i]=true;
                        visc[pc]=true;
                        ret--,ans--;
                    }
                }
            }
        for(int i=1;i<=m&&ret>0;i++)
            if(!picked[i]) ret--,picked[i]=true;
    }
    cout<<ans<<"\n";
    for(int i=1;i<=m;i++)
        if(!picked[i]) cout<<i<<" ";
    cout<<"\n";
    return;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr),cout.tie(nullptr);
    int T;
    cin>>T;
    while(T--)
        solve();
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 2ms
memory: 9824kb

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: 66ms
memory: 12324kb

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: 0
Accepted
time: 118ms
memory: 20132kb

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:

81531
5869 5872 5874 5876 5878 5884 5885 5893 5894 5895 5898 5899 5907 5911 5914 5917 5919 5920 5923 5924 5926 5927 5928 5930 5931 5933 5934 5940 5941 5943 5949 5956 5961 5963 5964 5968 5974 5975 5977 5979 5980 5981 5983 5984 5985 5987 5989 5992 5993 5997 6003 6006 6007 6008 6013 6017 6024 6028 6031...

result:

ok ok 1 cases (1 test case)

Test #4:

score: 0
Accepted
time: 113ms
memory: 20356kb

input:

1
99057 99722 73893
190482185 274379837
466851670 641324039
993028302 128875937
102891466 286559847
526771097 794238060
565736409 328262657
190329865 598878250
790626887 595298790
308031819 470646878
341575785 374318107
257299536 280924175
64420619 591124604
323023069 811512407
428956686 719615923
2...

output:

82045
1 2 6 9 10 11 13 15 16 18 19 20 21 22 24 25 28 29 30 33 34 35 36 37 39 43 45 49 50 51 52 54 55 59 60 61 62 65 66 67 69 70 71 79 81 82 83 87 90 91 93 94 95 96 99 100 101 102 104 105 107 109 110 111 112 113 114 118 120 128 129 131 133 136 137 138 142 143 147 148 149 151 152 153 154 155 156 159 1...

result:

ok ok 1 cases (1 test case)

Test #5:

score: 0
Accepted
time: 145ms
memory: 22348kb

input:

1
100000 99990 27662
913840909 999962982
690565691 31053
780601566 31053
54745498 31053
5383 859704869
538124857 999962982
5383 66851413
1444277 31053
119603839 999962982
999833258 543197820
999833258 349576387
999833258 759855830
999833258 124692224
266093388 999962982
5383 100041707
999833258 2843...

output:

100891
65667 65668 65669 65673 65676 65677 65679 65680 65681 65682 65683 65684 65685 65686 65687 65688 65689 65690 65691 65692 65693 65694 65695 65696 65697 65698 65699 65700 65701 65703 65706 65707 65709 65710 65711 65712 65713 65716 65717 65718 65720 65722 65723 65724 65725 65726 65728 65730 65731...

result:

ok ok 1 cases (1 test case)

Test #6:

score: 0
Accepted
time: 68ms
memory: 18364kb

input:

1
100000 49997 21428
9380 4333
9380 999999628
49202 4333
49202 999999628
50841 4333
50841 999999628
77418 4333
77418 999999628
95722 4333
95722 999999628
144002 4333
144002 999999628
234359 4333
234359 999999628
268942 4333
268942 999999628
288956 4333
288956 999999628
415094 4333
415094 999999628
4...

output:

100000
7099 7100 7102 7103 7104 7105 7106 7108 7110 7113 7114 7117 7119 7120 7122 7123 7126 7130 7131 7134 7135 7136 7140 7145 7149 7151 7154 7157 7158 7160 7162 7163 7167 7170 7172 7173 7174 7176 7178 7182 7183 7184 7188 7190 7197 7199 7201 7204 7205 7206 7208 7209 7211 7212 7213 7215 7216 7221 722...

result:

ok ok 1 cases (1 test case)

Test #7:

score: -100
Runtime Error

input:

1
100000 100000 76259
931427170 7
367311884 7
646435086 7
925372747 7
371054451 7
284185575 7
695090232 7
889183241 7
615617158 7
44230096 7
293281406 7
758261641 7
685549291 7
679471071 7
723138327 7
901136691 7
49281635 7
256352978 7
320188290 7
78730802 7
788131872 7
234735044 7
664906524 7
79430...

output:


result: