zoukankan      html  css  js  c++  java
  • uva 11080 二分图判定

    Problem G

    Place the Guards
    Input: Standard Input

    Output: Standard Output

    In the country of Ajabdesh there are some streets and junctions. Each street connects 2 junctions. The king of Ajabdesh wants to place some guards in some junctions so that all the junctions and streets can be guarded by them. A guard in a junction can guard all the junctions and streets adjacent to it. But the guards themselves are not gentle. If a street is guarded by multiple guards then they start fighting. So the king does not want the scenario where a street may be guarded by two guards. Given the information about the streets and junctions of Ajabdesh, help the king to find the minimum number of guards needed to guard all the junctions and streets of his country.

               

    Input:

    The first line of the input contains a single integer T (T<80) indicating the number of test cases. Each test case begins with 2 integers v (1 v 200) and e (0 e 10000.). v is the number of junctions and e is the number of streets. Each of the next e line contains 2 integer f and t denoting that there is a street between f and t. All the junctions are numbered from 0 to v-1.

    Output:

    For each test case output in a single line an integer m denoting the minimum number of guards needed to guard all the junctions and streets. Set the value of m as -1 if it is impossible to place the guards without fighting.

    Sample Input                             Output for Sample Input

    2

    4 2

    0 1

    2 3

    5 5

    0 1

    1 2

    2 3

    0 4

    3 4

     

    2

    -1

     


    Problemsetter: Abdullah-al-Mahmud

    Special Thanks: Md. Kamruzzaman

    比较显然的二分图,注意防坑就是。

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<vector>
    #include<cstdlib>
    #include<algorithm>
    #include<queue>
    #include<map>
    #include<stack>
    
    using namespace std;
    
    #define LL long long
    #define UINT unsigned int
    #define MAX_INT 0x7fffffff
    #define cint const int
    
    #define MAXN 222
    vector<int> g[MAXN];
    int n, m, col[MAXN];
    
    bool dfs(int u, int &sum, int &one, int color){
        col[u] = color;
        sum++;
        if(col[u] == 1) one++;
        for(int i=0; i<g[u].size(); i++){
            int v = g[u][i];
            if(col[v]==col[u] || !col[v]&&!dfs(v, sum, one, 3-color))
                return false;
        }
        return true;
    }
    
    int main(){
    //    freopen("C:\Users\Administrator\Desktop\in.txt","r",stdin);
        int T;
        scanf(" %d", &T);
        while(T--){
            scanf(" %d %d", &n, &m);
            int i, u, v;
            for(i=0; i<n; i++) g[i].clear();
            for(i=0; i<m; i++){
                scanf(" %d %d", &u, &v);
                g[u].push_back(v);
                g[v].push_back(u);
            }
            fill_n(col, n, 0);
            int ans = 0;
            for(i=0; i<n; i++) if(!col[i]){
                int sum=0, one=0;
                if(!dfs(i, sum, one, 1)) break;
                else if(one<sum) ans += min(one, sum-one);
                else ans += sum;
            }
            if(i<n) ans=-1;
            printf("%d
    ", ans);
        }
        return 0;
    }
    
  • 相关阅读:
    Ant 执行 exec cmd.exe 时路径包含空格的问题
    时区时差换算(GMT,UTC,PST,PDT)
    windows 共存多个位数不同的jdk时,eclipse的报错对应措施
    Windows下查询指定端口进程,并杀死
    关于windows的jdk
    第一阶段工作总结
    mac配置git mergetool为p4merge(2013笔记整理)
    ubuntu 14.04 安装压缩包版mysql
    关于微信公众号内嵌网页的几个meta标签
    关于js的keyCode
  • 原文地址:https://www.cnblogs.com/ramanujan/p/3378709.html
Copyright © 2011-2022 走看看