The main topic: In a nxn box, some locations have cars, to give each car color, so that the same row and the same column of any two car color, to find a need for a minimum number of color coloring scheme.
Title Analysis: The minimum number of colors required is obviously possible, assuming that the minimum number of colors is K. If the position (I,J) is a car, then even a side i->j, get a two-dimensional graph, the K-match can be constructed out of the solution.
AC Code:
# include<iostream># include<cstdio># include<vector># include<cstring># include< Algorithm>using namespace std;# define REP (i,s,n) for (int i=s;i<n;++i) # define CL (b) memset (A,b,sizeof (a)) # Define CLL (A,b,n) Fill (a,a+n,b) const int N=105;int Inr[n],inc[n],vis[n],link[n],ans[n][n],n;char p[n][n];vector< Int>g[n];bool dfs (int x) {REP (I,0,g[x].size ()) {int y=g[x][i]; if (Vis[y]) continue; Vis[y]=1; if (link[y]==-1| | DFS (Link[y])) {link[y]=x; return true; }} return false;} void Match () {CL (link,-1); REP (i,0,n) {CL (vis,0); DFS (i); }}int Main () {int T; scanf ("%d", &t); while (t--) {scanf ("%d", &n); REP (I,0,n) g[i].clear (); REP (i,0,n) scanf ("%s", P[i]); CL (inr,0); CL (inc,0); Rep (I,0,n) Rep (j,0,n) if (p[i][j]== ' * ') {++inr[i]; ++INC[J]; G[i].push_back (j); } int maxn=0; REP (i,0,n) Maxn=max (Maxn,max (Inr[i],inc[i])); REP (I,0,n) if (INR[I]<MAXN) {for (int. j=0;j<n&&inr[i]<maxn;++j) {while (inr[i]< MAXN&&INC[J]<MAXN) {++inr[i]; ++INC[J]; G[i].push_back (j); }}} CL (ans,0); REP (k,1,maxn+1) {match (); REP (i,0,n) {int r=link[i]; if (p[r][i]== ' * ') ans[r][i]=k; REP (J,0,g[r].size ()) if (g[r][j]==i) {g[r].erase (G[r].begin () +j); Break }}} printf ("%d\n", MAXN); Rep (I,0,n) Rep (j,0,n) printf ("%d%c", Ans[i][j], (j==n-1)? ' \ n ': '); } return 0;}
UVA-10615 Rooks (binary image matching)