poj1469 二分图匹配,匈牙利算法的模型
2012-06-26 21:56
387 查看
典型的二分图匹配,用匈牙利算法,(其实可以看作匈牙利算法的模型了)这题的数据范围值得注意。开始写的时候,把学生和课程的顺序乱了,悲剧WA~,改了之后就AC了,程序在时间和空间上都没加优化,1228K,860MS
program course;
type
node=record
x,y,next:integer;
end;
var
n,m,k,l,i,j,tot,num,c:integer;
link:array[1..300] of integer;//link[i]=0,顶点i为未盖点,link[i]=j,边j,i为匹配边
first:array[1..300] of integer;//first[i]表示与i相连的第一条边在g中的下标
g:array[1..60000] of node;//存二分图的边,g[i].next表示与g[i].x相连的下一条边的下标
used:array[1..300] of boolean;//记录第i个点有没有用过
function find(s:integer):boolean;
var
temp:integer;
begin
find:=true;
temp:=first[s];
while temp<>-1 do
begin
if used[g[temp].y] then
begin
used[g[temp].y]:=false;
if (link[g[temp].y]=0) or (find(link[g[temp].y])) then
begin
link[g[temp].y]:=s;
exit;
end;
end;
temp:=g[temp].next;
end;
find:=false;
end;
begin
readln(l);
for k:=1 to l do
begin
fillchar(g,sizeof(g),0);
readln(n,m);
tot:=0;
for i:=1 to m do
first[i]:=-1;
for i:=1 to n do
begin
read(num);
for j:=1 to num do
begin
read(c);
inc(tot);
g[tot].x:=c;
g[tot].y:=i;
g[tot].next:=first[c];
first[c]:=tot;
end;
readln;
end;
fillchar(link,sizeof(link),0);
for i:=1 to m do
begin
fillchar(used,sizeof(used),true);
find(i);
end;
i:=1;
while link[i]<>0 do inc(i);
if i<=n then writeln('NO')
else writeln('YES');
end;
end.
program course;
type
node=record
x,y,next:integer;
end;
var
n,m,k,l,i,j,tot,num,c:integer;
link:array[1..300] of integer;//link[i]=0,顶点i为未盖点,link[i]=j,边j,i为匹配边
first:array[1..300] of integer;//first[i]表示与i相连的第一条边在g中的下标
g:array[1..60000] of node;//存二分图的边,g[i].next表示与g[i].x相连的下一条边的下标
used:array[1..300] of boolean;//记录第i个点有没有用过
function find(s:integer):boolean;
var
temp:integer;
begin
find:=true;
temp:=first[s];
while temp<>-1 do
begin
if used[g[temp].y] then
begin
used[g[temp].y]:=false;
if (link[g[temp].y]=0) or (find(link[g[temp].y])) then
begin
link[g[temp].y]:=s;
exit;
end;
end;
temp:=g[temp].next;
end;
find:=false;
end;
begin
readln(l);
for k:=1 to l do
begin
fillchar(g,sizeof(g),0);
readln(n,m);
tot:=0;
for i:=1 to m do
first[i]:=-1;
for i:=1 to n do
begin
read(num);
for j:=1 to num do
begin
read(c);
inc(tot);
g[tot].x:=c;
g[tot].y:=i;
g[tot].next:=first[c];
first[c]:=tot;
end;
readln;
end;
fillchar(link,sizeof(link),0);
for i:=1 to m do
begin
fillchar(used,sizeof(used),true);
find(i);
end;
i:=1;
while link[i]<>0 do inc(i);
if i<=n then writeln('NO')
else writeln('YES');
end;
end.
相关文章推荐
- POJ 1469-COURSES(二分图匹配入门-匈牙利算法)
- poj题目1469 COURSES (二分图匹配,匈牙利算法)
- poj1469 二分图匹配(匈牙利算法)
- zoj 1140 poj 1469 COURSES(二分图匹配 匈牙利算法)
- Poj1469_匈牙利算法_最大二分匹配
- 【poj 1274 】The Perfect Stall (二分图匹配,匈牙利算法)
- poj 1469 COURSES 匈牙利算法
- POJ 3041 Asteroids 二分图匹配 匈牙利算法
- poj_1469 COURSES匈牙利算法
- 匈牙利算法 (poj1422-Air Raid,poj1469-COURSES)
- POJ2239 Selecting Courses(二分图匹配,匈牙利算法)
- POJ 1469,1274,2239,2536,2584,2446二分图的匈牙利算法(裸)
- POJ 3041 Asteroids 二分图匹配 匈牙利算法
- POJ 1325-Machine Schedule(二分图匹配-匈牙利算法)
- Poj1469_匈牙利算法_最大二分匹配
- POJ1469[COURSES] 二分图最大匹配 匈牙利算法
- 二分图匹配——定义,经典模型以及简单的匈牙利算法
- poj 1469(匈牙利算法)
- poj 1469 COURSES 二分图最大匹配 匈牙利算法
- poj 1469 二分图匹配(dfs算法)