HDU2819 Swap (binary match + output result)

Source: Internet
Author: User

Test instructions

Give a n*n 01 matrix and ask if it is possible to exchange rows to make

The number on the main diagonal is 1.

If you can output the exchange process, if not output-1

Ideas:

Linear algebra has been studied, and row and column exchanges do not change the rank of matrices

Three rank equal

If we can change the number to the main diagonal, it's 1.

The original matrix must be full-rank

/************************************************//author:d evil//created TIME:2016/5/10 21:49:26//*********** ************************************* */#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<vector>#include<queue>#include<Set>#include<map>#include<string>#include<cmath>#include<stdlib.h>using namespacestd;Const intn= the; Vector<int>Eg[n];intlink[n],a[n*n],b[n*N];BOOLVis[n];BOOLDfsintu) {     for(intI=0; I<eg[u].size (); i++)    {        intv=Eg[u][i]; if(!Vis[v]) {Vis[v]=1; if(link[v]==-1||DFS (Link[v])) {Link[v]=u; return 1; }        }    }    return 0;}intMain () {//freopen ("In.txt", "R", stdin);    intn,x;  while(~SCANF ("%d",&N) {memset (link),-1,sizeof(link));  for(intI=1; i<=n;i++) eg[i].clear ();  for(intI=1; i<=n;i++)        {             for(intj=1; j<=n;j++) {scanf ("%d",&x); if(x) Eg[i].push_back (j); }        }        intans=0;  for(intI=1; i<=n;i++) {memset (Vis,0,sizeof(VIS)); Ans+=DFS (i); }        if(ans<N) {printf ("-1\n"); Continue; }        intCnt=0, J;  for(intI=1; i<=n;i++)        {             for(j=1; j<=n;j++)                if(link[j]==i) Break; if(j!=i) {a[cnt]=i; B[cnt++]=J;            Swap (link[j],link[i]); }} printf ("%d\n", CNT);  for(intI=0; i<cnt;i++) printf ("C%d%d\n", A[i],b[i]); }    return 0;}

HDU2819 Swap (binary match + output result)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.