|
Hỏi bài tìm khớp và cầu (cơ bản) 8 năm, 2 tháng trước
|
(+0)
|
Các anh chị cho em hỏi thuật toán cụ thể của bài GRAPH_ (Tìm khớp và cầu) được không ạ? Em làm mãi mà chỉ được có 4.76 điểm. Sau đây là code của em, các anh chị xem và sửa dùm em càng tốt ạ
Code: | program khopcau;
const fin='khopcau.inp';
fon='khopcau.out';
type canh=record
d,c:longint;
end;
var a:array[0..50000] of canh;
free:array[0..50000] of boolean;
tt,low,d:array[0..50000] of longint;
n,m,stt,dem,kq,i,j,k,tam,ktg,goc:longint;
procedure nhap;
var i,x,y:longint;
begin
stt:=0;
dem:=0;
kq:=0;
tam:=0;
fillchar(a,sizeof(a),0);
fillchar(free,sizeof(free),true);
fillchar(tt,sizeof(tt),0);
fillchar(low,sizeof(low),0);
read(n,m);
for i:=1 to m do
begin
read(x,y);
inc(tam);
a[tam].d:=x;
a[tam].c:=y;
end;
end;
function min(x,y:longint):longint;
begin
if x<=y then exit(x)
else exit(y);
end;
procedure dfs(u:longint);
var v:longint;
begin
inc(stt);
tt[u]:=stt;
low[u]:=tt[u];
for v:=1 to n do
for i:=1 to tam do
if (a[i].d=u) and (a[i].c=v) then
if free[v]=true then
begin
free[v]:=false;
dfs(v);
low[u]:=min(low[u],low[v]);
end
else low[u]:=min(low[u],tt[v]);
if u=goc then inc(ktg);
if tt[u]<=low[u] then
begin
if (u=goc) and (ktg>1) then inc(kq)
else
if u<>goc then inc(kq);
inc(dem);
d[dem]:=u;
end;
end;
procedure xuli;
var u:longint;
begin
for u:=1 to n do
if tt[u]=0 then
begin
goc:=u;
ktg:=0;
dfs(u);
end;
write(dem,' ',kq);
end;
BEGIN
assign(input,fin);
reset(input);
assign(output,fon);
rewrite(output);
nhap;
xuli;
close(input);
close(output);
END.
|
|
|