QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#625103#9424. Stop the Castle 2ucup-team4153#WA 75ms38592kbC++206.2kb2024-10-09 17:28:212024-10-09 17:28:22

Judging History

This is the latest submission verdict.

  • [2024-10-09 17:28:22]
  • Judged
  • Verdict: WA
  • Time: 75ms
  • Memory: 38592kb
  • [2024-10-09 17:28:21]
  • Submitted

answer

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5,INF=0x3f3f3f3f;
int t,n,m,k;
struct node{int op,bh,r,c;}cas[N],obs[N];
bool cmp(const node &x,const node &y){if(x.r==y.r)return x.c<y.c;return x.r<y.r;}
bool cmp2(const node &x,const node &y){if(x.c==y.c)return x.r<y.r;return x.c<y.c;}
vector<int>E[N<<1];
bool chose[N],vis[N*2];
struct Edge{
    int from,to,cap,flow,op;
    Edge(int a,int b,int c,int d,int op):from(a),to(b),cap(c),flow(d),op(op){}
};
const int maxn=1e6+5;
struct Dinic{
    int n,m,s,t;
    vector<Edge>edges;
    vector<int>G[maxn];
    bool vis[maxn];
    int d[maxn],cur[maxn];
    void init(int _n){
        this->n=_n;
        for(int i=0;i<=n;i++)G[i].clear();
        edges.clear();
    }
    void AddEdge(int from,int to,int cap,int op){
        edges.push_back(Edge(from,to,cap,0,op));
        edges.push_back(Edge(to,from,0,0,op)); //有向图:Edge(to,from,0,0)  无向图:Edge(to,from,cap,0)
        m=edges.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }
    bool BFS(){
        for(int i=0;i<=n;i++)vis[i]=false;
        queue<int>Q;
        Q.push(s);
        d[s]=0;
        vis[s]=true;
        while(!Q.empty()){
            int x=Q.front();
            Q.pop();
            for(int i=0;i<G[x].size();i++){
                Edge &e=edges[G[x][i]];
                if(!vis[e.to] && e.cap > e.flow){
                    vis[e.to]=true;
                    d[e.to]=d[x]+1;
                    Q.push(e.to);
                }
            }
        }
        return vis[t];
    }
    int DFS(int x,int a){
        if(x==t || a==0)return a;
        int flow=0,f;
        for(int &i=cur[x];i<G[x].size();i++){
            Edge &e=edges[G[x][i]];
            if(d[x]+1==d[e.to] && (f=DFS(e.to,min(a,e.cap-e.flow)))>0){
                e.flow+=f;
                edges[G[x][i]^1].flow-=f;
                flow+=f;
                a-=f;
                if(a==0)break;
            }
        }
        return flow;
    }
    int MaxFlow(int _s,int _t){
        this->s=_s;this->t=_t;
        int flow=0;
        while(BFS()){
            for(int i=0;i<=n;i++)cur[i]=0;
            flow+=DFS(s,INF);
        }
        return flow;
    }
}MF;
signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cin>>t;
    while(t--)
    {
        cin>>n>>m>>k;
        //if(n>1e4)exit(1);
        for(int i=1;i<=n;i++){
            cas[i].op=0;
            cin>>cas[i].r>>cas[i].c;
        }
        for(int i=1;i<=m;i++){
            E[i].clear();
            obs[i].op=1;
            cin>>obs[i].r>>obs[i].c;
            obs[i].bh=i;
            chose[i]=false;
        }
        int cnt1=0,cnt2=0;
        {
            sort(cas+1,cas+n+1,cmp);
            vector<node>vec;
            for(int i=1;i<=n;i++){
                if(i==n || cas[i].r!=cas[i+1].r){
                    cas[i].bh=-1;
                }else{
                    cas[i].bh=++cnt1;
                    vec.push_back(cas[i]);
                }
            }
            for(int i=1;i<=m;i++){
                vec.push_back(obs[i]);
            }
            sort(vec.begin(),vec.end(),cmp);
            int pre=-1;
            for(int i=0;i<vec.size();i++){
                if(vec[i].op==1 && pre!=-1){
                    E[vec[i].bh].push_back(pre);
                }
                if(i==vec.size()-1 || vec[i].r!=vec[i+1].r){
                    pre=-1;
                }else{
                    if(vec[i].op==0){
                        pre=vec[i].bh;
                    }
                }
            }
        }
        
        {
            sort(cas+1,cas+n+1,cmp2);
            vector<node>vec;
            for(int i=1;i<=n;i++){
                if(i==n || cas[i].c!=cas[i+1].c){
                    cas[i].bh=-1;
                }else{
                    cas[i].bh=(++cnt2)+cnt1;
                    vec.push_back(cas[i]);
                }
            }
            for(int i=1;i<=m;i++){
                vec.push_back(obs[i]);
            }
            sort(vec.begin(),vec.end(),cmp2);
            int pre=-1;
            for(int i=0;i<vec.size();i++){
                if(vec[i].op==1 && pre!=-1){
                    E[vec[i].bh].push_back(pre);
                }
                if(i==vec.size()-1 || vec[i].c!=vec[i+1].c){
                    pre=-1;
                }else{
                    if(vec[i].op==0){
                        pre=vec[i].bh;
                    }
                }
            }
        }



        for(int i=0;i<=cnt1+cnt2;i++)vis[i]=false;
        k=m-k;
        MF.init(cnt1+cnt2+10);
        for(int i=1;i<=m;i++){
            if(E[i].size()==2){
                MF.AddEdge(E[i][0],E[i][1],1,i);
            }
        }
        int flow_s=cnt1+cnt2+1,flow_t=cnt1+cnt2+2;
        for(int i=1;i<=cnt1;i++){
            MF.AddEdge(flow_s,i,1,0);
        }
        for(int i=cnt1+1;i<=cnt1+cnt2;i++){
            MF.AddEdge(i,flow_t,1,0);
        }
        int flow=MF.MaxFlow(flow_s,flow_t);
        for(int x=1;x<=cnt1;x++){
            for(int i=0;i<MF.G[x].size();i++){
                Edge &e=MF.edges[MF.G[x][i]];
                if(e.flow>0 && e.op>0 && !chose[e.op] && k>0){
                    k--;
                    chose[e.op]=true;
                    vis[e.from]=vis[e.to]=true;
                }
            }
        }

        for(int i=1;i<=m;i++){
            if(chose[i])continue;
            int val=E[i].size();
            for(auto x:E[i]){
                if(vis[x])val--;
            }
            if(val && k>0){
                if(val==2)exit(1);
                k--;
                chose[i]=true;
                for(auto x:E[i]){
                    vis[x]=true;
                }
            }
        }

        for(int i=1;i<=m;i++){
            if(chose[i])continue;
            if(k>0){
                k--;
                chose[i]=true;
            }
        }
        if(k)while(1);
        int res=0;
        for(int i=1;i<=cnt1+cnt2;i++)if(!vis[i])res++;
        cout<<res<<'\n';
        for(int i=1;i<=m;i++){
            if(!chose[i])cout<<i<<' ';
        }
        cout<<'\n';
    }
    return 0;
}

详细

Test #1:

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

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: 75ms
memory: 38592kb

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:

6
2 3 4 5 6 7 9 10 11 12 13 15 16 17 
14
2 3 
0
3 4 5 6 
0
2 3 4 5 6 7 8 9 
10
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 
5
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 
6
10 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 6 but is actually 7 (test case 1)