这题一看就应该知道是二分图匹配……
我记得有个类似的题有一个并查集的解法,但是我找不到了……
1 var i,n,m:longint;
2 p:array[0..1500] of longint;
3 v:array[0..1500] of boolean;
4 a:array[0..1500,1..2] of longint;
5 function find(x:longint):boolean;
6 var i,y:longint;
7 begin
8 for i:=1 to 2 do
9 begin
10 y:=a[x,i];
11 if v[y] then continue;
12 v[y]:=true;
13 if (p[y]=0) or (find(p[y])) then
14 begin
15 p[y]:=x;
16 exit(true);
17 end;
18 end;
19 exit(false);
20 end;
21 procedure main;
22 begin
23 readln(n,m);
24 for i:=1 to m do begin readln(a[i,1],a[i,2]);inc(a[i,1]);inc(a[i,2]);end;
25 for i:=1 to m do
26 begin
27 fillchar(v,sizeof(v),false);
28 if not(find(i)) then begin writeln(i-1);halt;end;
29 end;
30 writeln(m);
31 end;
32 begin
33 main;
34 end.
时间: 2024-12-16 12:37:51