Wikioi 1222 letter and envelope Problems

Source: Internet
Author: User
Description Description

Mr. John wrote n letters in the evening and wrote n envelopes accordingly. The letter was packed and prepared to be sent. But the next day John's son, small John, took out all the N emails. Unfortunately, small John cannot properly load the letter he has taken back into the envelope.

 

The N messages provided by small John are numbered 1, 2 ,..., N, and N envelopes are numbered 1, 2 ,..., N. Assuming that small John can provide a set of information: the I-th letter is certainly not contained in envelope J. Please program and help small John to pack as many emails as possible correctly back into the envelope.

 

Input description Input description

The first line of the N file is an integer n (n ≤ 100 ). The numbers of the letter and the envelope are 1, 2 ,..., N.

N there are two numbers of I and j in each row in the next row, indicating that the I-th letter is definitely not contained in the J-th envelope. The last row of the file is two zeros, indicating that the end is over.

Output description Output description

There are two numbers of I and j in each row of the output file, indicating that the I-th letter is bound to the J-th envelope. Output in ascending order of the letter id I. If you are unsure of any letter that is correctly loaded into the envelope, "NONE" is output ".

Sample Input Sample Input

3

1 2

1 3

2 1

0 0

Sample output Sample output

1 1

Data range and prompt Data size & hint

Question: (excerpt)

A typical bipartite graph maximum matching Hungarian algorithm

If an edge must exist in a perfect match, the perfect match will not exist if this edge is deleted.

Code:

 1 var px,py:array[0..100] of longint; 2     map:array[0..100,0..100] of boolean; 3     i,n,op,t,x,y,ans:longint; 4     vis:array[0..100] of longint; 5     flag:boolean; 6 function find(x:longint):boolean; 7   var i:longint; 8   begin 9   for i:=1 to n do10    if map[x,i] then11     begin12      if (vis[i]=t) then continue;13      vis[i]:=t;14      if (py[i]=0) or (find(py[i])) then15       begin16        py[i]:=x;17        px[x]:=i;18        exit(true);19       end;20     end;21    exit(false);22   end;23 procedure init;24  begin25  readln(n);26  readln(x,y);27  fillchar(map,sizeof(map),true);28  while (x<>0) and (y<>0) do29   begin30   map[x,y]:=false;31   readln(x,y);32   end;33  end;34 procedure main;35  begin36  for i:=1 to n do37   begin38   t:=i;39   if (find(i)) then inc(ans);40   end;41  if ans<>n then writeln(‘none‘)42  else43   begin44    flag:=false;45    fillchar(vis,sizeof(vis),0);46    for i:=1 to n do47     begin48      t:=i;49      op:=px[i];50      map[i,op]:=false;51      py[op]:=0;px[i]:=0;52      if not(find(i)) then53       begin54        writeln(i,‘ ‘,op);55        px[i]:=op;56        py[op]:=i;57        flag:=true;58        end;59      map[i,op]:=true;60      end;61    if not(flag) then writeln(‘none‘);62   end;63  end;64 begin65  init;66  main;67 end.
View code

 

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.