二分图建图 判断是否是完全匹配就行
最大流也行
#include <cstdio> #include <cstring> const int MAX = 300; int a[MAX][MAX]; int Match[MAX]; bool vis[MAX]; char str[7][10] = {"XXL","XL","L","M","S","XS"}; int n,m; int get(char *s) { for(int i = 0;i < 6; i++) { if(strcmp(s,str[i]) == 0) { return i + 1; } } } bool dfs(int u) { int i; for(i = i;i <= m; i++) { if(!a[u][i]) continue; if(vis[i]) continue; vis[i] = true; if(Match[i] == -1 || dfs(Match[i])) { Match[i] = u; return true; } } return false; } int match() { int ret = 0; int i; memset(Match,-1,sizeof(Match)); for(i = 1;i <= n; i++) { memset(vis,false,sizeof(vis)); if(dfs(i)) ret++; } return ret; } int main() { int t,i,j; char s1[10]; char s2[10]; scanf("%d",&t); while(t--) { scanf("%d %d",&n,&m); memset(a,0,sizeof(a)); for(i = 1;i <= m; i++) { scanf("%s %s",s1,s2); int id1 = get(s1); int id2 = get(s2); for(j = 0;j < n/6; j++) { a[id1+6*j][i] = 1; a[id2+6*j][i] = 1; } } if(match() == m) puts("YES"); else puts("NO"); } return 0; }