bzoj 1191 匈牙利算法

只需要做一遍匈牙利,只要有一个没法匹配上就break就行了

/**************************************************************

    Problem: 1191

    User: BLADEVIL

    Language: Pascal

    Result: Accepted

    Time:12 ms

    Memory:256 kb

****************************************************************/

 

//By BLADEVIL

var

    flag                    :array[0..1010] of boolean;

    link                    :array[0..1010] of longint;

    n, m                    :longint;

    pre, other, last        :array[0..2020] of longint;

    l                       :longint;

    ans                     :longint;

     

procedure connect(x,y:longint);

begin

    inc(l);

    pre[l]:=last[x];

    last[x]:=l;

    other[l]:=y;

end;

     

procedure init;

var

    i                       :longint;

    x, y                    :longint;

begin

    read(n,m);

    for i:=1 to m do

    begin

        read(x,y);

        connect(i,x);

        connect(i,y);

    end;

end;

 

function find(x:longint):boolean;

var

    i                       :longint;

    q, p                    :longint;

begin

    q:=last[x];

    while q<>0 do

    begin

        p:=other[q];

        if not flag[p] then

        begin

            flag[p]:=true;

            if (link[p]=0) or (find(link[p])) then

            begin

                link[p]:=x;

                exit(true);

            end;

        end;

        q:=pre[q];

    end;

    exit(false);

end;

 

procedure main;

var

    i                       :longint;

begin

    ans:=0;

    for i:=1 to m do

    begin

        fillchar(flag,sizeof(flag),false);

        if find(i) then inc(ans) else break;

    end;

    writeln(ans);

end;

 

begin

    init;

    main;

end.

 

你可能感兴趣的:(ZOJ)