Problem F
Montesco vs Capuleto
Background
Romeo and Juliet have finally decided to get married. Butpreparing the wedding party will not be so easy, as it iswell-known that their respective families --the Montesco and theCapuleto-- are bloody enemies. In this problem you will have todecide which person to invite and which person not to invite, inorder to prevent a slaugther.
The Problem
We have a list of N people who can be invited to theparty or not. For every person i, we have a list of hisenemies: E1, E2, ..., Ep. The"enemy" relationship has the following properties:
- Anti-transitive. If a is an enemyof b, and b is an enemy of c, then ais a friend of c. Also, the enemies of the friends of aare his enemies, and the friends of the friends of a arehis friends.
- Symmetrical. If a is an enemy of b,then b is an enemy of a (although it may not beindicated in his list of enemies).
One person will accept an invitation to the party if, and onlyif, he is invited, all his friends are invited and none of hisenemies is invited. You have to find the maximum number of peoplethat can be invited, so that all of them accept their invitation.
For instance, if N=5, and we know that: 1 is enemy of3, 2 is enemy of 1, and 4 is enemy of 5, then we could invite amaximum of 3 people. These people could be 2, 3 and 4, but forthis problem we only want the number of people invited.
Input
The first line of the input file contains the number Mof test cases in this file. A blank line follows this number, anda blank line is also used to separate test cases. The first lineof each test case contains an integer N, indicating thenumber of people who have to be considered. You can assume that N<=200.For each of these N people, there is a line with hislist of enemies. The first line contains the list of enemies ofperson 1, the second line contains the list of enemies of person2, and so on. Each list of enemies starts with an integer p(the number of known enemies of that person) and then, there are pintegers (the p enemies of that person). So, forexample, if a person's enemies are 5 and 7, his list of enemieswould be: "2 5 7".
Output
For each test case, the output should consist of a single linecontaining an integer, the maximum number of people who can beinvited, so that all of them accept their invitation.
SampleInput
3 5 1 3 1 1 0 1 5 0 8 2 4 5 2 1 3 0 0 0 1 3 0 1 5 3 2 2 3 1 3 1 1
SampleOutput
3 5 0
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 210;
int g[N][N];
int n;
int vis[N];
void input()
{
scanf("%d", &n);
memset(g, 0x00, sizeof(g));
for (int u = 0; u < n; u++) {
int num, v;
scanf("%d", &num);
for (int i = 0; i < num; i++) {
scanf("%d", &v);
v--;
g[u][v] = g[v][u] = 1;
}
}
}
void dfs(int u, int &a, int &b, int &ok, int val)
{
if (vis[u]) {
ok &= (vis[u] == val);
return;
} else {
vis[u] = val;
a++;
for (int v = 0; v < n; v++) {
if (g[u][v]) dfs(v, b, a, ok, 3 - val);
}
}
}
void solve()
{
memset(vis, 0, sizeof(vis));
int ans = 0;
for (int i = 0; i < n; i++) {
if (!vis[i]) {
int a = 0, b= 0, ok = 1;
dfs(i, a, b, ok, 1);
ans += ok * max(a, b);
}
}
printf("%d\n", ans);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("d:\\OJ\\uva_in.txt", "r", stdin);
#endif
int t;
scanf("%d", &t);
while (t--) {
input();
solve();
}
return 0;
}