信与信封问题 SSL_1331(最大匹配)

来源:互联网 发布:php socket可以做什么 编辑:程序博客网 时间:2024/06/05 00:21

Description

  John先生晚上写了n封信,并相应地写了n个信封将信装好,准备寄出。但是,第二天John的儿子Small John将这n封信都拿出了信封。不幸的是,Small John无法将拿出的信正确地装回信封中了。
  将Small John所提供的n封信依次编号为1,2,…,n;且n个信封也依次编号为1,2,…,n。假定Small John能提供一组信息:第i封信肯定不是装在信封j中。请编程帮助Small John,尽可能多地将信正确地装回信封。

Input

第一行是一个整数n(n≤100)。信和信封依次编号为1,2,…,n。
接下来的各行中每行有2个数i和j,表示第i封信肯定不是装在第j个信封中。文件最后一行是2个0,表示结束。

Output

输出的各行中每行有2个数i和j,表示第i封信肯定是装在第j个信封中。请按信的编号i从小到大顺序输出。若不能确定正确装入信封的任何信件,则输出“none”。

Sample Input

3
1 2
1 3
2 1
0 0
Sample Output

1 1

var link:array[0..101] of longint; cover:array[0..101] of boolean; map:array[0..101,0..101] of boolean; i,j,k,n,m,x,y:longint; ans:boolean;function find(i:longint):boolean; var k,q:longint;begin find:=true; for k:=1 to n do  if (map[i,k]) and (not(cover[k]))   then begin         q:=link[k];         link[k]:=i;         cover[k]:=true;         if (q=0)or (find(q)) then exit;         link[k]:=q;        end; exit(false);end;begin readln(n); x:=1; y:=1; fillchar(map,sizeof(map),true); while (x<>0) and (y<>0) do  begin   readln(x,y);   map[y,x]:=false;  end;  for i:=1 to n do  begin   fillchar(cover,sizeof(cover),false);   find(i);  end; for i:=1 to n do  begin   y:=link[i];   link[i]:=0;   map[y,i]:=false;   fillchar(cover,sizeof(cover),false);   if not(find(y)) then    begin     link[i]:=y;     writeln(i,' ',y);     ans:=true;    end;  map[y,i]:=true;  end; if ans=false then writeln('none');end.