题目链接  http://acm.hdu.edu.cn/showproblem.php?pid=2819

题目大意很明确,交换图的某些行或者是某些列(可以都换),使得这个N*N的图对角线上全部都是1.

这里有一点需要说明,就是说题目的交换,其实是将原来图的某一行移到最后图的某一行,而不是指先交换两行,得到一个新图,再交换新图的两行。感觉这里比较坑。

这里先说明的一点就是,如果通过交换某些行没有办法的到解的话,那么只交换列 或者 既交换行又交换列 那也没办法得到解。其实个人感觉这个可以用矩阵的秩来解释,所有的对角线都是1,所以也就是矩阵的秩就是N,所以秩小于N就无解。另外,根据矩阵的性质,任意交换矩阵的两行  或者  两列,矩阵的秩不变,也就保证了如果通过 只交换行  或  只交换列 无法得到解的话,那么其他交换形式也必然无解。

既然说是用二分图的最大匹配,那怎么构建二分图呢,我们构建的二分图,第一部分X表示的是横坐标,第二部分Y表示纵坐标,所以范围都是1~N,然后如果a[i][j]是1,那我们就从X的i向Y的j引一条边,那么这条边的含义就可以解释为可以将Y的第j列(因为Y表示的是列的集合)移到第i列,使得a[i][i]变成1,这样就相当于是第i行第i列就变成了1,也就是说对角线多了一个1。

因此我们求这个二分图的最大匹配(目的是为了让每一列只与X中的某一行匹配),这样来就形成了N条边,那我们只需要将所有匹配的边的右边(列)  和  左边(行)所在的列  交换,这样一来对角线上这一行就成了1.

上面也也正好提示了如果最大匹配是N,那就存在解,否则无解。

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABJMAAAA6CAIAAACLTY/mAAAZzUlEQVR4nO1dS27cxtrlOrQMdS8i3oMHsnsF9tBA5pHsnsaAvYdfyr1qeBB4LiATObKjXw3YgyCABh5oEjff5B0UWaw3q9ikWGSfgw+JxSaLxY+nvqpTLwblQaIoiizLkiSJoigMwzAM4zhO0zTP86IGezI5PmKGgVkCPAR8AHgI+ADwEPAB4CHgAww8DMbO2zjI8zyO46hGkiRZlpGyJ5dMABgI4CHgA8BDwAeAh4APAA8BH2DgYXD09ArGGimWo2cD5oOR8jN6NmCwR7AwDKMoGj0bnY1WeKPnxDeD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xY62fSstLHto8HlclaupK6Zz5DrwpGjn8ti6qXV5Y6HzONz163SmHxQg8lAkhSTEoN69gnhtpuKr1HAov42EP6KLcivrTbTVOLqIoDMM4jv/7vJn0cMk85N2bZRCsLvMGtRfYdMQK8FL2lDDBQohA0tERoJoC0rJliPqh2hYkN9DtRGLnE41L2wWw9kn1ORfqCe3b1CzfZy9X3f3kIqq+7cZczVKIsC5X8NAEr0qsj9iuFd9wE3aD5Dbul49sxGmW4vTLlXg75VcBqmvZc5bletjZknTHKnaL4VCaDT9PHrKL02qrx9Y0X+IWLqnV3e+/6j/b7Qc0e4rowp1RuYnxq4cWo788NNWJ3EniWJDoG+U5s2tsO7YH1DW1Ze08gPdG5eHgTwf0AmFUzUm5yZcr4W887AMdlRvJaMZ/NiGqd8mkpykvtH9IH1sqgDcAD2cNaZ2brwAPAR8AHgI+ADwEXGE5W9I8kVLAvHm4l3Jjt9Skw5HsRFKUK6CUejXkg52TBQ/ni/6VG3gI+ADwEPAB4CHgA4qioGKMUkK5BM412RnzsNM6txpFUbAfFKcTScf6rDjgP4YoLeDhHDHsmBt4CPgA8BDwAeAh4APAQ0vsq9yInI2iiOzBQiB/8w44ZBT11OGBOjnAQ8AG4CHgA8BDwAeAh4APAA87YC/lVjJfsiMeIdtoRlE0XY8A/YIWG7aHo/fhafAQMAM8BHwAeAj4APAQ8AHgYTfsq9xKZvMW+gGEOI6nOwoJ9AUyz5h+5ZDt4RhiYjF4CCgBHgI+ADwEfAB4CPgA8HAfdPoSt3SE3byF7NwyxTV/QI8o6u9jkI9pkK4OSoxelkHLR8BDQAB4CPgA8BDwAeAh4APAwz3RXbmxz0yGO+M4juPYvOEmcAgomG/Sk6JCOzn6Gp4GD4FWgIeADwAPAR8AHgI+ADzcH/8DOLyxnsEHz70AAAAASUVORK5CYII=" alt="" width="688" height="34" />

 #include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define eps 1e-15
#define MAXN 105
#define INF 1000000007
#define MAX(a,b) (a > b ? a : b)
#define MIN(a,b) (a < b ? a : b)
#define mem(a) memset(a,0,sizeof(a)) bool G[MAXN][MAXN],vis[MAXN];
int Left[MAXN],N,M,T,a[MAXN],b[MAXN]; bool DFS(int u)
{
for(int v=;v<=N;v++) if(G[u][v] && !vis[v])
{
vis[v] = true;
if(!Left[v] || DFS(Left[v]))
{
Left[v] = u;
return true;
}
}
return false;
} int main()
{
while(~scanf("%d", &N))
{
mem(G); mem(Left);
int x,ans = ;
for(int i=;i<=N;i++) for(int j=;j<=N;j++)
{
scanf("%d", &x);
if(x)G[i][j] = true;
}
for(int i=;i<=N;i++)//求最大匹配
{
mem(vis);
if(DFS(i)) ans ++;
}
if(ans < N){printf("-1\n");continue;}//小于N无解
int tot = ,j;
for(int i=;i<=N;i++)
{
for(j=;j<=N && Left[j]!=i ;j++);
if(i != j)//交换第i列和第j列
{
a[tot] = i; b[tot] = j; tot ++;//记录结果
int t = Left[i]; Left[i] = Left[j]; Left[j] = t;
}
}
printf("%d\n",tot);
for(int i=;i<tot;i++) printf("C %d %d\n", a[i],b[i]);
}
return ;
}
04-26 15:07