spoj#WPC5G. The dilemma of Idli

The dilemma of Idli

It is emergency time! There has been a clash between the organizers of the Galactical Wars and the Inter-Galactic Parliament due to some feud. The entire civilization has been split into two groups A and B. The civilians in A love some particular members in the Organization Committee of the Galactical Wars, and hate some members from the Inter-Galactic Parliament. On the other hand, civilians in B love some particular members from the Inter-Galactic Parliament, and hate some in the Organization Committee.

You are Idli, the Inter-Galactic Dean, and it is your job to ensure satisfaction of the civilians. You decide to do so by impeaching some members from the Organization Committee and some from the Parliament. A civilian is satisfied if and only if all the members he loves are not impeached, and all the members he hates are impeached.

Of course, Idli wants to satisfy most number of civilians. Help Idli in devising an algorithm to do so.

 

Input:
First line contains two space separated integers n1 and n2, denoting the number of Galactic Wars organizers (numbered 1 to n1) and the number of Members of Parliaments(MP) respectively (numbered 1 to n2).
Second line contains a single integer ’n’, denoting the number of civilians.
’n’ lines follow containing the description of the civilians in the following format:
First character contains a single character ‘A’, or ‘B’ denoting the group of the civilian.
Then there is a space.
Then some space separated integers follow ended by -1 denoting the members of same group whom he loves. Then some space separated integers follow ended by -1 denoting the members of other group which he hates. 
Output:
A single integer denoting the maximum number of civilians Idli can satisfy.
Constraints:
1 <= n1 <= 1000000
1 <= n2 <= 1000000
1 <= n <= 500
0 <= number of organizers/MP a person loves <= 50
0 <= number of organizers/MP a person hates <= 50
Example:
Input:
4 5
3
A 1 -1 5 3 -1
B 2 3 -1 2 5 -1
B -1 -1
Output:
2
Explanation: 3rd person doesn’t love or hate anyone, and thus is always satisfied. Only 1 out of the 1st 2 civilians can be satisfied. Hence Idli can satisfy at max 2 civilians.

Input:

First line contains two space separated integers n1 and n2, denoting the number of Galactic Wars organizers (numbered 1 to n1) and the number of Members of Parliaments(MP) respectively (numbered 1 to n2).

Second line contains a single integer ’n’, denoting the number of civilians.

’n’ lines follow containing the description of the civilians in the following format:

First character contains a single character ‘A’, or ‘B’ denoting the group of the civilian.

Then there is a space.

Then some space separated integers follow ended by -1 denoting the members of same group whom he loves.

Then some space separated integers follow ended by -1 denoting the members of other group which he hates. 

 

Output:

A single integer denoting the maximum number of civilians Idli can satisfy.

 

Constraints:

1 <= n1 <= 1000000

1 <= n2 <= 1000000

1 <= n <= 500

0 <= number of organizers/MPs a person loves <= 50

0 <= number of organizers/MPs a person hates <= 50

 

Time Limit: 1 second.

 

Example:

Input:

5 5

3

A 1 -1 5 3 -1

B 2 3 -1 2 5 -1

B -1 -1

 

Output:

2

 

Explanation: 3rd person doesn’t love or hate anyone, and thus is always satisfied. Only 1 out of the 1st 2 civilians can be satisfied. Hence Idli can satisfy at max 2 civilians.