题意一开始是理解错的。。。结果就各种WA啦~
对于两个观众,假如有某只宠物,一个人讨厌另一个人却喜欢,这两个人就是有矛盾的,连边。
最后求最小顶点覆盖。因为把这个覆盖点集去掉的话剩下的图中没有两个点是相连的。
由于不可能有奇数环的出现,总点数减去最大匹配就是答案了。
最小顶点覆盖=总点数-最大匹配(无奇数环)
#include <cstdlib>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <fstream>
#include <iostream>
#define rep(i, l, r) for(int i=l; i<=r; i++)
#define clr(x, c) memset(x, c, sizeof(x))
#define N 567
using namespace std;
int read()
{
int x=0, f=0; char ch=getchar();
while (ch<'0' || ch>'9') { if (ch=='D') f=N; ch=getchar(); }
while (ch>='0' && ch<='9') { x=x*10+ch-'0'; ch=getchar(); }
return x+f;
}
struct edge{int y, n;} e[N*N*2]; int fir[N], en;
struct node{int x, y;} p[N];
int n, k[N], ans;
bool b[N];
void Add(int x, int y)
{
en++, e[en].y=y, e[en].n=fir[x], fir[x]=en;
en++, e[en].y=x, e[en].n=fir[y], fir[y]=en;
}
bool Find(int x)
{
int o=fir[x], y=e[o].y;
while (o)
{
if (!b[y])
{
b[y]=1; if (!k[y] || Find(k[y])) { k[y]=x; return true; }
}
o=e[o].n, y=e[o].y;
}
return false;
}
int main()
{
int t=read();
while (t--)
{
rep(i, 1, n) fir[i]=0; en=0;
rep(i, 1, n) k[i]=0; ans=0;
n=read(); n=read(); n=read();
rep(i, 1, n) { p[i].x=read(), p[i].y=read(); }
rep(i, 1, n) rep(j, i+1, n) if ((p[i].x==p[j].y || p[i].y==p[j].x)) Add(i, j);
rep(i, 1, n) if (p[i].x<N)
{
rep(j, 1, n) b[j]=0;
if (Find(i)) ans++;
}
printf("%d\n", n-ans);
}
}