Want to see more problem-solving reports: http://blog.csdn.net/wangjian8006/article/details/7870410
Reprinted please indicate the source: http://blog.csdn.net/wangjian8006
Give you a matrix of R * C, and each column of this matrix has two grids that are white, and then you take a shot in each column, you can create any white grid in this column and ask if the number of rows in the grid you hit includes all rows. If all rows are included, output the rows in the grid that you grabbed in column 1-C, of course, this is not unique,
Output no if no
The question first shows how many groups of data are there. For each group of data, the first row represents R and C, and the next row contains two numbers, which two columns of this row are white grids?
Solution: similar to the classic binary match, a single shot in a row can eliminate the question of all rows. It is easy to think of binary match. We will point the rows in the white lattice to the column, find the maximum match and use the row to match the column to check whether the matching number is R. If it is R, all rows can be hit, then, the corresponding row of each column is output if the matching number is not r
Of course, there are special cases. If R> C, it is impossible to open C to grab the R line, so no is output directly.
/*Memory 1172KTime 47MS*/#include <iostream>using namespace std;#define MAXV 1010int r,c;bool map[MAXV][MAXV],use[MAXV];int vx[MAXV],vy[MAXV];bool dfs(int x){int i;for(i=1;i<=c;i++){if(map[x][i] && !use[i]){use[i]=1;if(vy[i]==-1 || dfs(vy[i])){vy[i]=x;vx[x]=i;return true;}}}return false;}int hungary(){int i,num=0;memset(vx,-1,sizeof(vx));memset(vy,-1,sizeof(vy));for(i=1;i<=r;i++){if(vx[i]==-1){memset(use,0,sizeof(use));if(dfs(i)) num++;}}return num;}int main(){int i,a,b,Case,j;scanf("%d",&Case);while(Case--){scanf("%d%d",&r,&c);memset(map,0,sizeof(map));for(i=1;i<=c;i++){scanf("%d%d",&a,&b);map[a][i]=1;map[b][i]=1;}if(c<r || hungary()!=r){printf("NO\n");}else{for(j = 1;j <= c;j++)if(vy[j] != -1) printf("%d ",vy[j]); else for(i=1;i<=r;i++) if(map[i][j]){ printf("%d ",i); break;}printf("\n");}}return 0;}