uva 11080 二分图判定

这篇博客介绍了一个关于在Ajabdesh王国中合理放置守卫以确保所有街道和交汇点都能得到保护的问题。通过提供输入数据,即街道数量和交汇点数量,以及具体的街道连接情况,读者可以了解如何计算所需的最小守卫数量。文章详细阐述了二分图的概念,并提供了一个C++代码示例来解决这个问题。

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\n", ans);
    }
    return 0;
}

转载于:https://www.cnblogs.com/ramanujan/p/3378709.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值