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;
    }
    
  • 相关阅读:
    uniapp的v-for的key不同平台的兼容解决
    想好要做什么,然后就放手去做吧!
    中国姓氏大全(常见508个,罕见740个)
    swiper鼠标移入停止滚动 移出开始滚动
    swiper文字垂直滚动(公告栏)
    简体生僻汉字大全21418个-GBK编码中的汉字
    uniapp终极查bug大法-无私分享
    uniapp数据更新了但是页面没有渲染-解决方案
    uniapp引入font-awsome字体图标-疑难解决
    优化问题及KKT条件
  • 原文地址:https://www.cnblogs.com/ramanujan/p/3378709.html
Copyright © 2011-2022 走看看