题解:
二分图最大独立及
每两个不能选的渐变
输出n+m-最大匹配
代码:
#include<cstdio> #include<cmath> #include<algorithm> #include<cstring> using namespace std; const int N=1005; int ne[N*N],num,fi[N],x,T,zz[N*N],n,m,f[N],match[N],a[N],b[N],cnt1,cnt2; char s1[N][N],s[N],s2[N][N],m1[N][N],m2[N][N]; void jb(int x,int y) { ne[++num]=fi[x]; fi[x]=num; zz[num]=y; } int dfs(int x) { for (int i=fi[x];i;i=ne[i]) if (!f[zz[i]]) { f[zz[i]]=1; if (!match[zz[i]]||dfs(match[zz[i]])) { match[zz[i]]=x; return 1; } } return 0; } int pd(char s1[],char s2[]) { if (strlen(s1)!=strlen(s2))return 0; for (int i=1;s1[i];i++) if (s1[i]!=s2[i])return 0; return 1; } int main() { scanf("%d",&T); while (T--) { memset(match,0,sizeof match); memset(fi,0,sizeof fi); num=cnt1=cnt2=0; scanf("%d",&n); for (int i=1;i<=n;i++) { scanf("%d%s",&x,&s); if (s[0]=='F') { a[++cnt1]=x; scanf("%s%s",&m1[cnt1],&s1[cnt1]); } else { b[++cnt2]=x; scanf("%s%s",&m2[cnt2],s2[cnt2]); } } for (int i=1;i<=cnt1;i++) for (int j=1;j<=cnt2;j++) if (abs(a[i]-b[j])<=40&&pd(m1[i],m2[j])&&!pd(s1[i],s2[j]))jb(i,j); int ans=0; for (int i=1;i<=n;i++) { memset(f,0,sizeof f); ans+=dfs(i); } printf("%d ",n+m-ans); } }