Codevs 1222 Letter and envelope problem (the perfect match of the binary chart)

Source: Internet
Author: User

1222 Letter and Envelope questions

Title Description Description

Mr. John wrote n letters in the evening , and wrote n envelopes accordingly to install the letter, ready to send it. But the next day John 's son, John Small, took all the n letters out of the envelope. Unfortunately, Small John was unable to properly put the letter back into the envelope.

The n letters supplied by small John are numbered 1,2, ...,N,and n envelopes are numbered 1,2, ..., n. Suppose small John can provide a set of information: The letter I is definitely not in the envelope J . Please program help small John to correctly load the letter back into the envelope as much as possible.

Enter a description Input Description

The first line of the n file is an integer n(n≤). Letters and envelopes are numbered 1,2, ...,n.

n each row in the next row has 2 of the number i and J, indicating that the letter I letter is definitely not in the first J envelope. The last line of the file is 2 0, which indicates the end.

Output description Output Description

Each line in the output file has 2 numbers I and J, indicating that the letter I is bound to be in the J Envelope. Please press the letter number I from small to large sequential output. If you are unsure of any correspondence to properly load the envelope, the output is "none".

Sample input Sample Input

3

1 2

1 3

2 1

0 0

Sample output Sample Output

1 1

Ideas

The perfect Match of the binary graph.

First match to see if there is a perfect match, none is none. If there is an enumeration that deletes an edge in a perfect match, the edge is output if the point does not match after the edge is deleted, if none is none.

1#include <cstdio>2#include <cstring>3#include <queue>4#include <vector>5 #definefor (A,B,C) for (int a= (b);a< (c); a++)6 using namespacestd;7 8 Const intMAXN = -+Ten;9 Ten BOOLT[MAXN]; One intLKX[MAXN],LKY[MAXN]; A //Lkx represents the Y-node that matches the X-node lky represents the X-node that matches the Y-node . - intn,m; - intA[MAXN][MAXN]; the  - BOOLMatchintu) { -      for(intv=1; v<=n;v++) -         if(! A[U][V] &&!T[v]) { +t[v]=1; -             if(!lky[v] | |match (Lky[v])) { +Lky[v]=u, lkx[u]=v; A                 return true; at             } -         } -     return false; - } -  - intMain () { inscanf"%d",&n); -     intu,v; to      while(SCANF ("%d%d", &u,&v) = =2&& (u&&v)) +a[u][v]=1; -     intans=0; the      for(intI=1; i<=n;i++) { *memset (T,0,sizeof(T)); $         if(Match (i)) ans++;Panax Notoginseng     } -     if(ans!=n) printf ("none\n"); the     Else { +         BOOLflag=0; A          for(intI=1; i<=n;i++) { the             intv=Lkx[i]; +a[i][v]=1;  -lkx[i]=0, lky[v]=0; $memset (T,0,sizeof(T)); $             if(!match (i)) { -printf"%d%d\n", i,v); -Lkx[i]=v,lky[v]=i; flag=1;//Recovery the             } -a[i][v]=0;//RecoveryWuyi         } the         if(!flag) puts ("none\n"); -     } Wu     return 0; -}

Codevs 1222 Letter and envelope problem (the perfect match of the binary chart)

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.