Cats and dogs (bipartite graph)

Cats and dogs

Memory limit: 128 MiB time limit: 1000 ms standard output
 
 

Title Description

Small k classmates playing a game, in the game he played the owner of a circus, now a small circus k students need to use the A and B dogs cats hold a show, before performing his audience voted, voting class capacity is: I want to see No. ___ cat / dog show, do not want to see No. ___ cat / dog show. Notice that each audience are more like cats or dogs prefer, it will be behind two empty hook on different content. Like cats audience will choose to empty the cat behind the first, second behind empty choosing a dog; dog will choose the other hand behind the first empty space behind the second choice cat. For each audience, vote only when the contents of TA have been satisfied (ie, TA would like to see an animal show appearances, TA did not want to see animals not involved in the show) when, TA will see the show. Of course, watching the show is to pay the ticket money, as the owner of the circus, a small k naturally people want to come up better. He wanted to find a program arrangements animal shows, so to see the show the audience as much as possible.

Input Format

Line 13 positive integers n, m, k, denote the number of cats, dogs and spectators

2 ~ k + 1 of rows, each row describes a viewer voting contents.

First enter a character C or D followed by a number that viewers want to see an animal, and then is separated by a space, the next is a C or D plus a number, indicating that viewers want to see animals, on the same line will not be two or two C D.

Output Format

Output line a positive integer representing small k in the optimal arrangement can attract a lot of viewers to see the show.

Sample

Sample input

1 2 4
C1 D1
C1 D1
C1 D2
D2 C1

Sample Output

3

Data range and tips

For 25% of the data n, m≤10, k≤25; to 100% of the data, n, m≤300, k≤500.

#include<bits/stdc++.h>
using namespace std;
int n,m,question,catnum,dognum;
int a[601][601],result[20000],tot=0;
int id[601][601];
bool state[1000];
bool find(int aa)
{
    for(int j=1;j<=n;j++)
        if(a[aa][j]==1&&state[j]==0)
        {
            state[j]=1;
            if(result[j]==0||find(result[j]))
            {
                result[j]=aa;
                return 1;
            }
        }
    return 0;
}
int main()
{
    cin>>catnum>>dognum>>n;
    int x,y;
    string aa[600],bb[600];
    for(int i=1;i<=n;i++)
    {
        cin>>aa[i];
        cin>>bb[i];
    }
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
        {
            if(aa[i]==bb[j]) a[i][j]=a[j][i]=1;
         }
    for(int i=1;i<=n;i++)
    {
        memset(state,0,sizeof(state));
        if(find(i)) tot++; 
    }
    cout<<n-tot/2<<endl;
}
View Code

The system brushed up bipartite graph, that this question quite value

We will build a contradiction between man's sides, then you can seek out the smallest independent set

 

Guess you like

Origin www.cnblogs.com/znsbc-13/p/11202640.html