procedure2012
It's not worth it to know you're not worth it!

[题目来源]:POJ1466

[关键字]:二分图最大独立集

[题目大意]:给出份信息,说明某两者之间有关系,要求求出含元素最多的集合,使其两两间无关系,输出集合元素数量。

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

[分析]:乍一看如果正面建图将没有关系的两人连边则很不好求。但若反过来想,将所有有关系的人连边,求出一个最大的集合使其中任意两点之间无边相连=最大独立集=所有点数-最大匹配,匈牙利算法。

[代码]:

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.



posted on 2011-10-18 01:02  procedure2012  阅读(252)  评论(0编辑  收藏  举报