[POJ1469 COURSES]

[题目来源]Southeastern Europe 2000

[关键字]:图论

[题目大意]:让每个学生代表一门课程,且每个课程只有一个学生代表行不行?

//=====================================================================================================

[分析]:如果二分图最大匹配达到了学生数则可行否则不可行

[代码]:

View Code
 1 program Project1;
2 var
3 n, p, tc: longint;
4 map: array[0..1000,0..1000] of boolean;
5 b: array[0..1000] of boolean;
6 link: array[0..1000] of longint;
7
8 procedure init;
9 var
10 i, j, t, y: longint;
11 begin
12 readln(p,n);
13 fillchar(map,sizeof(map),false);
14 for i := 1 to p do
15 begin
16 read(t);
17 for j := 1 to t do
18 begin
19 read(y);
20 map[i,p+y] := true;
21 map[p+y,i] := true;
22 //writeln(i,'',y);
23 end;
24 end;
25 { for i := 1 to n+p do
26 for j := 1 to n+p do writeln(i,' ',j,' ',map[i,j]); }
27 end;
28
29 function dfs(k: longint):boolean;
30 var
31 i: longint;
32 begin
33 for i := 1 to n+p do
34 if (map[k,i]) and (not b[i]) then
35 begin
36 b[i] := true;
37 if (link[i] = 0) or (dfs(link[i])) then
38 begin
39 link[i] := k;
40 exit(true);
41 end;
42 end;
43 exit(false);
44 end;
45
46 procedure work;
47 var
48 i, max: longint;
49 begin
50 fillchar(link,sizeof(link),0);
51 max := 0;
52 for i := 1 to p do
53 begin
54 fillchar(b,sizeof(b),false);
55 if dfs(i) then inc(max);
56 end;
57 // writeln(max);
58 // for i := 1 to n do write(link[i],'');
59 if max = p then writeln('YES') else writeln('NO');
60 end;
61
62 begin
63 readln(tc);
64 while tc > 0 do
65 begin
66 init;
67 work;
68 dec(tc);
69 end;
70 end.



你可能感兴趣的:(poj)