zoukankan      html  css  js  c++  java
  • UVA11080

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

    二分图染色:

    #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
    ", solve());
        }
        return 0;
    }
  • 相关阅读:
    Window 命令
    HTTP 状态码
    Mysql基本用法-存储引擎-04
    Mysql基本用法-存储引擎-03
    Mysql基本用法-left join、right join、 inner join、子查询和join-02
    Mysql基本用法-01
    二进制编码-详细讲解
    JS操作文件
    PHP5接口技术入门
    PHP5中__get()、__set()方法
  • 原文地址:https://www.cnblogs.com/soTired/p/5323315.html
Copyright © 2011-2022 走看看