1721. Two Sides of the Same Coin

http://acm.timus.ru/problem.aspx?space=1&num=1721

左边:rank   3,4,7,8,11,12.........

右边:rank   5,6,9,10,13,14.........

然后匈牙利算法

代码:

#include<iostream>

#include<stdio.h>

#include<string.h>

#include<math.h>

#include<algorithm>

#include<vector>

#include<set>

#include<map>

#include<string>

#include<queue>

#include<stack>

#include <iomanip>

using namespace std;

#define LL long long

const int INF=0x3f3f3f3f;

const int N=1006;

bool visited[N];

string name[N];

string type[N];

int r[N];

int f[N];

int head[N],I;

struct node

{

    int j,next;

}side[N*N];

void add(int i,int j)

{//cout<<i<<" "<<j<<endl;

    side[I].j=j;

    side[I].next=head[i];

    head[i]=I++;

}

bool dfs(int x)

{

    for(int t=head[x];t!=-1;t=side[t].next)

    {

        int j=side[t].j;

        if(!visited[j])

        {

            visited[j]=true;

            if(f[j]==-1||dfs(f[j]))

            {

                f[j]=x;

                return true;

            }

        }

    }

    return false;

}

inline bool L(int i)

{

    if((r[i]-3)%4==0||(r[i]-4)%4==0)

    return true;

    return false;

}

int main()

{

    //freopen("data.in","r",stdin);

    int n;

    while(cin>>n)

    {

        memset(head,-1,sizeof(head));

        I=0;

        for(int i=0;i<n;++i)

        {

            cin>>name[i]>>type[i]>>r[i];

            for(int j=0;j<i;++j)

            {

                if(abs(r[i]-r[j])==2&&(type[i]!=type[j]||type[i]=="anything"))

                {

                    if(L(i)&&!L(j))

                    add(i,j);

                    if(L(j)&&!L(i))

                    add(j,i);

                }

            }

        }

        int ans=0;

        memset(f,-1,sizeof(f));

        for(int i=0;i<n;++i)

        {

            if(L(i))

            {

                memset(visited,false,sizeof(visited));

                if(dfs(i))

                ++ans;

            }

        }

        cout<<ans<<endl;

        for(int i=0;i<n;++i)

        {

            if(f[i]!=-1)

            {

                if(type[i]=="statements"||type[f[i]]=="testdata")

                cout<<name[i]<<" "<<name[f[i]]<<endl;

                else

                cout<<name[f[i]]<<" "<<name[i]<<endl;

            }

        }



    }

    return 0;

}

 

你可能感兴趣的:(ide)