Codeforces 741C Arpa's overnight party and Mehrdad's silent entry

This question is the first example that Mr. Ho Siqi told us about construction. If you deal with a couple, you can just dye the dichotomy directly. But how to deal with the requirement that three people in any row have at least two kinds of food? According to Ho Siqi's explanation, this topic is to connect each couple first. Then connect 2k+1 with 2k+2. Why is this feasible? Just imagine if there is a ring after the edge is connected like this. So the number of points in this ring is even. So we just need to dye a bipartite graph on such a graph... I seem to have forgotten how to write the binary graph dyeing code for learning from Mr. Ho Siqi. .

Reference code:

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define DB double
#define SG string
using namespace std;
const int Max=1e7+5;
const int Mod=1e9+7;
const int Mx=1e5+5;
struct Node{
    int A,B;
}CP[Mx];
int N,Mark=1,Vis[Max],Col[Max];
int Cnt,To[Max],Next[Max],Head[Max];
inline int Read(){
    int X=0;char CH=getchar();bool F=0;
    while(CH>'9'||CH<'0'){if(CH=='-')F=1;CH=getchar();}
    while(CH>='0'&&CH<='9'){X=(X<<1)+(X<<3)+CH-'0';CH=getchar();}
    return F?-X:X;
}
inline void Write(int X){
    if(X<0)X=-X,putchar('-');
    if(X>9)Write(X/10);
    putchar(X%10+48);
}
void Insert(int X,int Y){
    To[++Cnt]=Y;Next[Cnt]=Head[X];Head[X]=Cnt;
}
void BFS(int Start){
    int I=0,J=1,K;
    Vis[1]=Start,Col[Start]=1;
    while(I<J){
        I++;
        int Cur=Vis[I];
        for(K=Head[Cur];K;K=Next[K]){
            int Y=To[K];
            if(Col[Y]&&Col[Y]!=3-Col[Cur]){
                Mark=0;return;
            }
            if(Col[Y]){
                continue;
            }Col[Y]=3-Col[Cur];
            Vis[++J]=Y;
        }
    }
}
int main(){
    int I,J,K;
    N=Read();
    for(I=1;I<=N;I++){
        CP[I].A=Read(),CP[I].B=Read();
        Insert(CP[I].A,CP[I].B);
        Insert(CP[I].B,CP[I].A);
    }
    for(I=1;I<=2*N;I+=2){
        Insert(I,I+1);Insert(I+1,I);
    }
    for(I=1;I<=2*N&&Mark;I++){
        if(Col[I]){
            continue;
        } else {
            BFS(I);
        }
    }
    if(!Mark){
        puts("-1");
    } else {
        for(I=1;I<=N;I++){
            Write(Col[CP[I].A]),putchar(' ');
            Write(Col[CP[I].B]),putchar('\n');
        }
    }
    return 0;
}

Posted by strangesoul on Tue, 05 Feb 2019 04:45:17 -0800