CSA:Flipping Matrix(二分图匹配 & 思维)
2018-03-02 12:23
239 查看
Flipping Matrix
Time limit: 1000 msMemory limit: 256 MB
You are given a binary matrix AA of size N \times NN×N. You are allowed to perform the following two operations:Take two rows and swap them. If we want to swap rows xx and yy, we'll encode this operation as
R x y.
Take two columns and swap them. If we want to swap columns xx and yy, we'll encode this operation as
C x y.
Is it possible to obtain only values of 11 on the main diagonal of AA by performing a sequence of at most NN operations? If so, print the required operations.
Standard input
The first line contains NN.The next NN lines contain NN binary values separated by spaces, representing AA.Standard output
If there is no solution, print -1−1. Otherwise, print every operation on a separated line.Constraints and notes
2 \leq N \leq 10^32≤N≤1030 \leq A_{i, j} \leq 10≤Ai,j≤1 for every 1 \leq i, j \leq N1≤i,j≤N
Input | Output |
---|---|
3 0 0 1 0 1 0 1 0 0 | C 1 3 |
4 1 1 0 0 0 1 0 1 1 1 0 0 0 0 0 1 | -1 |
5 0 1 0 0 1 0 0 1 0 0 0 1 0 0 0 0 0 1 1 0 1 0 0 0 0 | R 1 5 C 2 3 |
using namespace std;
const int maxn = 1e3+3;
int a[maxn][maxn];
int l[maxn], vis[maxn];
vector<int>g[maxn];
int dfs(int u)
{
for(int v:g[u])
{
if(!vis[v])
{
vis[v]=1;
if(l[v]==0 || dfs(l[v]))
{
l[v] = u;
return 1;
}
}
}
return 0;
}
int main()
{
int n, ans=0;
scanf("%d",&n);
for(int i=1; i<=n; ++i)
for(int j=1; j<=n; ++j) scanf("%d",&a[i][j]);
for(int i=1; i<=n; ++i)
for(int j=1; j<=n; ++j) if(a[i][j]) g[i].push_back(j);
for(int i=1; i<=n; ++i)
{
memset(vis, 0, sizeof(vis));
if(dfs(i)) ++ans;
}
if(ans < n) puts("-1");
else
{
memset(vis, 0, sizeof(vis));
for(int i=1; i<=n; ++i)
{
if(vis[i]) continue;
int j=i;
while(l[j]!=i)
{
vis[l[j]]=1;
printf("C %d %d\n",i,l[j]);
j=l[j];
}
}
}
return 0;
}
相关文章推荐
- HDU4920:Matrix multiplication(思维 & bitset)
- HDU2686-Matrix & HDU3376-Matrix Again(费用流)
- Gym - 101490D-思维&好题&思考&递推-Bridge Automation
- poj 3422 Kaka's Matrix Travels(最小费用最大流)
- Codeforces Round #435 (Div. 2)-异或规律&思维-Mahmoud and Ehab and the xor
- hdu 5015 233 Matrix 2014 ACM/ICPC Asia Regional Xi'an Online 矩阵快速幂
- EOJ月赛:唐纳德和他的数学老师(二分图匹配 & 最大流)
- CF632D:Longest Subsequence(思维 & 数论)
- DisplayMatrix.density & DisplayMatrix.densityDpi & pixel
- 【codeforces486B】OR in Matrix(思维)
- POJ 3233 && NYOJ 298 Matrix Power Series(矩阵快速幂)
- hdoj Matrix 5671 (矩阵模拟&技巧)
- hdoj--5671--Matrix(思维)
- 我曾经的"愤青"思维
- android matrix&colormatrix 使用
- hdu KK's Steel【思维】【斐波那契数列应用】
- AtCoder:pushpush(STL & 思维)
- 提示"illegal reference to data member'CPMAgentManageDlg::m_matrixMatrixSt'in a static member function"
- Learning about the Matrix & Vector
- hdu6178-搜索&思维&读入挂-Monkeys