QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#673291#9424. Stop the Castle 2ucup-team4479WA 80ms14684kbC++235.4kb2024-10-24 21:36:152024-10-24 21:36:16

Judging History

This is the latest submission verdict.

  • [2024-10-24 21:36:16]
  • Judged
  • Verdict: WA
  • Time: 80ms
  • Memory: 14684kb
  • [2024-10-24 21:36:15]
  • 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;
        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;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 9916kb

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: -100
Wrong Answer
time: 80ms
memory: 14684kb

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:

wrong answer Participant states the answer to be 3 but is actually 5 (test case 52)