QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#520796#8759. 小班课lefyRE 1ms4164kbC++142.6kb2024-08-15 15:51:052024-08-15 15:51:07

Judging History

你现在查看的是最新测评结果

  • [2024-08-15 15:51:07]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:4164kb
  • [2024-08-15 15:51:05]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
const int N=510,M=1e6+10,inf=1e9;
int a[N];
struct node{
    int to,nxt,val,w;
}e[M];
int head[N<<1],cnt=1,s,t;
void Add(int u,int v,int val,int w){
    e[++cnt]={v,head[u],val,w};
    head[u]=cnt;
}
void add(int u,int v,int val,int w){
    Add(u,v,val,w),Add(v,u,0,-w);
}
int vis[N<<1],d[N<<1];
int spfa(){
    queue<int>q;q.push(s);
    for(int i=s;i<=t;i++)d[i]=1e9;
    d[s]=0;
    int flag=0;
    while(!q.empty()){
        int x=q.front();q.pop();vis[x]=0;
        for(int i=head[x];i;i=e[i].nxt){
            int v=e[i].to;
            if(e[i].val&&d[x]+e[i].w<d[v]){
                d[v]=d[x]+e[i].w;
                if(!vis[v])vis[v]=1,q.push(v);
                if(v==t)flag=1;
            }
        }
    }
    return flag;
}
int dinic(int x,int flow){
    if(x==t)return flow;
    int k,r=flow;
    vis[x]=1;
    for(int i=head[x];i;i=e[i].nxt){
        int v=e[i].to;
        if(vis[v]||d[v]!=d[x]+e[i].w||!e[i].val)continue;
        k=dinic(v,min(e[i].val,r));
        if(!k)d[v]=inf;
        e[i].val-=k;e[i^1].val+=k;
        r-=k;if(!r)break;
    }
    vis[x]=0;
    return flow-r;
}
vector<int>cho[N];
vector<int>b[N<<1];
int du[N];
void solve(){
    int n,m;cnt=1;
    scanf("%d%d",&n,&m);
    t=n+m+1;
    for(int i=1;i<=m;i++)scanf("%d",&a[i]),add(i+n,t,a[i],0);
    for(int i=1;i<=n;i++){
        int X;scanf("%d",&X);cho[i].clear();cho[i].push_back(0);
        for(int j=1;j<=X;j++){
            int x;scanf("%d",&x);
            add(i,x+n,1,j);
            cho[i].push_back(x);
        }
        add(s,i,1,0);
    }
    int ans=0;
    while(spfa())ans+=dinic(s,inf);
    printf("%d\n",ans);
    for(int i=1;i<=n;i++){
        int id=0;
        for(int j=head[i];j;j=e[j].nxt){
            // cout<<e[j].w<<" "<<e[j].val<<"\n";
            if(e[j].w&&!e[j].val){
                id=e[j].w;break;
            }
        }
        // cout<<id<<"\n";
        if(id){
            // cout<<id<<"\n";
            b[i].push_back(cho[i][id]+n);du[cho[i][id]+n]++;
            for(int j=1;j<id;j++)b[cho[i][j]+n].push_back(i),du[i]++;
        }else vis[i]=1;
    }
    queue<int>q;
    for(int i=1;i<=n+m;i++)if(!du[i]&&!vis[i])q.push(i);
    while(!q.empty()){
        int x=q.front();q.pop();if(x<=n)printf("%d ",x);
        for(int v:b[x]){
            du[v]--;
            if(!du[v])q.push(v);
        }
    }
    for(int i=1;i<=n;i++)if(vis[i])printf("%d ",i);
    printf("\n");
    for(int i=s;i<=t;i++)head[i]=0,b[i].clear(),vis[i]=0;cnt=1;
}
int main() {
    int t;
    scanf("%d",&t);
    while(t--)solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 4164kb

input:

3
5 5
1 1 1 1 1
4 1 3 2 4
1 5
4 3 4 2 1
2 3 5
1 1
5 3
1 2 2
2 1 2
2 1 2
2 1 3
2 1 3
2 1 3
5 5
1 1 1 1 1
2 1 2
2 5 4
2 3 2
2 4 3
2 5 1

output:

5
2 4 5 3 1 
5
5 1 2 3 4 
5
2 3 4 5 1 

result:

ok Correct!

Test #2:

score: 0
Accepted
time: 1ms
memory: 4136kb

input:

250
2 1
2
1 1
1 1
1 1
1
0
2 2
1 1
1 1
2 2 1
2 2
0 2
2 1 2
1 2
1 1
1
1 1
1 2
1 0
0
1 2
1 0
0
2 1
2
1 1
0
1 2
1 0
0
2 1
2
1 1
1 1
1 1
1
1 1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2
0
3 3 5 2
2 4 3
0
5 5
1 0 1 1 2
0
1 4
1 3
1 3
0
5 5
1 2 1 1 0
1 ...

output:

3
2 4 3 1 5 
1
1 2 3 4 5 
2
1 5 2 3 4 
3
1 3 5 2 4 
2
1 3 2 4 5 
4
2 5 4 1 3 
3
1 4 3 2 5 
2
2 4 1 3 5 
1
1 2 3 4 5 
4
1 2 3 4 5 
2
2 3 1 4 5 
2
1 4 2 3 5 
3
2 3 5 1 4 
3
3 4 1 2 5 
3
1 2 4 3 5 
3
1 3 2 4 5 
2
1 3 2 4 5 
3
1 4 5 2 3 
1
1 2 3 4 5 
3
3 5 2 1 4 
1
4 1 2 3 5 
2
3 4 1 2 5 
2
1 4 2 3 5 
2...

result:

ok Correct!

Test #6:

score: 0
Accepted
time: 1ms
memory: 3856kb

input:

10
45 47
3 0 2 0 1 1 1 0 2 0 1 0 0 3 0 0 0 4 0 1 0 0 1 2 1 1 1 0 1 1 1 0 0 0 0 1 0 0 0 1 2 4 1 2 1 2 3
7 1 37 21 3 13 43 22
0
10 23 46 22 40 12 19 47 27 16 42
4 29 19 45 35
10 6 26 2 43 41 7 9 16 42 44
5 39 40 34 46 14
3 34 3 38
8 10 5 38 23 19 37 9 34
0
5 31 29 15 13 35
3 40 4 28
1 7
6 29 12 9 35 2...

output:

33
1 11 12 14 15 19 29 37 38 39 42 16 30 17 43 7 31 21 6 40 10 35 8 5 25 36 44 4 13 24 3 18 34 2 9 20 22 23 26 27 28 32 33 41 45 
39
10 12 14 15 16 20 30 32 43 44 25 45 28 29 3 35 19 17 40 31 33 38 36 2 24 18 7 9 21 42 11 34 5 26 1 41 6 23 39 4 8 13 22 27 37 
36
3 4 10 20 28 29 31 32 33 46 47 16 17 ...

result:

ok Correct!

Test #7:

score: -100
Runtime Error

input:

1
499 497
1 2 0 2 0 1 0 0 0 2 1 2 0 3 1 2 0 0 0 1 0 1 0 2 1 0 1 0 1 1 1 2 0 1 0 1 0 2 2 3 1 1 2 1 0 0 1 0 2 3 0 1 0 0 2 0 1 2 1 0 0 1 2 0 0 2 0 2 0 1 0 1 0 0 1 0 0 1 1 1 1 1 0 0 0 1 2 3 0 0 0 4 2 2 1 2 2 0 1 0 1 0 2 0 1 0 2 0 0 1 1 1 3 2 0 2 2 2 0 1 1 1 1 1 0 1 0 1 1 1 1 1 2 0 0 1 0 2 1 2 1 2 1 0 1 ...

output:


result: