• Source
    1. #include <cstdio>
    2. #include <cstring>
    3. #include <algorithm>
    4. #include <queue>
    5. // by zrt
    6. // problem:
    7. // 无论你在什么时候开始,重要的是开始以后就不要停止。
    8. using namespace std ;
    9. typedef long long LL ;
    10. const double eps(1e-10) ;
    11. const int inf(0x3f3f3f3f) ;
    12. int tt,kase,n,m;
    13. int S,T;
    14. int H[40],P[1605],X[1605],flow[1605],tot;
    15. int zl[40];
    16. inline void add(int x,int y,int z){
    17. P[++tot]=y;X[tot]=H[x];H[x]=tot;flow[tot]=z;
    18. }
    19. queue<int> q;
    20. int d[40];
    21. bool bfs(){
    22. memset(d,0,sizeof d);
    23. d[S]=1;q.push(S);int x;
    24. while(!q.empty()){
    25. x=q.front();q.pop();
    26. for(int i=H[x];i;i=X[i]){
    27. if(flow[i]>0&&!d[P[i]]){
    28. d[P[i]]=d[x]+1;
    29. q.push(P[i]);
    30. }
    31. }
    32. }
    33. return d[T];
    34. }
    35. int dfs(int x,int a){
    36. if(x==T||a==0) return a;
    37. int f=a,tmp;
    38. for(int i=H[x];i;i=X[i]){
    39. if(flow[i]>0&&d[P[i]]==d[x]+1){
    40. tmp=dfs(P[i],min(flow[i],a));
    41. a-=tmp;
    42. flow[i]-=tmp;
    43. flow[i^1]+=tmp;
    44. if(!a) break;
    45. }
    46. }
    47. if(f==a) d[x]=-1;
    48. return f-a;
    49. }
    50. int Dinic(){
    51. int f(0);
    52. while(bfs()) f+=dfs(S,inf);
    53. return f;
    54. }
    55. int main(){
    56. #ifdef LOCAL
    57. freopen("in.txt","r",stdin) ;
    58. freopen("out.txt","w",stdout) ;
    59. #endif
    60. scanf("%d",&tt);
    61. S=0,T=39;
    62. while(tt--){
    63. tot=1;memset(H,0,sizeof H);
    64. scanf("%d%d",&n,&m);
    65. kase++;
    66. memset(zl,0,sizeof zl);
    67. int k;
    68. scanf("%d",&k);
    69. for(int i=0,x;i<k;i++){
    70. scanf("%d",&x);zl[x]++;
    71. }
    72. for(int i=1;i<=m;i++){
    73. if(zl[i]){
    74. add(S,i,zl[i]);
    75. add(i,S,0);
    76. }
    77. }
    78. for(int i=1;i<n;i++){
    79. scanf("%d",&k);
    80. memset(zl,0,sizeof zl);
    81. for(int j=0,x;j<k;j++){
    82. scanf("%d",&x);zl[x]++;
    83. }
    84. for(int j=1;j<=m;j++){
    85. if(zl[j]>1){
    86. add(i+m,j,zl[j]-1);
    87. add(j,i+m,0);
    88. }
    89. if(zl[j]==0){
    90. add(j,i+m,1);
    91. add(i+m,j,0);
    92. }
    93. }
    94.  
    95. }
    96. for(int i=1;i<=m;i++){
    97. add(i,T,1);
    98. add(T,i,0);
    99. }
    100. printf("Case #%d: %d\n",kase,Dinic());
    101. }
    102.  
    103. return 0 ;
    104. }
    105.