[題目大意]
[分析題解]
[個人**]
view code
1 //10180085 perseawe 2186 accepted 1424k 79ms pascal 2645b 2012-05-10
19:54:27
23 var
4 n,m,tot,numofblocks,time,top:longint;
5 dfn,low,hv,faim,stack:array [0..10000+100] of longint;
6 vis,instack:array [0..10000+100] of boolean;
7 edge:array [0..50000+500] of record wh,next:longint;end;
89 procedure addedge(u,v:longint);
10begin
11 inc(tot);
12 edge[tot].next:=hv[u];hv[u]:=tot;
13 edge[tot].wh:=v;
14end;
1516 procedure init;
17var
18 i,u,v:longint;
19begin
20 readln(n,m);
21 tot:=0;fillchar(hv,sizeof(hv),0);
22for i:=1
to m do
23begin
24 readln(u,v);
25 addedge(u,v);
26end;
27end;
2829 function min(a,b:longint):longint;begin
if athen exit(a);exit(b);end;
3031 procedure tarjan(u:longint);
32var
33 v,tnode:longint;
34begin
35 inc(time);
36 dfn[u]:=time;low[u]:=time;vis[u]:=true;
37 inc(top);stack[top]:=u;instack[u]:=true;
38 tnode:=hv[u];
39while tnode<>0
do40
begin
41 v:=edge[tnode].wh;
42if
not(vis[v]) then//樹枝邊,min取low
43begin
44 tarjan(v);
45 low[u]:=min(low[u],low[v]);
46end
47else
48if instack[u] then low[u]:=min(low[u],dfn[v]);//後向弧,low和dfn
49 tnode:=edge[tnode].next;
50end;
51if dfn[u]=low[u] then//找到乙個分支,下面是彈棧+標記的過程
52begin
53 inc(numofblocks);
54 repeat
55 v:=stack[top];
56 faim[v]:=numofblocks;
57 instack[v]:=false;
58 dec(top);
59 until u=v;
60end;
61end;
6263 procedure main;
64var i:longint;
6566
begin
67 fillchar(vis,sizeof(vis),false);
68 fillchar(instack,sizeof(instack),false);
69 fillchar(dfn,sizeof(dfn),0);
7071 top:=0;numofblocks:=0;
72 for i:=1
to n do
73if not(vis[i]) then
74 tarjan(i);
7576 for i:=1
to n do writeln(i,'
aim to
',faim[i]);
7778
79end;
8081 procedure print;
82var
83 i,tmp,ans,u,v,tnode:longint;
84begin
85 fillchar(dfn,sizeof(dfn),0);
86 for i:=1
to n do
87begin
88 tnode:=hv[i];u:=faim[i];
89 while tnode<>0
do90
begin
91 v:=faim[edge[tnode].wh];
92if u<>v then inc(dfn[u]);//如果不在乙個強連通分治中
93 tnode:=edge[tnode].next;
94end;
95end;
96 tmp:=0;
97 for i:=1
to numofblocks do
98if dfn[i]=0
then
begin inc(tmp);ans:=i;end;
99if tmp>1
then
begin writeln(0);halt;end;
100 tmp:=ans;ans:=0;
101 for i:=1
to n do
if faim[i]=tmp then inc(ans);
102 writeln(ans);
103end;
104105 begin
106107 assign(input,'
p2186.in
');reset(input);
108 //assign(output,'
p2186.out
');rewrite(output);
109110
111 init;
112 main;
113 print;
114115
116 close(input);close(output);
117118 end.
[相關鏈結]
[啟發總結]
挺厲害的,1a!
POJ 2186 強連通分量
比較簡單吧。套模板。gabow include cstdlib include cctype include cstring include cstdio include cmath include algorithm include vector include string include io...
poj2186強連通分量
這題。做了半天,倒是讓我更加了解了強連通分量,是我的第二道強連通。石建讓我做一下這道題,我用模板再變形,改了很久之後,能給出的樣例都過了。discussion上的樣例都過了,倒是上面n多人說資料沒過ac了。你讓我們這資料過了沒ac的生活如何自理。先放著,哪天有思路再來搞。重新做了一遍,發現之前的思路...
poj2186 強連通分量 縮點
題意 如果a仰慕b,b仰慕c那麼a仰慕c,1 n同牛中求有多少頭牛被所有的牛仰慕。分析 若存在多個牛同時被所有的牛仰慕,那麼這些牛可以構成乙個強連通分量,那麼就可以把所有的強連通分量壓縮成乙個點,這樣這些點中只有處於葉子節點的強連通分量才有可能被所有的店點經過,而且是只有乙個葉子節點,這樣在求強連通...