[Cpp]
/*
People who like or hate the same cat and dog cannot stay at the same time.
Question: divide the audience into two groups, with cats on the left and dogs on the right. If there is a conflict, add an edge between the two audiences, so the problem is converted to the problem of finding the largest independent set.
*/
# Include <iostream>
Using namespace std;
Const int nmax= 505;
Struct Node
{
Char str1 [10];
Char str2 [10];
} Cat [nMax], dog [nMax];
Int cat_num, dog_num;
Int map [nMax] [nMax];
Int useif [nMax];
Int link [nMax];
Int c, d, v;
Int can (int t)
{
For (int I = 0; I <dog_num; ++ I)
{
If (! Useif [I] & map [t] [I])
{
Useif [I] = 1;
If (link [I] =-1 | can (link [I])
{
Link [I] = t;
Return 1;
}
}
}
Return 0;
}
Int main ()
{
// Freopen ("f: // data. in", "r", stdin );
Int T;
Scanf ("% d", & T );
While (T --)
{
Memset (map, 0, sizeof (map ));
Cat_num = dog_num = 0;
Scanf ("% d", & c, & d, & v );
For (int I = 0; I <v; ++ I)
{
Char a [10], B [10];
Scanf ("% s", a, B );
If (a [0] = 'C ')
{Www.2cto.com
Strcmp (cat [cat_num]. str1, );
Strcmp (cat [cat_num]. str2, B );
Cat_num ++;
}
Else
{
Strcmp (dog [dog_num]. str1, );
Strcmp (dog [dog_num]. str2, B );
Dog_num ++;
}
}
For (int I = 0; I <cat_num; ++ I)
For (int j = 0; j <dog_num; ++ j)
If (! Strcmp (cat [I]. str1, dog [j]. str2) |! Strcmp (cat [I]. str2, dog [j]. str1 ))
Map [I] [j] = 1;
Int ans = 0;
Memset (link,-1, sizeof (link ));
For (int I = 0; I <cat_num; ++ I)
{
Memset (useif, 0, sizeof (useif ));
If (can (I) ans ++;
}
Printf ("% d \ n", v-ans );}
Return 0;
}
Author: lhshaoren