Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type
- triplet = record
- u, pr, v: integer;
- end;
- var
- list, head, next, a, b, vv: array [1..2500000] of integer;
- joo: array [0..9000000] of triplet;
- i, h, f, m, n, u, uu, ish, beg, en: integer;
- procedure push(a, b: integer);
- begin
- inc(h);
- list[h] := b;
- next[h] := head[a];
- head[a] := h;
- end;
- procedure push1(u, pr, v: integer);
- begin
- en := (en + 1) mod 9000000;
- joo[en].u := u;
- joo[en].pr := pr;
- joo[en].v := v;
- end;
- function min(a, b: integer): integer;
- begin
- if a < b then min := a
- else min := b;
- end;
- procedure bfs();
- var
- u, pr, v: integer;
- begin
- while (beg <> en) do
- begin
- beg := (beg + 1) mod 9000000;
- u := joo[beg].u;
- pr := joo[beg].pr;
- v := joo[beg].v;
- b[u] := pr;
- vv[u] := v;
- u := head[u];
- while (u <> 0) and ((v + 1) * 2 + 1 < f) do
- begin
- if (b[list[u]] <> pr) and (b[list[u]] <> 0) then f := min(f, v + 1 + vv[list[u]])
- else if (b[list[u]] = 0) then push1(list[u], pr, v + 1);
- u := next[u];
- end;
- end;
- end;
- begin
- readln(n, m);
- h := 0;
- beg := 0;
- en := 0;
- for i := 1 to n do read(a[i]);
- for i := 1 to m do
- begin
- readln(u, uu);
- push(u, uu);
- push(uu, u);
- end;
- f := m + 2;
- for ish := 1 to n do
- if (a[ish] <> 0) then
- push1(ish, a[ish], 0);
- bfs;
- writeln(f);
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement