Wikioi 1222 信与信封问题(二分图匹配)
1222 信與信封問題
時間限制: 1 s
空間限制: 128000 KB
題目等級 : 鉆石 Diamond
題解
題目描述 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 Description
n文件的第一行是一個整數n(n≤100)。信和信封依次編號為1,2,…,n。
n接下來的各行中每行有2個數i和j,表示第i封信肯定不是裝在第j個信封中。文件最后一行是2個0,表示結束。
輸出描述 Output Description
輸出文件的各行中每行有2個數i和j,表示第i封信肯定是裝在第j個信封中。請按信的編號i從小到大順序輸出。若不能確定正確裝入信封的任何信件,則輸出“none”。
樣例輸入 Sample Input
3
1 2
1 3
2 1
0 0
樣例輸出 Sample Output
1 1
program df;
var i,j,n,m,x,y,z,k,t,tt:longint;
a:array[0..500,0..500] of longint;
b:array[0..100] of boolean;
d,c,e:array[0..100] of longint;
function check(x:longint):boolean;
var i:longint;
begin
for i:=1 to n do
if (a[x,i]<>1) and (not b[i]) then
begin
b[i]:=true;
if (d[i]=0) or (check(d[i])) then
begin
d[i]:=x;
exit(true);
end;
end;
exit(false);
end;
begin
readln(n);
readln(x,y);
while (x<>0) and (y<>0) do
begin
a[x,y]:=1;
readln(x,y);
end;
t:=0;
for i:=1 to n do
begin
fillchar(b,sizeof(b),false);
if check(i) then inc(t); //先將匹配數量求出來
end;
for i:=1 to n do
for j:=1 to n do
if a[i,j]=0 then
begin
a[i,j]:=1; //假設這條邊不存在,即i與j是不能匹配
fillchar(d,sizeof(d),0);
tt:=0;
for k:=1 to n do
begin
fillchar(b,sizeof(b),false);
if check(k) then inc(tt);
end;
if tt<>t then //產生了影響,則這條邊是肯定的
begin
z:=1;
writeln(i,’ ‘,j);
end;
a[i,j]:=0;
end;
if z<>1 then writeln(‘none’);
end.
轉載于:https://www.cnblogs.com/Gxyhqzt/p/7784287.html
總結
以上是生活随笔為你收集整理的Wikioi 1222 信与信封问题(二分图匹配)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MYSQL数据库导入出错:#1046 -
- 下一篇: Windows下 VS2015编译boo