## bzoj 1064 图论

dfs，每个点有一个值，经过一条边到另一个点之后，用原来的点值和边权

/**************************************************************
Problem: 1064
User: BLADEVIL
Language: Pascal
Result: Accepted
Time:284 ms
Memory:34596 kb
****************************************************************/

{\$M 65536000}
//By BLADEVIL
var
n, m                            :longint;
pre, other, len                 :array[0..2000020] of longint;
last                            :array[0..100010] of longint;
l                               :longint;
flag                            :array[0..100010] of boolean;
size, father, key, tmin, tmax   :array[0..100010] of longint;
g                               :array[0..2000100] of longint;
ans1, ans2                      :longint;

procedure swap(var a,b:longint);
var
c                               :longint;
begin
c:=a; a:=b; b:=c;
end;

function min(a,b:longint):longint;
begin
if a>b then min:=b else min:=a;
end;

function max(a,b:longint):longint;
begin
if a>b then max:=a else max:=b;
end;

function gcd(a,b:longint):longint;
begin
if b>a then swap(a,b);
if b=0 then exit(a) else exit(gcd(b,a mod b));
end;

procedure connect(x,y,z:longint);
begin
inc(l);
pre[l]:=last[x];
last[x]:=l;
other[l]:=y;
len[l]:=z;
end;

procedure dfs(x:longint);
var
q, p                            :longint;
begin
flag[x]:=true;
q:=last[x];
while q<>0 do
begin
p:=other[q];
if len[q]=1 then father[p]:=x;
if not flag[p] then
begin
flag[p]:=true;
size[p]:=size[x]+len[q];
key[p]:=key[x];
dfs(p);
end else
begin
inc(g[0]);
g[g[0]]:=abs(size[x]+len[q]-size[p]);
if g[g[0]]=0 then dec(g[0]);
end;
q:=pre[q];
end;
end;

procedure init;
var
i                               :longint;
x, y                            :longint;
begin
read(n,m);
for i:=1 to m do
begin
read(x,y);
connect(x,y,1);
connect(y,x,-1);
end;
for i:=1 to n do father[i]:=-1;
for i:=1 to n do if not flag[i] then dfs(i);
end;

procedure main;
var
i                               :longint;
begin
if g[0]=0 then
begin
fillchar(flag,sizeof(flag),false);
for i:=1 to n do if (father[i]=-1) and (key[i]=0) then
begin
key[i]:=i;
dfs(i);
end;
for i:=1 to n do tmax[i]:=-maxlongint;
for i:=1 to n do tmin[i]:=maxlongint;
for i:=1 to n do
begin
tmax[key[i]]:=max(tmax[key[i]],size[i]);
tmin[key[i]]:=min(tmin[key[i]],size[i]);
end;
for i:=1 to n do if tmin[i]<>maxlongint then inc(ans1,tmax[i]-tmin[i]+1);
if ans1<3 then
begin
writeln(-1,' ',-1);
exit;
end else
begin
writeln(ans1,' ',3);
exit;
end;
end else
begin
ans1:=g[1];
for i:=2 to g[0] do
ans1:=gcd(ans1,g[i]);
if ans1<3 then
begin
writeln(-1,' ',-1);
exit;
end else
begin
ans2:=0;
for i:=3 to ans1 do
if ans1 mod i=0 then
begin
ans2:=i;
break;
end;
if ans2=0 then ans2:=ans1;
writeln(ans1,' ',ans2);
end;
end;
end;

begin
init;
main;
end.

posted on 2013-12-24 23:17  BLADEVIL  阅读(273)  评论(0编辑  收藏

• 随笔 - 244
• 文章 - 0
• 评论 - 12