ADS - Quảng cáo

Tác giả: RR

Ngôn ngữ: Pascal

{$R+,Q+}
uses math;
const
  FINP='';
  FOUT='';
  MAXN=2001;
var
  f1,f2:text;
  n,m:longint;
  queue,deg,xet:array[1..MAXN] of longint;
  a:array[1..MAXN,1..MAXN] of longint;
procedure openF;
begin
  assign(f1,FINP); reset(f1);
  assign(f2,FOUT); rewrite(f2);
end;
procedure closeF;
begin
  close(f1); close(f2);
end;
procedure inp;
var
  i,u,v:longint;
begin
  read(f1,n,m);
  for i:=1 to m do
    begin
      read(f1,u,v);
      inc(deg[u]); a[u,deg[u]]:=v;
      inc(deg[v]); a[v,deg[v]]:=u;
    end;
end;
procedure bfs(u:longint);
var
  i,v,first,last:longint;
begin
  first:=1; last:=1; queue[1]:=u; xet[u]:=1;
  while first<=last do
    begin
      u:=queue[first]; inc(first);
      for i:=1 to deg[u] do
        begin
          v:=a[u,i];
          if xet[v]=0 then
            begin xet[v]:=1; inc(last); queue[last]:=v; end;
        end;
    end;
end;
procedure solve;
var
  count,i:longint;
begin
  count:=m;
  for i:=1 to n do
    if xet[i]=0 then
      begin
        inc(count);
        bfs(i);
      end;
  writeln(f2,count-n);
end;
begin
  openF;
  inp;
  solve;
  closeF;
end.

Download