24h購物| | PChome| 登入
2012-06-22 16:37:28| 人氣1,609| 回應0 | 上一篇 | 下一篇

[UVA][二分圖圖色] 11080 - Place the Guards

推薦 0 收藏 0 轉貼0 訂閱站台

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 <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
vector<int> Link[201];
int color[201], access = 1, cnt, total;
void dfs(int nd) {
    total++;
    if(color[nd] == 1)
        cnt++;
    if(!access)
        return;
    for(vector<int>::iterator i = Link[nd].begin(); i != Link[nd].end(); i++) {
        if(color[*i] == color[nd])
            access = 0;
        else if(color[*i] == 0) {
            color[*i] = 3-color[nd];
            dfs(*i);
        }
    }
}
int main() {
    int t, n, m, i, j;
    scanf("%d", &t);
    while(t--) {
        scanf("%d %d", &n, &m);
        for(i = 0; i < n; i++)
            Link[i].clear();
        while(m--) {
            scanf("%d %d", &i, &j);
            Link[i].push_back(j);
            Link[j].push_back(i);
        }
        memset(color, 0, sizeof(color));
        int ans = 0;
        access = 1;
        for(i = 0; i < n; i++) {
            if(color[i] == 0 && access == 1) {
                access = 1;
                color[i] = 1;
                cnt = 0, total = 0;
                dfs(i);
                if(cnt > total - cnt && total > 1)
                    cnt = total - cnt;
                ans += cnt;
            }
        }
        if(access) {
            printf("%d\n", ans);
        } else {
            puts("-1");
        }
    }
    return 0;
}

台長: Morris
人氣(1,609) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 343 - What Base Is This?
此分類上一篇:[UVA] 713 - Adding Reversed Numbers

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文