pku2239 Selecting Courses
看到这个标题后我毅然交上了CTSC的树形DP,WA的结果不言而喻,囧
仔细读题后有两种思路:
1.把每个课程拆成两个点,对于时间发生矛盾的课之间
(i‘-->j)
(i-->j')
下面应该是一个最大独立集问题了,点数减匹配数即可
这个思路没有实践,如果有错,请指出。
2.很多人都用这种方法,二分图一边是84个时间点,另一边是课程,连边之后求最大匹配即可
View Code
1 program pku2239(input,output);
2 var
3 f : array[0..100,0..301] of boolean;
4 lk : array[0..301] of longint;
5 v : array[0..301] of boolean;
6 n : longint;
7 procedure init;
8 var
9 i,j,x,y,z : longint;
10 begin
11 fillchar(f,sizeof(f),false);
12 fillchar(lk,sizeof(lk),0);
13 readln(n);
14 for i:=1 to n do
15 begin
16 read(x);
17 for j:=1 to x do
18 begin
19 read(y,z);
20 f[(y-1)*12+z,i]:=true;
21 end;
22 readln;
23 end;
24 end; { init }
25 function find(now :longint ):boolean;
26 var
27 i : longint;
28 begin
29 for i:=1 to n do
30 if (f[now,i])and(not v[i]) then
31 begin
32 v[i]:=true;
33 if (lk[i]=0)or(find(lk[i])) then
34 begin
35 lk[i]:=now;
36 exit(true);
37 end;
38 end;
39 exit(false);
40 end; { find }
41 function main():longint;
42 var
43 i : longint;
44 begin
45 main:=0;
46 for i:=1 to 12*7 do
47 begin
48 fillchar(v,sizeof(v),false);
49 if find(i) then
50 inc(main);
51 end;
52 end; { main }
53 begin
54 while not eof do
55 begin
56 init;
57 writeln(main);
58 end;
59 end.