24h購物| | PChome| 登入
2012-05-13 15:52:55| 人氣959| 回應0 | 上一篇 | 下一篇

[UVA][二分圖] 11045 - My T-shirt suits me

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


  My T-shirt suits me 

Our friend Victor participates as an instructor in an environmental volunteer program. His boss asked Victor to distribute N T-shirts to M volunteers, one T-shirt each volunteer, where N is multiple of six, and N$ ge$M. There are the same number of T-shirts of each one of the six available sizes: XXL, XL, L, M , S, and XS. Victor has a little problem because only two sizes of the T-shirts suit each volunteer.


You must write a program to decide if Victor can distribute T-shirts in such a way that all volunteers get a T-shirt that suit them. If N $ neq$ M, there can be some remaining T-shirts.

Input 

The first line of the input contains the number of test cases. For each test case, there is a line with two numbers N and M. N is multiple of 6, 1$ le$N$ le$36, and indicates the number of T-shirts. Number M, 1$ le$M$ le$30, indicates the number of volunteers, with N$ ge$M. Subsequently, M lines are listed where each line contains, separated by one space, the two sizes that suit each volunteer (XXL, XL, L, M , S, or XS).

Output 

For each test case you are to print a line containing YES if there is, at least, one distribution where T-shirts suit all volunteers, or NO, in other case.

Sample Input 

 
3
18 6
L XL
XL L
XXL XL
S XS
M S
M L
6 4
S XL
L S
L XL
L XL
6 1
L M

Sample Output 

 
YES
NO
YES

照理講應該不會過得, 不過測資很水, 用 dfs 就過了, 就不必使用最大匹配了 

#include <stdio.h>
#include <string.h>
const char s[][4] = {"XXL", "XL", "L", "M", "S", "XS"};
int code(const char *str) {
    static int i;
    for(i = 0; i < 6; i++)
        if(!strcmp(s[i], str))
            return i;
}
int map[30][2], flag, used[6];
int n, m, t;
void dfs(int idx) {
    if(flag)
        return;
    if(idx == m) {
        flag = 1;
        return;
    }
    if(used[map[idx][0]] > 0) {
        used[map[idx][0]]--;
        dfs(idx+1);
        used[map[idx][0]]++;
    }
    if(used[map[idx][1]] > 0) {
        used[map[idx][1]]--;
        dfs(idx+1);
        used[map[idx][1]]++;
    }
}
int main() {
    char str[20];
    int i, num;
    scanf("%d", &t);
    while(t--) {
        scanf("%d %d", &n, &m);
        n = n/6;
        for(i = 0; i < m; i++) {
            scanf("%s", str);
            num = code(str);
            map[i][0] = num;
            scanf("%s", str);
            num = code(str);
            map[i][1] = num;
        }
        for(i = 0; i < 6; i++)
            used[i] = n;
        flag = 0;
        dfs(0);
        if(flag)
            puts("YES");
        else
            puts("NO");
    }
    return 0;
}


台長: Morris
人氣(959) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][Math] 545 - Heads
此分類上一篇:[UVA] 750 - 8 Queens Chess Problem

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