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;
    }
    
  • 相关阅读:
    VB Open 函数详解 打开、关闭、读、写文件
    VB程序设计中Combobox的取值问题
    vb中typename函数
    VB.NET Event RaiseEvent用处
    通过关键字Event定义用户自己的事件
    [转]如何在注册表中进行查找
    [转]ADT中通过DDMS导入文件出错ddms transfer error: Read-only file system,Failed to push selection: Read-only file system
    [转]在eclipse打开的android虚拟手机,打开File Explorer,下面是空的没有data、mnt、system三个文件
    [转]Android开发过程中遇到的问题
    [转]如何解决android模拟器慢的问题
  • 原文地址:https://www.cnblogs.com/ramanujan/p/3378709.html
Copyright © 2011-2022 走看看