UVA11080 - Place the Guards (二分图染色)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA11080 - Place the Guards (二分图染色)相关的知识,希望对你有一定的参考价值。

题意: 给出一个无向图,  有联通地方,  求联通个数。 (好吧, 我还不是很懂)

 

二分图染色:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

const int MAXNODE = 510;
const int MAXEDGE = 100010;

struct Edge
{
    int v, next;
    Edge() {}
    Edge(int v, int next): v(v), next(next){} 
} E[MAXEDGE];

int head[MAXNODE], color[MAXNODE];
int tot, n, m;
int s, b;
int sum;
 
void addEdge(int u, int v)
{
    E[tot]=Edge(v, head[u]);
    head[u]=tot++;
}

bool bipartite(int u)
{
    if(color[u]==1) s++;
    else b++;
    
    for(int i=head[u]; i!=-1; i=E[i].next)
    {
        int v=E[i].v;
        if(color[v] == color[u]) return false;
        if(!color[v])
        {
            color[v]=3-color[u];
            if(!bipartite(v)) return false;
        }
    }
    return true;
}

void init()
{
    scanf("%d%d", &n, &m);
    memset(head, -1, sizeof(head));
    //memset(color, 0, sizeof(color));
    tot=0; sum=0;
    
    int u, v;
    for(int i=0; i<m; i++)
    {
        scanf("%d%d", &u, &v);
        addEdge(u, v);
        addEdge(v, u); 
    }
}

int solve()
{
    //所有颜色未定, 将1这个节点定义成颜色1, 然后dfs进行染色 ;
    memset(color, 0, sizeof(color));
    //color[1]=1;
    for(int i=0; i<n; i++)
    {
        if(color[i]==0)
        {
            s=b=0;
            color[i]=1;
            if(!bipartite(i)) return -1;
            sum += max(1, min(s, b));
        }
    }
    return sum;
}
int main()
{
    //scanf("%d%d", &n, &m);
    int T;
    scanf("%d", &T);
    while(T--)
    {
        init();
        printf("%d\n", solve());
    }
    return 0;
}

 

以上是关于UVA11080 - Place the Guards (二分图染色)的主要内容,如果未能解决你的问题,请参考以下文章

CF782B The Meeting Place Cannot Be Changed

The End of Gold Age:Why Beijing isn't the Best Place for Expats?

c_cpp 11080

Tip of the Week #65: Putting Things in their Place

Tip of the Week #65: Putting Things in their Place

ZOJ 1654--Place the Robots二分匹配 &amp;&amp; 经典建图