NOIP2015信息传递
2016-11-08 07:05
288 查看
Tarjan求最小生成环(注意要形成一个环而不是一个点,即size>1),这真的很模板...
var
n,x,l,time,top,t,minn:longint;
i :longint;
dfn,low,z,size :array[0..200010] of longint;
last,pre,other :array[0..200010] of longint;
vis :array[0..200010] of boolean;
function min(a,b:longint):longint;
begin
if a<b then exit(a) else exit(b);
end;
procedure connect(x,y:longint);
begin
inc(l);
pre[l]:=last[x];
last[x]:=l;
other[l]:=y;
end;
procedure dfs(x:longint);
var
p,q,cur:longint;
begin
inc(time);
low[x]:=time;
dfn[x]:=time;
vis[x]:=true;
inc(top);
z[top]:=x;
//
q:=last[x];
while (q<>0) do
begin
p:=other[q];
if (dfn[p]=0) then
begin
dfs(p);
low[x]:=min(low[x],low[p]);
end else
if vis[p] then low[x]:=min(low[x],dfn[p]);
q:=pre[q];
end;
//
if (dfn[x]=low[x]) then
begin
cur:=-1;inc(t);
while (cur<>x) do
begin
cur:=z[top];
dec(top);
vis[cur]:=false;
inc(size[t]);
end;
end;
end;
begin
read(n);
for i:=1 to n do
begin
read(x);
connect(i,x);
end;
//
for i:=1 to n do if (dfn[i]=0) then dfs(i);
minn:=maxlongint;
for i:=1 to t do
if (size[i]<minn) and (size[i]>1) then minn:=size[i];
writeln(minn);
end.
——by Eirlys
转载请注明出处=w=
var
n,x,l,time,top,t,minn:longint;
i :longint;
dfn,low,z,size :array[0..200010] of longint;
last,pre,other :array[0..200010] of longint;
vis :array[0..200010] of boolean;
function min(a,b:longint):longint;
begin
if a<b then exit(a) else exit(b);
end;
procedure connect(x,y:longint);
begin
inc(l);
pre[l]:=last[x];
last[x]:=l;
other[l]:=y;
end;
procedure dfs(x:longint);
var
p,q,cur:longint;
begin
inc(time);
low[x]:=time;
dfn[x]:=time;
vis[x]:=true;
inc(top);
z[top]:=x;
//
q:=last[x];
while (q<>0) do
begin
p:=other[q];
if (dfn[p]=0) then
begin
dfs(p);
low[x]:=min(low[x],low[p]);
end else
if vis[p] then low[x]:=min(low[x],dfn[p]);
q:=pre[q];
end;
//
if (dfn[x]=low[x]) then
begin
cur:=-1;inc(t);
while (cur<>x) do
begin
cur:=z[top];
dec(top);
vis[cur]:=false;
inc(size[t]);
end;
end;
end;
begin
read(n);
for i:=1 to n do
begin
read(x);
connect(i,x);
end;
//
for i:=1 to n do if (dfn[i]=0) then dfs(i);
minn:=maxlongint;
for i:=1 to t do
if (size[i]<minn) and (size[i]>1) then minn:=size[i];
writeln(minn);
end.
——by Eirlys
转载请注明出处=w=
相关文章推荐
- codevs 4511 信息传递(NOIP2015 day1 T2)
- 【noip 2015】信息传递
- [NOIP2015] 信息传递
- NOIP2015 提高组 day1 信息传递
- NOIP 2015 Senior 2 - 信息传递
- NOIP2015信息传递
- [NOIP2015] 提高组 洛谷P2661 信息传递
- NOIP 2015 d1t2 信息传递
- NOIP2015 提高组 day1 信息传递
- 【题解】NOIP 2015 信息传递(tarjan 强连通分量)
- 【NOIP2015】信息传递 dfs
- 【NOIP 2015 Day1 T2】信息传递(dfs || 拓扑排序 || Tarjan)
- codevs 4511 信息传递 NOIP2015 day1 T2
- 【NOIP2015】洛谷2661 信息传递
- UOJ 146 【NOIP2015】信息传递
- Noip2015 Day1 T2 信息传递(虽然是图论题但是...)
- Cpp环境【NOIP2015 D1P2】【Viijos1979】【Code[VS] 4511】【CQYZOS3198】 信息传递
- [NOIp2015提高组]信息传递
- NOIP2015提高组 信息传递
- NOIP2015提高组day1 —— 信息传递(message)