记录编号 33238 评测结果 AAAAAAAAAA
题目名称 游历校园 最终得分 100
用户昵称 Gravatarreamb 是否通过 通过
代码语言 Pascal 运行时间 1.587 s
提交时间 2011-11-09 20:30:06 内存使用 1.83 MiB
显示代码纯文本
program school;
var
  t,n,m,i,j,x,x1,x2,z,y:longint;
  a,l,d,p:array[1..100000]of longint;
  bz,biaozhi:array[1..100000]of boolean;
function find(v:longint):longint;
begin
  {if v=l[v] then
  begin
    x:=v;
    exit
  end
  else
  begin
    find(l[v]);
    l[v]:=x
  end   }
if l[v]<>v then l[v]:=find(l[v]);
exit(l[v]);
end;
begin
  assign (input,'sent.in');
  reset (input);
  assign (output,'sent.out');
  rewrite (output);
    readln (n);
    readln (m);
    for i:=1 to n do
    begin
      bz[i]:=true;
      biaozhi[i]:=true
    end;
    for i:=1 to n do
      l[i]:=i;
    for i:=1 to m do
    begin
      readln (z,y);
      biaozhi[z]:=false;
      biaozhi[y]:=false;
      inc(a[z]);
      inc(a[y]);
      x1:=find(z);
      x2:=find(y);
      l[x1]:=x2
    end;
    for i:=1 to n do
    begin
      x:=find(i);
      bz[x]:=false;
      if a[i]mod 2=0 then
        inc(d[x])
      else
        inc(p[x])
    end;
    for i:=1 to n do
    begin
      if bz[i]=false then
      begin
        inc(t);
        if (p[i]<>0)and(p[i]<>2) then
          t:=t+(p[i]-2)div 2
      end
    end;
    for i:=1 to n do
      if biaozhi[i] then
        dec(t);
    t:=t-1;
    if t=0 then
      writeln ('-1')
    else
      writeln(t);
  close (input);
  close (output)
end.