Communards shipyard Project1129 successful development

Face questions

This question God.

No afternoon watching a train of thought, the feeling is not a deep understanding of bipartite graph, and re-read it again bipartite graph on the Blue Book, also played questions practiced hand, but did not come up with this question ...

So just decided to look back meal solution to a problem, we understood why.

Comrades each point can be seen as a side match, it's the number of rows and columns, even an edge, you will find the whole image is a set of matching.

When exchanging rows and columns, the equivalent of the process of consultation in Hungary algorithm, so the maximum matching unchanged.

You can have n or more matches if exchange, it shows that Italy will be able to achieve.

With pictures as follows (Figure fonts in a little please forgive me):

With pictures 1.png

With pictures 2.png

Then it is to run a bare Hungary, find the number of matching it.

Code:


#pragma GCC diagnostic error "-std=c++11"
#pragma GCC target("sse2")
#pragma GCC optimize(3)
#pragma GCC target("avx")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>

using namespace std;

struct edge
{
    int to,next;
}e[1000010];

int t,n,size,CommunismParty_cnt;
int head[1000010],match[1000010];
bool flag[1000010];

inline void EdgeAdd(int,int);
inline void Hungary();
inline bool find(int);

int main()
{
    scanf("%d",&t);
    for(int _=1;_<=t;_++)
    {
        scanf("%d",&n);
        CommunismParty_cnt=0;
        size=0;
        memset(head,-1,sizeof(head));
        memset(match,0,sizeof(match));
        memset(e,0,sizeof(e));
        for(int __=1;__<=n;__++)
        {
            for(int ___=1;___<=n;___++)
            {
                int CommunistYouthLeague_color;
                scanf("%d",&CommunistYouthLeague_color);
                if(CommunistYouthLeague_color==1)
                {
                    EdgeAdd(__,___);
                }
            }
        }
        Hungary();
        printf("%s\n",CommunismParty_cnt>=n?"Yes":"No");
    }
return 0;
}

inline void EdgeAdd(int from,int to)
{
    e[++size].to=to;
    e[size].next=head[from];
    head[from]=size;
}

inline void Hungary()
{
    for(int _=1;_<=n;_++)
    {
        memset(flag,false,sizeof(flag));
        if(find(_)==true)
        {
            CommunismParty_cnt++;
        }
    }
}

inline bool find(int from)
{
    for(int _=head[from];_!=-1;_=e[_].next)
    {
        int to=e[_].to;
        if(flag[to]==false)
        {
            flag[to]=true;
            if(match[to]==0||find(match[to])==true)
            {
                match[to]=from;
                return true;
            }
        }
    }
return false;
}

Guess you like

Origin www.cnblogs.com/Lemir3/p/11104604.html