Algorithm


problem Link : https://onlinejudge.org/index.php?option=onlinejudge&Itemid=8&page=show_problem&problem=2021 

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 2 4 2 0 1 2 3 5 5 0 1 1 2 2 3 0 4 3 4

Sample Output 2 -1

Code Examples

#1 Code Example with C Programming

Code - C Programming

#include<bits/stdc++.h>
using namespace std;

int cntColor1 = 0;
int cntColor2 = 0;
vector<int> color;
unordered_map < int,unordered_set<int>> graph;

bool dfs(int n, int col){
    if(color[n] != 0) return color[n] == col;
    color[n] = col;
    if(col == 1) cntColor1++;
    else cntColor2++;
    for(auto& neigh : graph[n]){
        if(!dfs(neigh,-col)) return false;
    }
    return true;
}

int main()
{
    int t,n,m,a,b;
    scanf("%d",&t);
    while(t--){
        scanf("%d %d",&n,&m);
        color.assign(n,0);
        graph.clear();
        for(int i = 0; i < m; i++){
            scanf("%d %d",&a,&b);
            graph[a].insert(b);
            graph[b].insert(a);
        }
        bool bipartile = true;
        int res = 0;
        for(int i=0;i < n && bipartile;i++){
            if(color[i] == 0){
                cntColor1 = cntColor2 = 0;
                bipartile = dfs(i,1);
                // foreach component, take less count color
                res += max(1,min(cntColor1,cntColor2));
            }
        }
        if(bipartile) printf("%d\n",res);
        else printf("%d\n",-1>;
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
2
4 2
0 1
2 3
5 5
0 1
1 2
2 3
0 4
3 4

Output

x
+
cmd
2
-1
Advertisements

Demonstration


UVA Online Judge solution - 11080-Place the Guards - UVA Online Judge solution in C,C++,java

Previous
UVA Online Judge solution - 11078-Open Credit System - UVA Online Judge solution in C,C++,java
Next
UVA Online Judge solution - 11084-Anagram Division - UVA Online Judge solution in C,C++,java