夏夜、

心若平似镜、何题不AC。

UVA 1364 Knights of the Round Table 双连通分量+二分图判定

//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<sstream>
#include<cmath>
#include<climits>
#include<string>
#include<map>
#include<queue>
#include<vector>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
#define pb(a) push(a)
#define INF 0x1f1f1f1f
#define lson idx<<1,l,mid
#define rson idx<<1|1,mid+1,r
#define PI  3.1415926535898
template<class T> T min(const T& a,const T& b,const T& c) {
    return min(min(a,b),min(a,c));
}
template<class T> T max(const T& a,const T& b,const T& c) {
    return max(max(a,b),max(a,c));
}
void debug() {
#ifdef ONLINE_JUDGE
#else

    freopen("d:\\in1.txt","r",stdin);
    freopen("d:\\out1.txt","w",stdout);
#endif
}
int getch() {
    int ch;
    while((ch=getchar())!=EOF) {
        if(ch!=' '&&ch!='\n')return ch;
    }
    return EOF;
}

struct Edge
{
    int u,v;
};

const int maxn=1111;

int pre[maxn],iscut[maxn],bccno[maxn];
int dfs_clock,bcc_cnt;

vector<int> g[maxn],bcc[maxn];

stack<Edge> s;

int dfs(int u,int fa)
{
    int lowu = pre[u] = ++dfs_clock;

    int child=0;
    for(int i=0;i<g[u].size();i++)
    {
        int v=g[u][i];
        Edge e = (Edge){u,v};

        if(!pre[v])
        {
            s.push(e);
            child++;
            int lowv=dfs(v,u);

            lowu=min(lowu,lowv);
            if(lowv>=pre[u])
            {
                bcc_cnt++;
                bcc[bcc_cnt].clear();
                iscut[u]=true;
                while(1)
                {
                    Edge x=s.top();s.pop();
                    if(bccno[x.u]!=bcc_cnt)
                    {
                        bcc[bcc_cnt].push_back(x.u);
                        bccno[x.u]=bcc_cnt;
                    }

                    if(bccno[x.v]!=bcc_cnt)
                    {
                        bcc[bcc_cnt].push_back(x.v);
                        bccno[x.v]=bcc_cnt;
                    }
                    if(x.u==u&&x.v==v)break;
                }
            }

        }else if(pre[v]<=pre[u]&&v!=fa)
        {
            lowu=min(pre[v],lowu);
            s.push(e);
        }
    }
    if(fa<0&&child==1)iscut[u]=0;
    return lowu;
}

void find_bcc(int n)
{
    memset(bccno,0,sizeof(bccno));
    memset(iscut,0,sizeof(iscut));
    memset(pre,0,sizeof(pre));
    dfs_clock=bcc_cnt=0;
    for(int i=0;i<n;i++)
        if(!pre[i])dfs(i,-1);
}

int odd[maxn],color[maxn];

bool bipartite(int u,int b)
{
    for(int i=0;i<g[u].size();i++)
    {
        int v=g[u][i];
        if(bccno[v]!=b)continue;
        if(color[v]==color[u])return false;
        if(!color[v])
        {
            color[v]=3-color[u];
            if(!bipartite(v,b))return false;
        }

    }
    return true;
}

int A[maxn][maxn];

int main()
{
    int ca=0,n,m;
    while(scanf("%d%d",&n,&m)!=EOF&&n)
    {
        memset(A,0,sizeof(A));
        for(int i=0;i<n;i++)
            g[i].clear();
        for(int i=0;i<m;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            u--;v--;
            A[u][v]=A[v][u]=1;
        }

        for(int i=0;i<n;i++)
        {
            for(int j=i+1;j<n;j++)
            {
                if(!A[i][j])
                {
                    g[i].push_back(j);
                    g[j].push_back(i);
                }
            }
        }
        find_bcc(n);

        memset(odd,0,sizeof(odd));
        for(int i=1;i<=bcc_cnt;i++)
        {
            memset(color,0,sizeof(color));
            for(int j=0;j<bcc[i].size();j++)bccno[bcc[i][j]]=i;
            int u=bcc[i][0];
            color[u]=1;
            if(!bipartite(u,i))
                for(int j=0;j<bcc[i].size();j++)
                    odd[bcc[i][j]]=1;
        }
        int res=0;
        for(int i=0;i<n;i++)
            if(!odd[i])res++;
        printf("%d\n",res);
    }
    return 0;
}
View Code

 

posted on 2014-03-19 23:13  BMan、  阅读(271)  评论(0编辑  收藏  举报

导航