[Explanations] seating arrangements

Topic Link

Subject to the effect that, for each point attached to two sides, that is, people like to do two positions, seeking the maximum matching on all seats match

Here, we like to open a two-dimensional, represents the seat two matches, the return to 01.

Hungary template title.

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int match[500000][2];
int head[500000],n,ans;
int vis[500000],tot; 
struct node{
    int nxt,to;
}e[500000];
bool dfs(int x){
    for(int i=head[x];i;i=e[i].nxt){
        int j=e[i].to;
        if(vis[j])continue; 
        VIS [j] = . 1 ;
         IF (! Match [j] [ 0 ] || DFS (match [j] [ 0 ])) { // first seat j is not already matched 
            match [j] [ 0 ] = X;
             return  to true ; 
        } 
        IF (! match [J] [ . 1 ] || DFS (match [J] [ . 1 ])) { // supra 
            match [J] [ . 1 ] = X;
             return  to true ; 
        } 
    } return  to false ; // no match 
}
 void work(){
    for(int i=1;i<=(n<<1);++i){
        memset(vis,0,sizeof(vis));
        ans+=dfs(i);//求最大匹配 
    }
}
inline void add(int x,int y){//临接表 
    e[++tot].nxt=head[x];
    head[x]=tot;
    e[tot].to=y; 
} 
int main(){
    scanf("%d",&n);
    for ( int I = . 1 ; I <= (n-<< . 1 ); I ++) { // 2N points 
        int X, Y; 
        Scanf ( " % D% D " , & X, & Y); 
        the Add (I , X); the Add (I, Y); // I want edge positions even 
    } 
    Work (); 
    the printf ( " % D \ n- " , ANS);
     return  0 ; 
}

 

Guess you like

Origin www.cnblogs.com/h-lka/p/11256487.html