P1654: [Usaco2006 Jan]The Cow Prom 奶牛舞會

來源:互聯網
上載者:User

標籤:

裸的強連通

 1 const maxe=50001; 2 type 3   node=record 4     f,t:longint; 5   end; 6 var n,m,dgr,i,u,v,num,ans:longint; 7 bfsdgr,low,head,f:array[0..maxe] of longint; 8 b:array[0..maxe] of node; 9 p:array[0..maxe] of boolean;10 procedure insert(u,v:longint);11 begin12   with b[i] do13     begin14       f:=head[u];15       t:=v;16     end;17   head[u]:=i;18 end;19 procedure tarjan(x:longint);20 var e,v:longint;21 begin22   inc(dgr);23   e:=head[x]; if e=0 then exit;24   inc(num);25   bfsdgr[x]:=dgr; low[x]:=dgr;26   f[num]:=x; p[x]:=true;27   //28   while e<>0 do29     begin30       v:=b[e].t;31       if bfsdgr[v]=0 then32         begin33           tarjan(v);34           if low[v]<low[x] then low[x]:=low[v];35         end36       else if (p[v]) and (bfsdgr[v]<low[x]) then37         low[x]:=bfsdgr[v];38       e:=b[e].f;39     end;40   if bfsdgr[x]=low[x] then41     begin42       inc(ans);43       //writeln(x);44       //repeat45       while f[num]<>x do46         begin47           p[f[num]]:=false;48           dec(num);49         end;50       //until f[num]=x;51     end;52 end;53 begin54   readln(n,m);55   for i:=1 to m do56     begin57       readln(u,v);58       insert(u,v);59     end;60   fillchar(bfsdgr,sizeof(bfsdgr),0);61   fillchar(p,sizeof(p),false);62   for i:=1 to n do63     if bfsdgr[i]=0 then tarjan(i);64   writeln(ans);65 end.
View Code

 (轉載請註明出處:http://www.cnblogs.com/Kalenda/)

P1654: [Usaco2006 Jan]The Cow Prom 奶牛舞會

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.