Blocking sunlight University

Topics address

Rarely done bipartite graph title, indeed less skilled.

Pre-knowledge: for a picture, if not odd, then the ring is a bipartite graph. There can be no odd ring dyeing process there is no contradiction to judge.
Pit (your own food, you can not say that people pit ah)
1. The figures are not necessarily Unicom. Only to find not just a point (for example, 1) get away. To analyze communication with each block.
2. (write their own place to hang) find contradictions \ (return \) . If \ (i == n \) is found contradictory statements can not execute the judgment. In order to finally determine what the insurance.
On the code:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int N = 400005; 
int n,m,head[N],tot;
int book[N],ans1,ans2,ans;
bool flag=0;
queue<int> q1,q2;
struct edge{
    int node,next;
}e[N];
void add(int x,int y)
{
    e[++tot].node=y;
    e[tot].next=head[x];
    head[x]=tot;
}
void work()
{
    while(!q1.empty()||!q2.empty())
    {
        if(!q1.empty())
        {
            while(!q1.empty())
            {
                int u=q1.front(); ++ans1;
                q1.pop();
                for(int i=head[u];i;i=e[i].next)
                {
                    int v=e[i].node;
                    if(book[v]==1) { flag=1; return ;}
                    if(!book[v]) { book[v]=2; q2.push(v);}
                }
            }
        } 
        else
        {
            while(!q2.empty())
            {
                int u=q2.front(); ++ans2;
                q2.pop();
                for(int i=head[u];i;i=e[i].next)
                {
                    int v=e[i].node;
                    if(book[v]==2) {  flag=1; return ;}
                    if(!book[v]) { book[v]=1; q1.push(v); }
                }
            }
        }
    }
}
int main()
{
    scanf("%d%d",&n,&m);
    int x,y;
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d",&x,&y);
        add(x,y); add(y,x);
    }
    for(int i=1;i<=n;i++)
    if(!book[i])
    {
        if(flag) { printf("Impossible\n"); return 0;}
        ans1=ans2=0;
        book[i]=1; q1.push(i);
        work();
        ans+=min(ans1,ans2);
    }
    printf("%d\n",ans);
    return 0;
}

Guess you like

Origin www.cnblogs.com/karryW/p/11354734.html