QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#504704#9102. Zayin and ElementsAfterlife#AC ✓3ms3744kbC++203.5kb2024-08-04 15:05:062024-08-04 15:05:07

Judging History

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

  • [2024-08-04 15:05:07]
  • 评测
  • 测评结果:AC
  • 用时:3ms
  • 内存:3744kb
  • [2024-08-04 15:05:06]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;

const int N=511;

vector<int>lnk[N];

int n,match[N],Q[N],head,tail;

int pred[N],base[N],start,finish,newbase;

bool inq[N],inb[N];

void push(int u)
{
    Q[tail++]=u;
    inq[u]=1;
}

int pop()
{
    return Q[head++];
}

int lca(int u,int v)
{
    bool inpath[N];
    for(int i=0;i<n;i++)
        inpath[i]=0;
    while(true){u=base[u];inpath[u]=1;if(u==start) break;u=pred[match[u]];}
    while(true){v=base[v];if(inpath[v]) break;v=pred[match[v]];}
    return v;
}

void ResetTrace(int u)
{
    int v;
    while(base[u]!=newbase)
    {
        v=match[u];
        inb[base[u]]=inb[base[v]]=1;
        u=pred[v];
        if(base[u]!=newbase)
            pred[u]=v;
    }
}

void Blossom(int u,int v)
{
    newbase=lca(u,v);
    for(int i=0;i<n;i++)
        inb[i]=0;
    ResetTrace(u);ResetTrace(v);
    if(base[u]!=newbase)
        pred[u]=v;
    if(base[v]!=newbase)
        pred[v]=u;
    for(int i=0;i<n;i++)
        if(inb[base[i]])
        {
            base[i]=newbase;
            if(!inq[i])
                push(i);
        }
}

bool find(int u)
{
    bool found=false;
    for(int i=0;i<n;i++)
        pred[i]=-1,base[i]=i;
    for(int i=0;i<n;i++)
        inq[i]=0;
    start=u,finish=-1;head=tail=0;push(start);
    while(head<tail)
    {
        int u=pop();
        for(int i=lnk[u].size()-1;i>=0;i--)
        {
            int v=lnk[u][i];
            if(base[u]!=base[v]&&match[u]!=v)
            {
                if(v==start||(match[v]>=0&&pred[match[v]]>=0))
                    Blossom(u,v);
                else if(pred[v]==-1)
                {
                    pred[v]=u;
                    if(match[v]>=0)
                        push(match[v]);
                    else
                    {
                        finish=v;
                        return 1;
                    }
                }
            }
        }
    }
    return found;
}

void aug()
{
    int u=finish,v,w;
    while(u>=0){v=pred[u];w=match[v];match[v]=u;match[u]=v;u=w;}
}

int T;

void add(int u,int v)
{
    lnk[u].push_back(v);
    lnk[v].push_back(u);
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>T;
    while(T--)
    {
        for(int i=0;i<n;i++)
            lnk[i].clear(),pred[i]=base[i]=inq[i]=inb[i]=0;
        start=finish=newbase=0;
        int L,R;
        cin>>L>>R;
        n=L-1;
        for(int i=1;i<=R;i++)
        {
            int a,b,c;
            cin>>a>>b>>c;
            int sn=n;
            n+=a;
            for(int j=1;j<b*2;j++)
                add(n+j,n+j+1);
            n+=b*2;
            for(int j=1;j<=c;j++)
                add(n+j,n+j+c);
            n+=c;
            int en=n;
            n+=c;
            int t;
            cin>>t;
            while(t--)
            {
                int x;
                cin>>x;
                x--;
                for(int j=sn+1;j<=en;j++)
                    add(x,j);
            }
        }
        int ans=-L;
        n++;
        for(int i=0;i<n;i++)
            match[i]=-1;
        for(int i=0;i<n;i++)
            if(match[i]==-1)
            {
                if(find(i))
                    aug(),ans++;
                else if(i<L)
                {
                    ans=-1;
                    break;
                }
            }
        // cerr<<MC<<endl;
        cout<<ans<<"\n";
    }    
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

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

output:

5
-1

result:

ok 2 lines

Test #2:

score: 0
Accepted
time: 3ms
memory: 3744kb

input:

5
9
10
0 0 10 2 3 8
0 0 10 2 4 6
0 8 2 2 2 4
0 0 10 3 1 3 8
0 4 6 2 2 3
0 8 2 3 2 6 7
0 9 1 2 1 7
0 2 8 3 1 4 9
0 7 3 1 5
0 0 10 3 5 6 9
3
10
0 9 1 1 2
0 5 5 1 1
0 1 9 1 1
0 7 3 1 1
0 7 3 0
0 0 10 0
0 6 4 1 3
0 9 1 0
0 7 3 0
0 9 1 1 2
90
20
0 6 4 12 1 4 5 22 32 64 66 67 73 87 88 89
0 1 9 12 3 8 21 3...

output:

94
97
155
151
152

result:

ok 5 lines