二分图最大匹配 (hdu1281、1528)

(hdu1281)棋盘游戏

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2544    Accepted Submission(s): 1482


Problem Description
小希和Gardon在玩一个游戏:对一个N*M的棋盘,在格子里放尽量多的一些国际象棋里面的“车”,并且使得他们不能互相攻击,这当然很简单,但是Gardon限制了只有某些格子才可以放,小希还是很轻松的解决了这个问题(见下图)注意不能放车的地方不影响车的互相攻击。 
所以现在Gardon想让小希来解决一个更难的问题,在保证尽量多的“车”的前提下,棋盘里有些格子是可以避开的,也就是说,不在这些格子上放车,也可以保证尽量多的“车”被放下。但是某些格子若不放子,就无法保证放尽量多的“车”,这样的格子被称做重要点。Gardon想让小希算出有多少个这样的重要点,你能解决这个问题么?
 

Input
输入包含多组数据, 
第一行有三个数N、M、K(1
 

Output
对输入的每组数据,按照如下格式输出: 
Board T have C important blanks for L chessmen.
 

Sample Input
 
    
3 3 4 1 2 1 3 2 1 2 2 3 3 4 1 2 1 3 2 1 3 2
 

Sample Output
 
    
Board 1 have 0 important blanks for 2 chessmen. Board 2 have 3 important blanks for 3 chessmen.
 

题意:棋盘中一些格子可以放棋子,但要求棋子不能同行也不能同列。有一些格子若不放棋子,则最大匹配数目减少,则这个格子就是“重要点

二分图的匹配:给定一个二分图G,M为G边集的一个子集,如果M满足当中的任意两条边都不依附于同一个顶点,则称M是一个匹配。 

#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#include"algorithm"
using namespace std;
#define N 105
int g[N][N];
int mark[N],link[N],n,m;
int find(int k)
{
    int i;
    for(i=1;i<=m;i++)
    {
        if(g[k][i]&&!mark[i])
        {
            mark[i]=1;
            if(link[i]==-1||find(link[i]))
            {
                link[i]=k;
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    int i,j,u,v,k,T=1;
    while(scanf("%d%d%d",&n,&m,&k)!=-1)
    {
        memset(link,-1,sizeof(link));
        memset(g,0,sizeof(g));
        while(k--)
        {
            scanf("%d%d",&u,&v);
            g[u][v]=1;
        }
        int ans=0,cnt=0;
        for(i=1;i<=n;i++)
        {
            memset(mark,0,sizeof(mark));
            ans+=find(i);
        }
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=m;j++)
            {
                if(g[i][j])
                {
                    g[i][j]=0;
                    memset(link,-1,sizeof(link));
                    int t=0;
                    for(k=1;k<=n;k++)
                    {
                        memset(mark,0,sizeof(mark));
                        t+=find(k);
                    }
                    g[i][j]=1;
                    if(t

(hdu1528、1962)Card Game Cheater

Card Game Cheater

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1309    Accepted Submission(s): 695


Problem Description
Adam and Eve play a card game using a regular deck of 52 cards. The rules are simple. The players sit on opposite sides of a table, facing each other. Each player gets k cards from the deck and, after looking at them, places the cards face down in a row on the table. Adam’s cards are numbered from 1 to k from his left, and Eve’s cards are numbered 1 to k from her right (so Eve’s i:th card is opposite Adam’s i:th card). The cards are turned face up, and points are awarded as follows (for each i ∈ {1, . . . , k}):


If Adam’s i:th card beats Eve’s i:th card, then Adam gets one point.


If Eve’s i:th card beats Adam’s i:th card, then Eve gets one point.


A card with higher value always beats a card with a lower value: a three beats a two, a four beats a three and a two, etc. An ace beats every card except (possibly) another ace.


If the two i:th cards have the same value, then the suit determines who wins: hearts beats all other suits, spades beats all suits except hearts, diamond beats only clubs, and clubs does not beat any suit. 

For example, the ten of spades beats the ten of diamonds but not the Jack of clubs. 

This ought to be a game of chance, but lately Eve is winning most of the time, and the reason is that she has started to use marked cards. In other words, she knows which cards Adam has on the table before he turns them face up. Using this information she orders her own cards so that she gets as many points as possible.

Your task is to, given Adam’s and Eve’s cards, determine how many points Eve will get if she plays optimally. 

 

Input
There will be several test cases. The first line of input will contain a single positive integer N giving the number of test cases. After that line follow the test cases.

Each test case starts with a line with a single positive integer k <= 26 which is the number of cards each player gets. The next line describes the k cards Adam has placed on the table, left to right. The next line describes the k cards Eve has (but she has not yet placed them on the table). A card is described by two characters, the first one being its value (2, 3, 4, 5, 6, 7, 8 ,9, T, J, Q, K, or A), and the second one being its suit (C, D, S, or H). Cards are separated by white spaces. So if Adam’s cards are the ten of clubs, the two of hearts, and the Jack of diamonds, that could be described by the line

TC 2H JD
 

Output
For each test case output a single line with the number of points Eve gets if she picks the optimal way to arrange her cards on the table.

 

Sample Input
 
    
3 1 JD JH 2 5D TC 4C 5H 3 2H 3H 4H 2D 3D 4D
 

Sample Output
 
    
1 1 2
 


题意:相当于田忌赛马的游戏,只不过换成扑克牌。比较点数大小,点数相同按花色分大小,红桃》黑桃》方块》梅花。

思路:把每张牌和一个数字映射起来,使得映射后的大小和牌中一样。然后建图,把 Eve的牌大于Adam的牌建一条边。

然后,求出最大匹配就OK了。

#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#define N 60
int a[N],b[N],g[N][N];
int mark[N],link[N];
int getch(char*s)    //把牌数转化为数字
{
    int t;
    switch(s[0])
    {
        case 'T':t=9;break;
        case 'J':t=10;break;
        case 'Q':t=11;break;
        case 'K':t=12;break;
        case 'A':t=13;break;
        default :t=s[0]-'0'-1;break;
    }
    t*=4;
    if(s[1]=='S')
        t-=1;
    else if(s[1]=='D')
        t-=2;
    else if(s[1]=='C')
        t-=3;
    //printf("%d\n",t);
    return t;
}
int find(int k)       //匈牙利算法求最大匹配数目
{
    int i;
    for(i=1;i



你可能感兴趣的:(二分图)