zoukankan      html  css  js  c++  java
  • P1330 封锁阳光大学 (二分图染色)

    题目链接

    题解:

    每一个子连通图,对它进行黑白染色,然后取两种染色中的最小值,然后最后汇总。

    #include <bits/stdc++.h>
    # define LL long long
    using namespace std;
    
    const int maxn=10000+10;
    const int maxm=100000+10;
    int n,m;
    int col[maxn];
    int sum[2];
    
    struct Edge{
        int to,next;
    }e[maxm<<1];
    int head[maxn];
    int en;
    
    void add(int u, int v){
        e[en].next=head[u];
        e[en].to=v;
        head[u]=en;
        ++en;
    }
    
    bool dfs(int u, int color){
        if(col[u]!=-1 && col[u]!=color) return false;
        if(col[u]!=-1 && col[u]==color) return true;
        col[u]=color;
        sum[color]++;
        for(int i=head[u];i!=-1;i=e[i].next){
            int v=e[i].to;
            if(!dfs(v,1-color)) return false;
        }
        return true;
    }
    
    int main(){
        memset(head,-1,sizeof(head));
        memset(col,-1,sizeof(col));
        scanf("%d %d", &n, &m);
        for(int i=1;i<=m;++i){
            int u,v;
            scanf("%d %d", &u, &v);
            add(u,v);
            add(v,u);
        }
        int res=0;
        for(int i=1;i<=n;++i){
            if(col[i]!=-1) continue;
            sum[0]=0;
            sum[1]=0;
            if(!dfs(i,0)){
                printf("Impossible");
                return 0;
            }
            res+=min(sum[0],sum[1]);
        }
        printf("%d", res);
        return 0;
    }
  • 相关阅读:
    UITabar 设置字体大小/颜色
    NSURLSession的基本使用
    报错/警告提示
    实现毛玻璃模糊效果/DRNRealTimeBlur
    免证书真机调试
    xcode添加音效
    NSCalenda日历类
    NSDate--日期格式
    NSArray其中的方法--遍历,
    Mysql学习笔记004
  • 原文地址:https://www.cnblogs.com/FEIIEF/p/12264646.html
Copyright © 2011-2022 走看看