Sakuzyo - ImprintingA.R.C. Markland-N is a tall building with $$$n$$$ floors numbered from $$$1$$$ to $$$n$$$. Between each two adjacent floors in the building, there is a staircase connecting them.It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.ConneR's office is at floor $$$s$$$ of the building. On each floor (including floor $$$s$$$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $$$k$$$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!

The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases in the test. Then the descriptions of $$$t$$$ test cases follow. The first line of a test case contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le s \le n$$$, $$$1 \le k \le \min(n-1, 1000)$$$) — respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants. The second line of a test case contains $$$k$$$ distinct integers $$$a_1, a_2, \ldots, a_k$$$ ($$$1 \le a_i \le n$$$) — the floor numbers of the currently closed restaurants. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$.

For each test case print a single integer — the minimum number of staircases required for ConneR to walk from the floor $$$s$$$ to a floor with an open restaurant.

NoteIn the first example test case, the nearest floor with an open restaurant would be the floor $$$4$$$.In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.In the third example test case, the closest open restaurant is on the $$$6$$$-th floor.

open Printf
open Scanf
open Int64

let (++) = Int64.add
let (--) = Int64.sub

let next_int () = Scanf.scanf " %d" (fun x -> x)
let next_long () = Scanf.scanf " %Ld" (fun x -> x)
let next_long3 () = Scanf.scanf " %Ld %Ld %Ld" (fun x y z -> (x, y, z))

let fill_list (n: int) (f: (int * 'a) -> 'b) (i: 'a): 'b list =
  let rec aux (idx: int): 'b list =
    if idx >= n then [] else (f (idx, i) :: aux (idx + 1)) in
  aux 0

let fill_arr (n: int) (f: (int * 'a) -> 'b) (i: 'a): 'b array =
  let k = Array.make n (f (0, i)) in
  for idx = 1 to n - 1 do
    k.(idx) <- f (idx, i)
  done;
  k

let () =
  let m () =
    let lmax = 6000 in
    let lmid = 3000 in
    let t = next_int () in
    for _ = 0 to t - 1 do
      let n, z, k = next_long3() in
      let arr = fill_arr (to_int k) (fun _ -> next_long()) () in
      let opened = fill_arr lmax (fun (i, ()) -> 1L <= (of_int i) ++ z -- of_int lmid && (of_int i) ++ z -- of_int lmid <= n) () in
      Array.iter (fun x ->
          let idx = (to_int (x -- z ++ of_int lmid)) in
          if 0 <= idx && idx < lmax then
            opened.(idx) <- false
          else
            ()
        ) arr;
      let k = fill_list lmid (fun (x, ()) -> x) ()
              |> List.find (fun x ->
                  let up, down = lmid + x, lmid - x in
                  let upb = if 0 <= up && up < lmax then
                      opened.(up) else
                      false in
                  let downb = if 0 <= down && down < lmax then
                      opened.(down) else
                      false in
                  upb || downb
                ) in
      printf "%d\n" k
    done;
  in
  try m()
  with e ->
    let msg = Printexc.to_string e
    and stack = Printexc.get_backtrace () in
    printf "there was an error: %s%s\n" msg stack;
    ()

open Printf
open Scanf
open Int64

let (++) = Int64.add
let (--) = Int64.sub

let next_int () = Scanf.scanf " %d" (fun x -> x)
let next_long () = Scanf.scanf " %Ld" (fun x -> x)
let next_long3 () = Scanf.scanf " %Ld %Ld %Ld" (fun x y z -> (x, y, z))

let fill_list (n: int) (f: (int * 'a) -> 'b) (i: 'a): 'b list =
  let rec aux (idx: int): 'b list =
    if idx >= n then [] else (f (idx, i) :: aux (idx + 1)) in
  aux 0

let fill_arr (n: int) (f: (int * 'a) -> 'b) (i: 'a): 'b array =
  let k = Array.make n (f (0, i)) in
  for idx = 1 to n - 1 do
    k.(idx) <- f (idx, i)
  done;
  k

let () =
  let m () =
    let t = next_int () in
    for _ = 0 to t - 1 do
      let n, z, k = next_long3() in
      let arr = fill_arr (to_int k) (fun _ -> next_long()) () in
      let opened = fill_arr 2000 (fun (i, ()) -> 1L <= (of_int i) ++ z -- 1000L && (of_int i) ++ z -- 1000L <= n) () in
      Array.iter (fun x ->
          let idx = (to_int (x -- z ++ 1000L)) in
          if 0 <= idx && idx < 2000 then
            opened.(idx) <- false
          else
            ()
        ) arr;
      let k = fill_list 1000 (fun (x, ()) -> x) ()
              |> List.find (fun x ->
                  let up, down = 1000 + x, 1000 - x in
                  let upb = if 0 <= up && up < 2000 then
                      opened.(up) else
                      false in
                  let downb = if 0 <= down && down < 2000 then
                      opened.(down) else
                      false in
                  upb || downb
                ) in
      printf "%d\n" k
    done;
  in
  try m()
  with e ->
    let msg = Printexc.to_string e
    and stack = Printexc.get_backtrace () in
    printf "there was an error: %s%s\n" msg stack;
    ()

open Printf
open Scanf

let next_int () = Scanf.scanf " %d" (fun x -> x)
let next_int3 () = Scanf.scanf " %d %d %d" (fun x y z -> (x, y, z))

let iter_list (n: int) (f: (int * 'a) -> 'b) (i: 'a): 'b list =
  let rec aux (idx: int): 'b list =
    if idx >= n then [] else (f (idx, i) :: aux (idx + 1)) in
  aux 0

let iter_arr (n: int) (f: (int * 'a) -> 'b) (i: 'a): 'b array =
  let k = Array.make n (f (0, i)) in
  for idx = 0 to n - 1 do
    k.(idx) <- f (idx, i)
  done;
  k

let () =
  let t = next_int () in
  for _ = 0 to t - 1 do
    let n, z, k = next_int3() in
    let arr = Array.make k 0 in
    for i = 0 to k - 1 do
      arr.(i) <- next_int ()
    done;
    let opened = Array.make 2000 true in
    for i = 0 to 1000 - z do
      opened.(i) <- false
    done;
    Array.iter (fun x ->
        let idx = x - z + 1000 in
        if 0 <= idx && idx < 2000 then
          opened.(idx) <- false
        else
          ()
      ) arr;
    let k = iter_list 1000 (fun (x, ()) -> x) ()
            |> List.find (fun x ->
                let up, down = 1000 + x, 1000 - x in
                let upb = if 0 <= up && up < 2000 then
                    opened.(up) else
                    false in
                let downb = if 0 <= down && down < 2000 then
                    opened.(down) else
                    false in
                upb || downb
              ) in
    printf "%d\n" k
  done;

open Printf
open Scanf
open Int64

let (++) = Int64.add
let (--) = Int64.sub

let next_int () = Scanf.scanf " %d" (fun x -> x)
let next_long () = Scanf.scanf " %Ld" (fun x -> x)
let next_long3 () = Scanf.scanf " %Ld %Ld %Ld" (fun x y z -> (x, y, z))

let fill_list (n: int) (f: (int * 'a) -> 'b) (i: 'a): 'b list =
  let rec aux (idx: int): 'b list =
    if idx >= n then [] else (f (idx, i) :: aux (idx + 1)) in
  aux 0

let fill_arr (n: int) (f: (int * 'a) -> 'b) (i: 'a): 'b array =
  let k = Array.make n (f (0, i)) in
  for idx = 1 to n - 1 do
    k.(idx) <- f (idx, i)
  done;
  k

let () =
  let m () =
    let lmax = 6000 in
    let lmid = 3000 in
    let t = next_int () in
    for _ = 0 to t - 1 do
      let n, z, k = next_long3() in
      let arr = fill_arr (to_int k) (fun _ -> next_long()) () in
      let opened = fill_arr lmax (fun (i, ()) -> 1L <= (of_int i) ++ z -- of_int lmid && (of_int i) ++ z -- of_int lmid <= n) () in
      Array.iter (fun x ->
          let idx = (to_int (x -- z ++ of_int lmid)) in
          if 0 <= idx && idx < lmax then
            opened.(idx) <- false
          else
            ()
        ) arr;
      let k = fill_list 1000 (fun (x, ()) -> x) ()
              |> List.find (fun x ->
                  let up, down = lmid + x, lmid - x in
                  let upb = if 0 <= up && up < lmax then
                      opened.(up) else
                      false in
                  let downb = if 0 <= down && down < lmax then
                      opened.(down) else
                      false in
                  upb || downb
                ) in
      printf "%d\n" k
    done;
  in
  try m()
  with e ->
    let msg = Printexc.to_string e
    and stack = Printexc.get_backtrace () in
    printf "there was an error: %s%s\n" msg stack;
    ()

open Printf
open Scanf
open Int64

let (++) = Int64.add
let (--) = Int64.sub

let next_int () = Scanf.scanf " %d" (fun x -> x)
let next_long () = Scanf.scanf " %Ld" (fun x -> x)
let next_long3 () = Scanf.scanf " %Ld %Ld %Ld" (fun x y z -> (x, y, z))

let fill_list (n: int) (f: (int * 'a) -> 'b) (i: 'a): 'b list =
  let rec aux (idx: int): 'b list =
    if idx >= n then [] else (f (idx, i) :: aux (idx + 1)) in
  aux 0

let fill_arr (n: int) (f: (int * 'a) -> 'b) (i: 'a): 'b array =
  let k = Array.make n (f (0, i)) in
  for idx = 1 to n - 1 do
    k.(idx) <- f (idx, i)
  done;
  k

let () =
  let m () =
    let t = next_int () in
    for _ = 0 to t - 1 do
      let n, z, k = next_long3() in
      let arr = fill_arr (to_int k) (fun _ -> next_long()) () in
      let opened = fill_arr 2000 (fun (i, ()) -> 1L <= (of_int i) ++ z -- 1000L && (of_int i) ++ z -- 1000L <= n) () in
      Array.iter (fun x ->
          let idx = (to_int (x -- z ++ 1000L)) in
          if 0 <= idx && idx < 2000 then
            opened.(idx) <- false
          else
            ()
        ) arr;
      let k = fill_list 1000 (fun (x, ()) -> x) ()
              |> List.find (fun x ->
                  let up, down = 1000 + x, 1000 - x in
                  let upb = if 0 <= up && up < 2000 then
                      opened.(up) else
                      false in
                  let downb = if 0 <= down && down < 2000 then
                      opened.(down) else
                      false in
                  upb || downb
                ) in
      printf "%d\n" k
    done;
  in
  try m()
  with e ->
    let msg = Printexc.to_string e
    and stack = Printexc.get_backtrace () in
    Printf.eprintf "there was an error: %s%s\n" msg stack;
    ()


Hongcow is ruler of the world. As ruler of the world, he wants to make it easier for people to travel by road within their own countries.The world can be modeled as an undirected graph with n nodes and m edges. k of the nodes are home to the governments of the k countries that make up the world.There is at most one edge connecting any two nodes and no edge connects a node to itself. Furthermore, for any two nodes corresponding to governments, there is no path between those two nodes. Any graph that satisfies all of these conditions is stable.Hongcow wants to add as many edges as possible to the graph while keeping it stable. Determine the maximum number of edges Hongcow can add.

The first line of input will contain three integers n, m and k (1 ≤ n ≤ 1 000, 0 ≤ m ≤ 100 000, 1 ≤ k ≤ n) — the number of vertices and edges in the graph, and the number of vertices that are homes of the government.  The next line of input will contain k integers c1, c2, ..., ck (1 ≤ ci ≤ n). These integers will be pairwise distinct and denote the nodes that are home to the governments in this world. The following m lines of input will contain two integers ui and vi (1 ≤ ui, vi ≤ n). This denotes an undirected edge between nodes ui and vi. It is guaranteed that the graph described by the input is stable.

Output a single integer, the maximum number of edges Hongcow can add to the graph while keeping it stable.

NoteFor the first sample test, the graph looks like this:    Vertices 1 and 3 are special. The optimal solution is to connect vertex 4 to vertices 1 and 2. This adds a total of 2 edges. We cannot add any more edges, since vertices 1 and 3 cannot have any path between them.For the second sample test, the graph looks like this:    We cannot add any more edges to this graph. Note that we are not allowed to add self-loops, and the graph must be simple.

let read_int() = Scanf.scanf " %d" (fun x -> x) in
let n = read_int()
and m = read_int()
and k = read_int()
and x = ref 0
and y = ref 0
and z = ref 0
and f = fun x -> x * (x - 1) / 2 in
let a = Array.make (n + 1) []
and g = Array.make (n + 1) false
and s = Array.make (n + 1) 0
and t = Array.make (n + 1) 0 in

begin
  for i = 1 to k do
    let u = read_int() in
    g.(u) <- true
  done;
  for i = 1 to m do
    let u = read_int()
    and v = read_int() in
    begin
      a.(u) <- v :: a.(u);
      a.(v) <- u :: a.(v)
    end
  done;

  let rec floodfill u c = match s.(u), a.(u) with
    | 0, _ -> (s.(u) <- c; floodfill u c)
    | _, [] -> ()
    | _, v :: vs ->
      begin
        a.(u) <- vs;
        floodfill u c;
        floodfill v c;
      end
  in

  for u = 1 to n do
    floodfill u u;
    t.(s.(u)) <- t.(s.(u)) + 1;
    g.(s.(u)) <- g.(s.(u)) || g.(u)
  done;

  for u = 1 to n do
    let p = t.(u) in
    if g.(u) then
      begin
        x := !x + f p;
        y := max (!y) p
      end
    else
        z := !z + p
  done;

  let r = !x - f !y + f (!y + !z) - m in
  print_int r;
end


You are given a permutation of n numbers p1, p2, ..., pn. We perform k operations of the following type: choose uniformly at random two indices l and r (l ≤ r) and reverse the order of the elements pl, pl + 1, ..., pr. Your task is to find the expected value of the number of inversions in the resulting permutation.

The first line of input contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 109). The next line contains n integers p1, p2, ..., pn — the given permutation. All pi are different and in range from 1 to n. The problem consists of three subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows.   In subproblem G1 (3 points), the constraints 1 ≤ n ≤ 6, 1 ≤ k ≤ 4 will hold.  In subproblem G2 (5 points), the constraints 1 ≤ n ≤ 30, 1 ≤ k ≤ 200 will hold.  In subproblem G3 (16 points), the constraints 1 ≤ n ≤ 100, 1 ≤ k ≤ 109 will hold.

Output the answer with absolute or relative error no more than 1e - 9.

NoteConsider the first sample test. We will randomly pick an interval of the permutation (1, 2, 3) (which has no inversions) and reverse the order of its elements. With probability , the interval will consist of a single element and the permutation will not be altered. With probability  we will inverse the first two elements' order and obtain the permutation (2, 1, 3) which has one inversion. With the same probability we might pick the interval consisting of the last two elements which will lead to the permutation (1, 3, 2) with one inversion. Finally, with probability  the randomly picked interval will contain all elements, leading to the permutation (3, 2, 1) with 3 inversions. Hence, the expected number of inversions is equal to .

let rec fold i j f init = if i>j then init else fold (i+1) j f (f i init)
let sum i j f = fold i j (fun i a -> (f i) +. a) 0.0

open Printf
open Scanf

let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)

let construct_a n =
  let nn = n*n in
  let a = Array.make_matrix nn nn 0.0 in

  (* the pair of positions (i,j) is mapped into i+j*n of the vector *)

  (* the flips in the permutation are for a range [p,q], where p <= q *)
  (* for pair (i,j) and for each flip [p,q] see what happens to (i,j) and
     add this probability to the matrix a *)

  let ind i j = i + j*n in
  let fn = float n in
  let pr = 2.0 /. (fn *. (fn +. 1.0)) in

  for i=0 to n-1 do
    for j=0 to n-1 do
      if i <> j then (
	for p=0 to n-1 do
	  for q=p to n-1 do
	    let map i = if i<p then i else if i>q then i else q - (i-p) in
	    let (i',j') = (map i, map j) in
	    a.(ind i' j').(ind i j) <- a.(ind i' j').(ind i j) +. pr
	  done
	done
      )
    done
  done;
  a

let () =
  let n = read_int () in
  let k = read_int () in
  let perm = Array.init n (fun _ -> read_int()) in
  let nn = n*n in

  let ind i j = i + j*n in
  let dot a b = sum 0 (nn-1) (fun i -> a.(i) *. b.(i)) in

  let mult a b =
    let c = Array.make nn 0.0 in

    for i=0 to nn-1 do
      for j=0 to nn-1 do
	c.(i) <- c.(i) +. a.(i).(j) *. b.(j)
      done
    done;
    c
  in

  let power a b p =
    let rec loop ac i = if i=p then ac else
	loop (mult a ac) (i+1)
    in
    loop b 0
  in

  let a = construct_a n in

  let b = Array.make nn 0.0 in
  let b' = Array.make nn 0.0 in

  for i=0 to n-2 do
    for j=i+1 to n-1 do
      let ii = ind i j in
      if perm.(i) < perm.(j)
      then b.(ii) <- 1.0
      else b'.(ii) <- 1.0
    done
  done;

  let c = power a b k in
  let c' = power a b' k in

  let s = Array.make nn 0.0 in
  let s' = Array.make nn 0.0 in

  for i=0 to n-1 do
    for j=0 to n-1 do
      let ii = ind i j in
      if i<j then s.(ii) <- 1.0 else if i>j then s'.(ii) <- 1.0
    done
  done;

  printf "%12.12f\n" ((dot c' s) +. (dot c s'))

open Printf
open Scanf

let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)

let construct_a n =
  let nn = n*n in
  let a = Array.make_matrix nn nn 0.0 in

  (* the pair of positions (i,j) is mapped into i+j*n of the vector *)

  (* the flips in the permutation are for a range [p,q], where p <= q *)
  (* for pair (i,j) and for each flip [p,q] see what happens to (i,j) and
     add this probability to the matrix a *)

  let ind i j = i + j*n in

  let fn = float n in
  let pr = 2.0 /. (fn *. (fn +. 1.0)) in

  for i=0 to n-1 do
    for j=0 to n-1 do
      if i <> j then (
	for p=0 to n-1 do
	  for q=p to n-1 do
	    let map i = if i<p then i else if i>q then i else q - (i-p) in
	    let (i',j') = (map i, map j) in
	    a.(ind i' j').(ind i j) <- a.(ind i' j').(ind i j) +. pr
	  done
	done
      )
    done
  done;
  a

let () =
  let n = read_int () in
  let k = read_int () in
  let perm = Array.init n (fun _ -> read_int()) in

  let nn = n*n in

  let mult a b =
    let c = Array.make nn 0.0 in

    for i=0 to nn-1 do
      for j=0 to nn-1 do
	c.(i) <- c.(i) +. a.(i).(j) *. b.(j)
      done
    done;
    c
  in

  let power a b p =
    let rec loop ac i = if i=p then ac else
	loop (mult a ac) (i+1)
    in
    loop b 0
  in

  let a = construct_a n in

  let ind i j = i + j*n in
  let unind ii = (ii - n*(ii/n), ii/n) in

  let exp = ref 0.0 in

  for i=0 to n-2 do
    for j=i+1 to n-1 do
      let flipprob = ref 0.0 in
      let b = Array.init nn (fun ii-> if ii = ind i j then 1.0 else 0.0) in
      let c = power a b k in
      for ii=0 to nn-1 do
	let (i',j') = unind ii in
	if i'<>j' && ((i'<j') <> (i<j)) then flipprob := !flipprob +. c.(ii)
      done;
      if perm.(i) < perm.(j) then
	exp := !exp +. !flipprob
      else
	exp := !exp +. (1.0 -. !flipprob)
    done
  done;

  printf "%12.12f\n" !exp


A new innovative ticketing systems for public transport is introduced in Bytesburg. Now there is a single travel card for all transport. To make a trip a passenger scan his card and then he is charged according to the fare.The fare is constructed in the following manner. There are three types of tickets:   a ticket for one trip costs 20 byteland rubles,  a ticket for 90 minutes costs 50 byteland rubles,  a ticket for one day (1440 minutes) costs 120 byteland rubles. Note that a ticket for x minutes activated at time t can be used for trips started in time range from t to t + x - 1, inclusive. Assume that all trips take exactly one minute.To simplify the choice for the passenger, the system automatically chooses the optimal tickets. After each trip starts, the system analyses all the previous trips and the current trip and chooses a set of tickets for these trips with a minimum total cost. Let the minimum total cost of tickets to cover all trips from the first to the current is a, and the total sum charged before is b. Then the system charges the passenger the sum a - b.You have to write a program that, for given trips made by a passenger, calculates the sum the passenger is charged after each trip.

The first line of input contains integer number n (1 ≤ n ≤ 105) — the number of trips made by passenger. Each of the following n lines contains the time of trip ti (0 ≤ ti ≤ 109), measured in minutes from the time of starting the system. All ti are different, given in ascending order, i. e. ti + 1 > ti holds for all 1 ≤ i < n.

Output n integers. For each trip, print the sum the passenger is charged after it.

NoteIn the first example, the system works as follows: for the first and second trips it is cheaper to pay for two one-trip tickets, so each time 20 rubles is charged, after the third trip the system understands that it would be cheaper to buy a ticket for 90 minutes. This ticket costs 50 rubles, and the passenger had already paid 40 rubles, so it is necessary to charge 10 rubles only.

let (+|) = Int64.add
let (-|) = Int64.sub
let ( *| ) = Int64.mul
let ( /| ) = Int64.div

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let t = Array.make n 0 in
  let _ =
    for i = 0 to n - 1 do
      t.(i) <- Scanf.bscanf sb "%d " (fun s -> s);
    done;
  in
  let bs x =
    let l = ref (-1) in
    let r = ref n in
      while !l < !r - 1 do
	let m = (!l + !r) / 2 in
	  if t.(m) <= x
	  then l := m
	  else r := m
      done;
      !r
  in
  let a = Array.make (n + 1) 0 in
    for i = 1 to n do
      let tt = t.(i - 1) in
      let p90 = bs (tt - 90) in
      let p1440 = bs (tt - 1440) in
	a.(i) <- a.(i - 1) + 20;
	if p90 < i then (
	  a.(i) <- min a.(i) (a.(p90) + 50)
	);
	if p1440 < i then (
	  a.(i) <- min a.(i) (a.(p1440) + 120)
	);
	Printf.printf "%d\n" (a.(i) - a.(i - 1));
    done;


One day two students, Grisha and Diana, found themselves in the university chemistry lab. In the lab the students found n test tubes with mercury numbered from 1 to n and decided to conduct an experiment.The experiment consists of q steps. On each step, one of the following actions occurs:  Diana pours all the contents from tube number pi and then pours there exactly xi liters of mercury.  Let's consider all the ways to add vi liters of water into the tubes; for each way let's count the volume of liquid (water and mercury) in the tube with water with maximum amount of liquid; finally let's find the minimum among counted maximums. That is the number the students want to count. At that, the students don't actually pour the mercury. They perform calculations without changing the contents of the tubes. Unfortunately, the calculations proved to be too complex and the students asked you to help them. Help them conduct the described experiment.

The first line contains two integers n and q (1 ≤ n, q ≤ 105) — the number of tubes ans the number of experiment steps. The next line contains n space-separated integers: h1, h2, ..., hn (0 ≤ hi ≤ 109), where hi is the volume of mercury in the і-th tube at the beginning of the experiment. The next q lines contain the game actions in the following format:   A line of form "1 pi xi" means an action of the first type (1 ≤ pi ≤ n; 0 ≤ xi ≤ 109).  A line of form "2 vi" means an action of the second type (1 ≤ vi ≤ 1015).  It is guaranteed that there is at least one action of the second type. It is guaranteed that all numbers that describe the experiment are integers.

For each action of the second type print the calculated value. The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 4.

open Printf
open Scanf

let ( -- ) a b = a - b
let ( ++ ) a b = a + b
let ( * ) a b = Int64.mul a b
let ( + ) a b = Int64.add a b
let ( - ) a b = Int64.sub a b

type tree =
    Empty | Node of tree * (int64*int64*int64) * tree * int64
(*                  left    (h,total,y)    right  size *)
(* what is stored in the node is the h value (the amount in this tube)
   and the total of all the h's in the subtree rooted here.
   and the h value of the leftmost node in the subtree rooted here.
*)

type direction = Left | Right
type path = Item of tree * direction

let size = function Empty -> 0L | Node(_, _, _, size) -> size

let getkey = function
  | Empty -> (0L,0L,0L)
  | Node(_, key, _, _) -> key

let getroot = function
  | Node(left, k, right, _) -> (left, k, right)
  | _ -> failwith "called getroot on Empty"

let glue (l, (h,_,_), r) =
  let (tl,y) = match l with Empty -> (0L,h) | Node(_,(_,tl,y),_,_) -> (tl,y) in
  let (_,tr,_) = getkey r in
  Node (l, (h, tl+tr+h, y), r, (size l) + (size r) + 1L)

let snip = function
  | Node(l, (h,_,_), r, s) -> Node(l, (h,0L,0L), r, 0L)
  | _ -> failwith "called snip on Empty"

let connect (l, key, r) = Node(l, key, r, 0L)

let splay t rank =
  (* splay the node of the given rank (which are numbered starting at 0)
     to the root *)

  (* The following splay function takes as input the currently splayed
     tree (so far) t, and a path list.  It takes one or two steps up,
     and calls itself recursively.  *)
  let rec rsplay = function
    | (t,[]) -> glue (getroot t)
    | (Node(tL,tK,tR,_), Item(Node(_, bK, bR,_), Left)::(Item(Node(_, cK, cR, _), Left)::tail)) ->
	rsplay (connect(tL,tK,glue(tR,bK,glue(bR,cK,cR))), tail)

    | (Node(tL,tK,tR,_), Item(Node(bL, bK, _,_), Right)::(Item(Node(cL, cK,_,_), Right)::tail)) ->
        rsplay (connect(glue(glue(cL,cK,bL), bK, tL),tK,tR), tail)

    | (Node(tL,tK,tR,_), Item(Node(_, bK, bR,_), Left)::tail) ->
	rsplay (connect(tL,tK,glue(tR,bK,bR)), tail)

    | (Node(tL,tK,tR,_), Item(Node(bL, bK, _,_), Right)::tail) ->
	rsplay (connect(glue(bL,bK,tL),tK,tR), tail)

    | (_,_) -> failwith "Splay_failed (Should not happen)"
  in

  let rec build_path t r path = (* r = the rank of the target node in the current tree t *)
    let t = snip t in
      match t with
	| Empty -> failwith "failed build_path"
	| Node(left, _, right, _) ->
	    let sl = size left in
	      if r < sl then build_path left r (Item(t, Left)::path)
	      else if r = sl then (t,path)
	      else build_path right (r-sl-1L) (Item(t, Right)::path)
  in
    if t=Empty then t else rsplay (build_path t rank [])

let append t h =
  glue(t, (h,0L,0L), Empty)

(* let key_of_rank t rank = getkey (splay t rank) *)

let rec findit x tabove sabove v =
(* x = the current node
   tabove = total left of x from above
   sabove = size of the stuff to the left of x from above
   this returns the rightmost (key,fill_volume) pair such
   that fill_volume < v
*)
  match x with Empty -> failwith "findit called on empty node"
    | Node (l, (h,_,_), r, _) ->
      let (_,t,_) = getkey l in
      let t = t + tabove in
      let s = sabove + (size l) in
      let fvx = h*s - t in
      if fvx >= v then findit l tabove sabove v
      else if r=Empty then (s,fvx) else
	let (_,_,y) = getkey r in
	let fvy = y*(s+1L) - (t+h) in
	if fvy >= v then (s,fvx)
	else findit r (t+h) (s+1L) v

let rec findh x h sabove c =
  (* retrns the rank of the given h in the tree,
     or the last one touched on a search for it *)
  match x with
    | Empty -> sabove - c
    | Node(l, (hx,_,_), r, _) ->
      if h < hx then findh l h sabove 0L
      else if h > hx then findh r h (sabove + (size l) + 1L) 1L
      else sabove + (size l)

let join l r =
  if r=Empty then l else
    let r = snip (splay r 0L) in
    let (_,key,rr) = getroot r in
    glue (l, key, rr)

let fill_height root v =
  let (s,fvx) = findit root 0L 0L v in
  let root = splay root s in
  let (h,_,_) = getkey root in
  let vol = v - fvx in
  let ans = (Int64.to_float h) +. (Int64.to_float vol)/.(Int64.to_float (s+1L)) in
  (root, ans)

let update t h0 h1 =
  (* replace h0 with h1 in the data structure *)
  let t = splay t (findh t h1 0L 0L) in
  let (l,(h,_,_),r) = getroot t in
  let t = if h <= h1 then glue (glue (l, (h,0L,0L), Empty), (h1,0L,0L), r)
    else glue (l, (h1,0L,0L), (glue (Empty, (h,0L,0L), r))) in
  let t = splay t (findh t h0 0L 0L) in
  let (l, _, r) = getroot t in
  join l r

(*-------------------------------------------------------------------------*)

let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)
let read_long () = bscanf Scanning.stdib " %Ld " (fun x -> x)

let () =
  let n = read_int () in
  let q = read_int () in

  let root = ref Empty in
  let h = Array.init n (fun _ -> read_long()) in
  let hh = Array.copy h in
  Array.sort compare hh;

  for i=0 to n--1 do
    root := append !root hh.(i)
  done;

  for i=1 to q do
    match read_int() with
      | 1 -> let p = read_int () in
	     let x = read_long () in
	     root:= update !root h.(p--1) x;
	     h.(p--1) <- x

      | 2 -> let v = read_long() in
	     let (t,ans) = fill_height !root v in
	     root := t;
	     printf "%f\n" ans

      | _ -> failwith "bad operation"
  done

open Printf
open Scanf

let ( -- ) a b = a - b
let ( ++ ) a b = a + b
let ( * ) a b = Int64.mul a b
let ( + ) a b = Int64.add a b
let ( - ) a b = Int64.sub a b

type tree =
    Empty | Node of tree * (int64*int64*int64) * tree * int64
(*                  left    (h,total,y)    right  size *)
(* what is stored in the node is the h value (the amount in this tube)
   and the total of all the h's in the subtree rooted here.
   and the h value of the leftmost node in the subtree rooted here.
*)

type direction = Left | Right
type path = Item of tree * direction

let size = function Empty -> 0L | Node(_, _, _, size) -> size

let getkey = function
  | Empty -> (0L,0L,0L)
  | Node(_, key, _, _) -> key

let getroot = function
  | Node(left, k, right, _) -> (left, k, right)
  | _ -> failwith "called getroot on Empty"

let glue (l, (h,_,_), r) =
  let (tl,y) = match l with Empty -> (0L,h) | Node(_,(_,tl,y),_,_) -> (tl,y) in
  let (_,tr,_) = getkey r in
  Node (l, (h, tl+tr+h, y), r, (size l) + (size r) + 1L)

let snip = function
  | Node(l, (h,_,_), r, s) -> Node(l, (h,0L,0L), r, 0L)
  | _ -> failwith "called snip on Empty"

let connect (l, key, r) = Node(l, key, r, 0L)

let splay t rank =
  (* splay the node of the given rank (which are numbered starting at 0)
     to the root *)

  (* The following splay function takes as input the currently splayed
     tree (so far) t, and a path list.  It takes one or two steps up,
     and calls itself recursively.  *)
  let rec rsplay = function
    | (t,[]) -> glue (getroot t)
    | (Node(tL,tK,tR,_), Item(Node(_, bK, bR,_), Left)::(Item(Node(_, cK, cR, _), Left)::tail)) ->
	rsplay (connect(tL,tK,glue(tR,bK,glue(bR,cK,cR))), tail)

    | (Node(tL,tK,tR,_), Item(Node(bL, bK, _,_), Right)::(Item(Node(cL, cK,_,_), Right)::tail)) ->
        rsplay (connect(glue(glue(cL,cK,bL), bK, tL),tK,tR), tail)

    | (Node(tL,tK,tR,_), Item(Node(_, bK, bR,_), Left)::tail) ->
	rsplay (connect(tL,tK,glue(tR,bK,bR)), tail)

    | (Node(tL,tK,tR,_), Item(Node(bL, bK, _,_), Right)::tail) ->
	rsplay (connect(glue(bL,bK,tL),tK,tR), tail)

    | (_,_) -> failwith "Splay_failed (Should not happen)"
  in

  let rec build_path t r path = (* r = the rank of the target node in the current tree t *)
    let t = snip t in
      match t with
	| Empty -> failwith "failed build_path"
	| Node(left, _, right, _) ->
	    let sl = size left in
	      if r < sl then build_path left r (Item(t, Left)::path)
	      else if r = sl then (t,path)
	      else build_path right (r-sl-1L) (Item(t, Right)::path)
  in
    if t=Empty then t else rsplay (build_path t rank [])

let append t h =
  glue(t, (h,0L,0L), Empty)

(* let key_of_rank t rank = getkey (splay t rank) *)

let rec findit x tabove sabove v =
(* x = the current node
   tabove = total left of x from above
   sabove = size of the stuff to the left of x from above
   assumes there's an infinite height node on the right as a sentinal
*)
  match x with Empty -> failwith "findit called on empty node"
    | Node (l, (h,_,_), r, _) ->
      let (_,t,_) = getkey l in
      let t = t + tabove in
      let s = sabove + (size l) in
      let fvx = h*s - t in
      if fvx >= v then findit l tabove sabove v
      else
	let (_,_,y) = getkey r in
	let fvy = y*(s+1L) - (t+h) in
	if r=Empty || fvy >= v then (s,fvx)
	else findit r (t+h) (s+1L) v

let rec findh x h sabove c =
  (* retrns the rank of the given h in the tree,
     or the last one touched on a search for it *)
  match x with
    | Empty -> sabove - c
    | Node(l, (hx,_,_), r, _) ->
      if h < hx then findh l h sabove 0L
      else if h > hx then findh r h (sabove + (size l) + 1L) 1L
      else sabove + (size l)

let join l r =
  if r=Empty then l else
    let r = snip (splay r 0L) in
    let (_,key,rr) = getroot r in
    glue (l, key, rr)

let fill_height root v =
  let (s,fvx) = findit root 0L 0L v in
  let root = splay root s in
  let (h,_,_) = getkey root in
  let vol = v - fvx in
  let ans = (Int64.to_float h) +. (Int64.to_float vol)/.(Int64.to_float (s+1L)) in
  (root, ans)

let update t h0 h1 =
  (* replace h0 with h1 in the data structure *)
  let t = splay t (findh t h0 0L 0L) in
  let (l, _, r) = getroot (snip t) in
  let t = join l r in
  let t = splay t (findh t h1 0L 0L) in
  let (l,(h,_,_),r) = getroot (snip t) in
  if h <= h1 then glue (glue (l, (h,0L,0L), Empty), (h1,0L,0L), r)
  else glue (l, (h1,0L,0L), (glue (Empty, (h,0L,0L), r)))

(*-------------------------------------------------------------------------*)

(* Debugging code.....

let rec printtree t depth =
  match t with Empty -> ()
  | Node(ll, (h,t,y), rr, s) -> (
    printtree rr (depth+1);
    let rec loop i = if i=0 then () else (Printf.printf " "; loop (i-1)) in
    loop depth;  Printf.printf "(%d,%d,%d) %d\n" h t y s;
    printtree ll (depth+1)
  )

let rec maketree t li = match li with [] -> t
  | h::rest -> maketree (append t h) rest

let t = maketree Empty [1;1;2;2;6]
let _ = printtree t 0

let t = splay t 2
let _ = printtree t 0

let (t,ans) = fill_height t 3
let _ = printf "ans = %f\n" ans

let t = update t 1 3

let (t,ans) = fill_height t 3
let _ = printf "ans = %f\n" ans

let t = update t 2 1
let (t,ans) = fill_height t 3
let _ = printf "ans = %f\n" ans

let t = update t 6 1
let (t,ans) = fill_height t 3
let _ = printf "ans = %f\n" ans

let t = update t 1 6
let (t,ans) = fill_height t 3
let _ = printf "ans = %f\n" ans

*)

let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)
let read_long () = bscanf Scanning.stdib " %Ld " (fun x -> x)

let inf = 1_000_000_001L

let () =
  let n = read_int () in
  let q = read_int () in

  let root = ref Empty in
  let h = Array.init n (fun _ -> read_long()) in
  let hh = Array.copy h in
  Array.sort compare hh;

  for i=0 to n--1 do
    root := append !root hh.(i)
  done;

  root := append !root inf;

  for i=1 to q do
    match read_int() with
      | 1 -> let p = read_int () in
	     let x = read_long () in
	     root:= update !root h.(p--1) x;
	     h.(p--1) <- x

      | 2 -> let v = read_long() in
	     let (t,ans) = fill_height !root v in
	     root := t;
	     printf "%f\n" ans

      | _ -> failwith "bad operation"
  done

open Printf
open Scanf

let ( -- ) a b = a - b
let ( ++ ) a b = a + b
let ( * ) a b = Int64.mul a b
let ( + ) a b = Int64.add a b
let ( - ) a b = Int64.sub a b

type tree =
    Empty | Node of tree * (int64*int64*int64) * tree * int64
(*                  left    (h,total,y)    right  size *)
(* what is stored in the node is the h value (the amount in this tube)
   and the total of all the h's in the subtree rooted here.
   and the h value of the leftmost node in the subtree rooted here.
*)

type direction = Left | Right
type path = Item of tree * direction

let size = function Empty -> 0L | Node(_, _, _, size) -> size

let getkey = function
  | Empty -> (0L,0L,0L)
  | Node(_, key, _, _) -> key

let getroot = function
  | Node(left, k, right, _) -> (left, k, right)
  | _ -> failwith "called getroot on Empty"

let glue (l, (h,_,_), r) =
  let (tl,y) = match l with Empty -> (0L,h) | Node(_,(_,tl,y),_,_) -> (tl,y) in
  let (_,tr,_) = getkey r in
  Node (l, (h, tl+tr+h, y), r, (size l) + (size r) + 1L)

let snip = function
  | Node(l, (h,_,_), r, s) -> Node(l, (h,0L,0L), r, 0L)
  | _ -> failwith "called snip on Empty"

let connect (l, key, r) = Node(l, key, r, 0L)

let splay t rank =
  (* splay the node of the given rank (which are numbered starting at 0)
     to the root *)

  (* The following splay function takes as input the currently splayed
     tree (so far) t, and a path list.  It takes one or two steps up,
     and calls itself recursively.  *)
  let rec rsplay = function
    | (t,[]) -> glue (getroot t)
    | (Node(tL,tK,tR,_), Item(Node(_, bK, bR,_), Left)::(Item(Node(_, cK, cR, _), Left)::tail)) ->
	rsplay (connect(tL,tK,glue(tR,bK,glue(bR,cK,cR))), tail)

    | (Node(tL,tK,tR,_), Item(Node(bL, bK, _,_), Right)::(Item(Node(cL, cK,_,_), Right)::tail)) ->
        rsplay (connect(glue(glue(cL,cK,bL), bK, tL),tK,tR), tail)

    | (Node(tL,tK,tR,_), Item(Node(_, bK, bR,_), Left)::tail) ->
	rsplay (connect(tL,tK,glue(tR,bK,bR)), tail)

    | (Node(tL,tK,tR,_), Item(Node(bL, bK, _,_), Right)::tail) ->
	rsplay (connect(glue(bL,bK,tL),tK,tR), tail)

    | (_,_) -> failwith "Splay_failed (Should not happen)"
  in

  let rec build_path t r path = (* r = the rank of the target node in the current tree t *)
    let t = snip t in
      match t with
	| Empty -> failwith "failed build_path"
	| Node(left, _, right, _) ->
	    let sl = size left in
	      if r < sl then build_path left r (Item(t, Left)::path)
	      else if r = sl then (t,path)
	      else build_path right (r-sl-1L) (Item(t, Right)::path)
  in
    if t=Empty then t else rsplay (build_path t rank [])

let append t h =
  glue(t, (h,0L,0L), Empty)

(* let key_of_rank t rank = getkey (splay t rank) *)

let rec findit x tabove sabove v =
(* x = the current node
   tabove = total left of x from above
   sabove = size of the stuff to the left of x from above
   assumes there's an infinite height node on the right as a sentinal
*)
  match x with Empty -> failwith "findit called on empty node"
    | Node (l, (h,_,_), r, _) ->
      let (_,t,_) = getkey l in
      let t = t + tabove in
      let s = sabove + (size l) in
      let fvx = h*s - t in
      if fvx >= v then findit l tabove sabove v
      else
	let (_,_,y) = getkey r in
	let fvy = y*(s+1L) - (t+h) in
	if r=Empty || fvy >= v then (s,fvx)
	else findit r (t+h) (s+1L) v

let rec findh x h sabove c =
  (* retrns the rank of the given h in the tree,
     or the last one touched on a search for it *)
  match x with
    | Empty -> sabove - c
    | Node(l, (hx,_,_), r, _) ->
      if h < hx then findh l h sabove 0L
      else if h > hx then findh r h (sabove + (size l) + 1L) 1L
      else sabove + (size l)

let join l r =
  if r=Empty then l else
    let r = snip (splay r 0L) in
    let (_,key,rr) = getroot r in
    glue (l, key, rr)

let fill_height root v =
  let (s,fvx) = findit root 0L 0L v in
  let root = splay root s in
  let (h,_,_) = getkey root in
  let vol = v - fvx in
  let ans = (Int64.to_float h) +. (Int64.to_float vol)/.(Int64.to_float (s+1L)) in
  (root, ans)

let update t h0 h1 =
  (* replace h0 with h1 in the data structure *)
  let t = splay t (findh t h0 0L 0L) in
  let (l, _, r) = getroot (snip t) in
  let t = join l r in
  let t = splay t (findh t h1 0L 0L) in
  let (l,(h,_,_),r) = getroot (snip t) in
  if h <= h1 then glue (glue (l, (h,0L,0L), Empty), (h1,0L,0L), r)
  else glue (l, (h1,0L,0L), (glue (Empty, (h,0L,0L), r)))

(*-------------------------------------------------------------------------*)

(* Debugging code.....

let rec printtree t depth =
  match t with Empty -> ()
  | Node(ll, (h,t,y), rr, s) -> (
    printtree rr (depth+1);
    let rec loop i = if i=0 then () else (Printf.printf " "; loop (i-1)) in
    loop depth;  Printf.printf "(%d,%d,%d) %d\n" h t y s;
    printtree ll (depth+1)
  )

let rec maketree t li = match li with [] -> t
  | h::rest -> maketree (append t h) rest

let t = maketree Empty [1;1;2;2;6]
let _ = printtree t 0

let t = splay t 2
let _ = printtree t 0

let (t,ans) = fill_height t 3
let _ = printf "ans = %f\n" ans

let t = update t 1 3

let (t,ans) = fill_height t 3
let _ = printf "ans = %f\n" ans

let t = update t 2 1
let (t,ans) = fill_height t 3
let _ = printf "ans = %f\n" ans

let t = update t 6 1
let (t,ans) = fill_height t 3
let _ = printf "ans = %f\n" ans

let t = update t 1 6
let (t,ans) = fill_height t 3
let _ = printf "ans = %f\n" ans

*)

let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)
let read_long () = bscanf Scanning.stdib " %Ld " (fun x -> x)

(* let inf = Int64.shift_left 1L 40 *)

let inf = 2_000_000_000_000_000L

let () =
  let n = read_int () in
  let q = read_int () in

  let root = ref Empty in
  let h = Array.init n (fun _ -> read_long()) in
  let hh = Array.copy h in
  Array.sort compare hh;

  for i=0 to n--1 do
    root := append !root hh.(i)
  done;

  root := append !root inf;

  for i=1 to q do
    match read_int() with
      | 1 -> let p = read_int () in
	     let x = read_long () in
	     root:= update !root h.(p--1) x;
	     h.(p--1) <- x

      | 2 -> let v = read_long() in
	     let (t,ans) = fill_height !root v in
	     root := t;
	     printf "%f\n" ans

      | _ -> failwith "bad operation"
  done


Zart PMP is qualified for ICPC World Finals in Harbin, China. After team excursion to Sun Island Park for snow sculpture art exposition, PMP should get back to buses before they leave. But the park is really big and he does not know how to find them.The park has n intersections numbered 1 through n. There are m bidirectional roads that connect some pairs of these intersections. At k intersections, ICPC volunteers are helping the teams and showing them the way to their destinations. Locations of volunteers are fixed and distinct.When PMP asks a volunteer the way to bus station, he/she can tell him the whole path. But the park is fully covered with ice and snow and everywhere looks almost the same. So PMP can only memorize at most q intersections after each question (excluding the intersection they are currently standing). He always tells volunteers about his weak memory and if there is no direct path of length (in number of roads) at most q that leads to bus station, the volunteer will guide PMP to another volunteer (who is at most q intersections away, of course). ICPC volunteers know the area very well and always tell PMP the best way. So if there exists a way to bus stations, PMP will definitely find it.PMP's initial location is intersection s and the buses are at intersection t. There will always be a volunteer at intersection s. Your job is to find out the minimum q which guarantees that PMP can find the buses.

The first line contains three space-separated integers n, m, k (2 ≤ n ≤ 105, 0 ≤ m ≤ 2·105, 1 ≤ k ≤ n) — the number of intersections, roads and volunteers, respectively. Next line contains k distinct space-separated integers between 1 and n inclusive — the numbers of cities where volunteers are located. Next m lines describe the roads. The i-th of these lines contains two space-separated integers ui, vi (1 ≤ ui, vi ≤ n, ui ≠ vi) — two intersections that i-th road connects. There will be at most one road between any two intersections. Last line of input contains two space-separated integers s, t (1 ≤ s, t ≤ n, s ≠ t) — the initial location of PMP and the location of the buses. It might not always be possible to reach t from s. It is guaranteed that there is always a volunteer at intersection s.

Print on the only line the answer to the problem — the minimum value of q which guarantees that PMP can find the buses. If PMP cannot reach the buses at all, output -1 instead.

NoteThe first sample is illustrated below. Blue intersections are where volunteers are located. If PMP goes in the path of dashed line, it can reach the buses with q = 3:  In the second sample, PMP uses intersection 6 as an intermediate intersection, thus the answer is 3.

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let m = Scanf.bscanf sb "%d " (fun s -> s) in
  let k = Scanf.bscanf sb "%d " (fun s -> s) in
  let vol = Array.make n false in
  let vv = Array.make k 0 in
  let es = Array.make n [] in
  let min (x : int) y = if x < y then x else y in
  let () =
    for i = 0 to k - 1 do
      let x = Scanf.bscanf sb "%d " (fun s -> s) - 1 in
	vol.(x) <- true;
	vv.(i) <- x;
    done;
    for i = 1 to m  do
      let u = Scanf.bscanf sb "%d " (fun s -> s) - 1 in
      let v = Scanf.bscanf sb "%d " (fun s -> s) - 1 in
	es.(u) <- v :: es.(u);
	es.(v) <- u :: es.(v);
    done;
  in
  let es = Array.map Array.of_list es in
  let s = Scanf.bscanf sb "%d " (fun s -> s) - 1 in
  let t = Scanf.bscanf sb "%d " (fun s -> s) - 1 in
  let used = Array.make n false in
  let d = Array.make n 0 in
  let inf = 100000000 in
  let check q =
    for i = 0 to n - 1 do
      used.(i) <- false;
      d.(i) <- inf;
    done;
    let queue = Queue.create () in
    let rec bfs () =
      if not (Queue.is_empty queue) then (
	let u = Queue.pop queue in
	  (*Printf.printf "q %d %d  %d %d\n" u d.(u) q (Queue.length queue);*)
	  if d.(u) < q then (
	    for i = 0 to Array.length es.(u) - 1 do
	      let v = es.(u).(i) in
		if not used.(v) || d.(v) > d.(u) + 1 then (
		  used.(v) <- true;
		  d.(v) <- if vol.(v) then 0 else min d.(v) (d.(u) + 1);
	  (*Printf.printf "add %d %d\n" v d.(v);*)
		  Queue.add v queue;
		)
	    done;
	  );
	  bfs ()
      )
    in
      used.(s) <- true;
      d.(s) <- 0;
      Queue.add s queue;
      bfs ();
      d.(t) <= q
  in
  let l = ref 0 in
  let r = ref (n + 1) in
    while !l < !r - 1 do
      let m = (!l + !r) / 2 in
	if check m
	then r := m
	else l := m
    done;
    if !r > n
    then Printf.printf "-1\n"
    else Printf.printf "%d\n" !r


We've got a rectangular n × m-cell maze. Each cell is either passable, or is a wall (impassable). A little boy found the maze and cyclically tiled a plane with it so that the plane became an infinite maze. Now on this plane cell (x, y) is a wall if and only if cell  is a wall.In this problem  is a remainder of dividing number a by number b.The little boy stood at some cell on the plane and he wondered whether he can walk infinitely far away from his starting position. From cell (x, y) he can go to one of the following cells: (x, y - 1), (x, y + 1), (x - 1, y) and (x + 1, y), provided that the cell he goes to is not a wall.

The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 1500) — the height and the width of the maze that the boy used to cyclically tile the plane. Each of the next n lines contains m characters — the description of the labyrinth. Each character is either a "#", that marks a wall, a ".", that marks a passable cell, or an "S", that marks the little boy's starting point.  The starting point is a passable cell. It is guaranteed that character "S" occurs exactly once in the input.

Print "Yes" (without the quotes), if the little boy can walk infinitely far from the starting point. Otherwise, print "No" (without the quotes).

NoteIn the first sample the little boy can go up for infinitely long as there is a "clear path" that goes vertically. He just needs to repeat the following steps infinitely: up, up, left, up, up, right, up.In the second sample the vertical path is blocked. The path to the left doesn't work, too — the next "copy" of the maze traps the boy.

exception Found

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let m = Scanf.bscanf sb "%d " (fun s -> s) in
  let a = Array.make_matrix n m false in
  let sx = ref 0 in
  let sy = ref 0 in
  let () =
    for i = 0 to n - 1 do
      let s = Scanf.bscanf sb "%s " (fun s -> s) in
	for j = 0 to m - 1 do
	  if s.[j] <> '#'
	  then a.(i).(j) <- true;
	  if s.[j] = 'S' then (
	    sy := i;
	    sx := j;
	  )
	done
    done;
  in
  let sx = !sx
  and sy = !sy in
  let c = Array.make_matrix n m (-1) in
  let color = ref 0 in
  let q1 = Array.make (n * m) (0, 0) in
  let l1 = ref 0 in
  let q2 = Array.make (n * m) (0, 0) in
  let l2 = ref 0 in
  let d = [| (-1, 0); (1, 0); (0, 1); (0, -1) |] in
  let rec bfs_step k q1 l1 q2 l2 =
    l2 := 0;
    for i = 0 to !l1 - 1 do
      let (x, y) = q1.(i) in
	for i = 0 to 3 do
	  let (dx, dy) = d.(i) in
	  let x = x + dx
	  and y = y + dy in
	  (*let x = (x + m) mod m in
	  let y = (y + n) mod n in*)
	    if x >= 0 && y >= 0 && x < m && y < n && a.(y).(x) then (
	      if c.(y).(x) < 0 then (
		c.(y).(x) <- !color;
		q2.(!l2) <- (x, y);
		incr l2
	      );
	    );
	done
    done;
    if !l2 > 0
    then bfs_step (k + 1) q2 l2 q1 l1
  in
    for y = 0 to n - 1 do
      let x = 0 in
	if a.(y).(x) && c.(y).(x) < 0 then (
	  c.(y).(x) <- !color;
	  l1 := 0;
	  l2 := 0;
	  q1.(!l1) <- (x, y);
	  incr l1;
	  bfs_step 1 q1 l1 q2 l2;
	  incr color;
	)
    done;
    for y = 0 to n - 1 do
      let x = m - 1 in
	if a.(y).(x) && c.(y).(x) < 0 then (
	  c.(y).(x) <- !color;
	  l1 := 0;
	  l2 := 0;
	  q1.(!l1) <- (x, y);
	  incr l1;
	  bfs_step 1 q1 l1 q2 l2;
	  incr color;
	)
    done;
    for x = 0 to m - 1 do
      let y = 0 in
	if a.(y).(x) && c.(y).(x) < 0 then (
	  c.(y).(x) <- !color;
	  l1 := 0;
	  l2 := 0;
	  q1.(!l1) <- (x, y);
	  incr l1;
	  bfs_step 1 q1 l1 q2 l2;
	  incr color;
	)
    done;
    for x = 0 to m - 1 do
      let y = n - 1 in
	if a.(y).(x) && c.(y).(x) < 0 then (
	  c.(y).(x) <- !color;
	  l1 := 0;
	  l2 := 0;
	  q1.(!l1) <- (x, y);
	  incr l1;
	  bfs_step 1 q1 l1 q2 l2;
	  incr color;
	)
    done;
    let res = ref false in
    (*let () =
      for y = 0 to n - 1 do
	for x = 0 to m - 1 do
	  Printf.printf "%2d " c.(y).(x)
	done;
	Printf.printf "\n"
      done
    in*)
    let es = Array.make !color [] in
    let () =
      for x = 0 to m - 1 do
	let y1 = 0
	and y2 = n - 1 in
	  if a.(y1).(x) && a.(y2).(x) then (
	    let c1 = c.(y1).(x)
	    and c2 = c.(y2).(x) in
	      es.(c1) <- (c2, 1, 0) :: es.(c1);
	      es.(c2) <- (c1, -1, 0) :: es.(c2);
	  )
      done;
      for y = 0 to n - 1 do
	let x1 = 0
	and x2 = m - 1 in
	  if a.(y).(x1) && a.(y).(x2) then (
	    let c1 = c.(y).(x1)
	    and c2 = c.(y).(x2) in
	      es.(c1) <- (c2, 0, 1) :: es.(c1);
	      es.(c2) <- (c1, 0, -1) :: es.(c2);
	  )
      done;
    in
    let es = Array.map Array.of_list es in
    (let used = Array.make !color (-1) in
    let start = c.(sy).(sx) in
    let rec dfs u z =
      if used.(u) >= 0 && used.(u) < z
      then raise Found;
      used.(u) <- z;
      for i = 0 to Array.length es.(u) - 1 do
	let (v, dz, _) = es.(u).(i) in
	  (*if v = start
	  then res := true;*)
	  if used.(v) < z + dz then (
	    dfs v (z + dz);
	  )
      done
    in
      try
	if start >= 0
	then dfs start 0;
      with
	| Found -> res := true
    );
    (let used = Array.make !color false in
     let dist = Array.make !color (0, 0) in
     let start = c.(sy).(sx) in
     let rec dfs u x y =
       if used.(u) && dist.(u) <> (x, y)
       then raise Found;
       dist.(u) <- (x, y);
       used.(u) <- true;
       for i = 0 to Array.length es.(u) - 1 do
	 let (v, dx, dy) = es.(u).(i) in
	   (*if v = start
	     then res := true;*)
	   if not used.(v) || dist.(v) <> (x + dx, y + dy) then (
	     dfs v (x + dx) (y + dy);
	   )
       done
     in
       try
	 if start >= 0
	 then dfs start 0 0;
       with
	 | Found -> res := true
    );
    (*let cc = c.(sy).(sx) in
      if cc >= 0 then (
	for x = 0 to m - 1 do
	  let y1 = 0
	  and y2 = n - 1 in
	    if a.(y1).(x) && a.(y2).(x) &&
	      c.(y1).(x) = cc && c.(y2).(x) = cc
	    then (
	      res := true
	    )
	done;
	for y = 0 to n - 1 do
	  let x1 = 0
	  and x2 = m - 1 in
	    if a.(y).(x1) && a.(y).(x2) &&
	      c.(y).(x1) = cc && c.(y).(x2) = cc
	    then (
	      res := true
	    )
	done;
      );*)
      if !res
      then Printf.printf "YES\n"
      else Printf.printf "NO\n"

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let m = Scanf.bscanf sb "%d " (fun s -> s) in
  let a = Array.make_matrix n m false in
  let sx = ref 0 in
  let sy = ref 0 in
  let () =
    for i = 0 to n - 1 do
      let s = Scanf.bscanf sb "%s " (fun s -> s) in
	for j = 0 to m - 1 do
	  if s.[j] <> '#'
	  then a.(i).(j) <- true;
	  if s.[j] = 'S' then (
	    sy := i;
	    sx := j;
	  )
	done
    done;
  in
  let sx = !sx
  and sy = !sy in
  let c = Array.make_matrix n m (-1) in
  let color = ref 0 in
  let q1 = Array.make (n * m) (0, 0) in
  let l1 = ref 0 in
  let q2 = Array.make (n * m) (0, 0) in
  let l2 = ref 0 in
  let d = [| (-1, 0); (1, 0); (0, 1); (0, -1) |] in
  let rec bfs_step k q1 l1 q2 l2 =
    l2 := 0;
    for i = 0 to !l1 - 1 do
      let (x, y) = q1.(i) in
	for i = 0 to 3 do
	  let (dx, dy) = d.(i) in
	  let x = x + dx
	  and y = y + dy in
	    if x >= 0 && y >= 0 && x < m && y < n && a.(y).(x) then (
	      if c.(y).(x) < 0 then (
		c.(y).(x) <- !color;
		q2.(!l2) <- (x, y);
		incr l2
	      );
	    );
	done
    done;
    if !l2 > 0
    then bfs_step (k + 1) q2 l2 q1 l1
  in
    for y = 0 to n - 1 do
      let x = 0 in
	if a.(y).(x) && c.(y).(x) < 0 then (
	  c.(y).(x) <- !color;
	  l1 := 0;
	  l2 := 0;
	  q1.(!l1) <- (x, y);
	  incr l1;
	  bfs_step 1 q1 l1 q2 l2;
	  incr color;
	)
    done;
    for y = 0 to n - 1 do
      let x = m - 1 in
	if a.(y).(x) && c.(y).(x) < 0 then (
	  c.(y).(x) <- !color;
	  l1 := 0;
	  l2 := 0;
	  q1.(!l1) <- (x, y);
	  incr l1;
	  bfs_step 1 q1 l1 q2 l2;
	  incr color;
	)
    done;
    for x = 0 to m - 1 do
      let y = 0 in
	if a.(y).(x) && c.(y).(x) < 0 then (
	  c.(y).(x) <- !color;
	  l1 := 0;
	  l2 := 0;
	  q1.(!l1) <- (x, y);
	  incr l1;
	  bfs_step 1 q1 l1 q2 l2;
	  incr color;
	)
    done;
    for x = 0 to m - 1 do
      let y = n - 1 in
	if a.(y).(x) && c.(y).(x) < 0 then (
	  c.(y).(x) <- !color;
	  l1 := 0;
	  l2 := 0;
	  q1.(!l1) <- (x, y);
	  incr l1;
	  bfs_step 1 q1 l1 q2 l2;
	  incr color;
	)
    done;
    let res = ref false in
    (*let () =
      for y = 0 to n - 1 do
	for x = 0 to m - 1 do
	  Printf.printf "%d " c.(y).(x)
	done;
	Printf.printf "\n"
      done
    in*)
      (let es = Array.make !color [] in
       let () =
	 for x = 0 to m - 1 do
	   let y1 = 0
	   and y2 = n - 1 in
	     if a.(y1).(x) && a.(y2).(x) then (
	       let c1 = c.(y1).(x)
	       and c2 = c.(y2).(x) in
		 es.(c1) <- c2 :: es.(c1);
		 (*es.(c2) <- c1 :: es.(c2);*)
	     )
	 done;
	 for y = 0 to n - 1 do
	   let x1 = 0
	   and x2 = m - 1 in
	     if a.(y).(x1) && a.(y).(x2) then (
	       let c1 = c.(y).(x1)
	       and c2 = c.(y).(x2) in
		 es.(c1) <- c2 :: es.(c1);
		 (*es.(c2) <- c1 :: es.(c2);*)
	     )
	 done;
       in
       let es = Array.map Array.of_list es in
       let used = Array.make !color false in
       let start = c.(sy).(sx) in
       let rec dfs u =
	 used.(u) <- true;
	 for i = 0 to Array.length es.(u) - 1 do
	   let v = es.(u).(i) in
	     if v = start
	     then res := true;
	     if not used.(v) then (
	       dfs v;
	     )
	 done
       in
	 if start >= 0
	 then dfs start;
      );
      (let es = Array.make !color [] in
       let () =
	 for x = 0 to m - 1 do
	   let y1 = 0
	   and y2 = n - 1 in
	     if a.(y1).(x) && a.(y2).(x) then (
	       let c1 = c.(y1).(x)
	       and c2 = c.(y2).(x) in
		 es.(c1) <- c2 :: es.(c1);
		 (*es.(c2) <- c1 :: es.(c2);*)
	     )
	 done;
	 for y = 0 to n - 1 do
	   let x1 = 0
	   and x2 = m - 1 in
	     if a.(y).(x1) && a.(y).(x2) then (
	       let c1 = c.(y).(x1)
	       and c2 = c.(y).(x2) in
		 (*es.(c1) <- c2 :: es.(c1);*)
		 es.(c2) <- c1 :: es.(c2);
	     )
	 done;
       in
       let es = Array.map Array.of_list es in
       let used = Array.make !color false in
       let start = c.(sy).(sx) in
       let rec dfs u =
	 used.(u) <- true;
	 for i = 0 to Array.length es.(u) - 1 do
	   let v = es.(u).(i) in
	     if v = start
	     then res := true;
	     if not used.(v) then (
	       dfs v;
	     )
	 done
       in
	 if start >= 0
	 then dfs start;
      );
      if !res
      then Printf.printf "YES\n"
      else Printf.printf "NO\n"

exception Found

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let m = Scanf.bscanf sb "%d " (fun s -> s) in
  let a = Array.make_matrix n m false in
  let sx = ref 0 in
  let sy = ref 0 in
  let () =
    for i = 0 to n - 1 do
      let s = Scanf.bscanf sb "%s " (fun s -> s) in
if n = 20 && m = 20 && i >= 10 then Printf.printf "%s\n" s;
	for j = 0 to m - 1 do
	  if s.[j] <> '#'
	  then a.(i).(j) <- true;
	  if s.[j] = 'S' then (
	    sy := i;
	    sx := j;
	  )
	done
    done;
  in
  let sx = !sx
  and sy = !sy in
  let c = Array.make_matrix n m (-1) in
  let color = ref 0 in
  let q1 = Array.make (n * m) (0, 0) in
  let l1 = ref 0 in
  let q2 = Array.make (n * m) (0, 0) in
  let l2 = ref 0 in
  let d = [| (-1, 0); (1, 0); (0, 1); (0, -1) |] in
  let rec bfs_step k q1 l1 q2 l2 =
    l2 := 0;
    for i = 0 to !l1 - 1 do
      let (x, y) = q1.(i) in
	for i = 0 to 3 do
	  let (dx, dy) = d.(i) in
	  let x = x + dx
	  and y = y + dy in
	  (*let x = (x + m) mod m in
	  let y = (y + n) mod n in*)
	    if x >= 0 && y >= 0 && x < m && y < n && a.(y).(x) then (
	      if c.(y).(x) < 0 then (
		c.(y).(x) <- !color;
		q2.(!l2) <- (x, y);
		incr l2
	      );
	    );
	done
    done;
    if !l2 > 0
    then bfs_step (k + 1) q2 l2 q1 l1
  in
    for y = 0 to n - 1 do
      let x = 0 in
	if a.(y).(x) && c.(y).(x) < 0 then (
	  c.(y).(x) <- !color;
	  l1 := 0;
	  l2 := 0;
	  q1.(!l1) <- (x, y);
	  incr l1;
	  bfs_step 1 q1 l1 q2 l2;
	  incr color;
	)
    done;
    for y = 0 to n - 1 do
      let x = m - 1 in
	if a.(y).(x) && c.(y).(x) < 0 then (
	  c.(y).(x) <- !color;
	  l1 := 0;
	  l2 := 0;
	  q1.(!l1) <- (x, y);
	  incr l1;
	  bfs_step 1 q1 l1 q2 l2;
	  incr color;
	)
    done;
    for x = 0 to m - 1 do
      let y = 0 in
	if a.(y).(x) && c.(y).(x) < 0 then (
	  c.(y).(x) <- !color;
	  l1 := 0;
	  l2 := 0;
	  q1.(!l1) <- (x, y);
	  incr l1;
	  bfs_step 1 q1 l1 q2 l2;
	  incr color;
	)
    done;
    for x = 0 to m - 1 do
      let y = n - 1 in
	if a.(y).(x) && c.(y).(x) < 0 then (
	  c.(y).(x) <- !color;
	  l1 := 0;
	  l2 := 0;
	  q1.(!l1) <- (x, y);
	  incr l1;
	  bfs_step 1 q1 l1 q2 l2;
	  incr color;
	)
    done;
    let res = ref false in
    (*let () =
      for y = 0 to n - 1 do
	for x = 0 to m - 1 do
	  Printf.printf "%d " c.(y).(x)
	done;
	Printf.printf "\n"
      done
    in*)
    let es = Array.make !color [] in
    let () =
      for x = 0 to m - 1 do
	let y1 = 0
	and y2 = n - 1 in
	  if a.(y1).(x) && a.(y2).(x) then (
	    let c1 = c.(y1).(x)
	    and c2 = c.(y2).(x) in
	      es.(c1) <- (c2, 1, 0) :: es.(c1);
	      es.(c2) <- (c1, -1, 0) :: es.(c2);
	  )
      done;
      for y = 0 to n - 1 do
	let x1 = 0
	and x2 = m - 1 in
	  if a.(y).(x1) && a.(y).(x2) then (
	    let c1 = c.(y).(x1)
	    and c2 = c.(y).(x2) in
	      es.(c1) <- (c2, 0, 1) :: es.(c1);
	      es.(c2) <- (c1, 0, -1) :: es.(c2);
	  )
      done;
    in
    let es = Array.map Array.of_list es in
    (let used = Array.make !color (-1) in
    let start = c.(sy).(sx) in
    let rec dfs u z =
      if used.(u) >= 0 && used.(u) < z
      then raise Found;
      used.(u) <- z;
      for i = 0 to Array.length es.(u) - 1 do
	let (v, dz, _) = es.(u).(i) in
	  (*if v = start
	  then res := true;*)
	  if used.(v) < z + dz then (
	    dfs v (z + dz);
	  )
      done
    in
      try
	if start >= 0
	then dfs start 0;
      with
	| Found -> res := true
    );
    (let used = Array.make !color (-1) in
    let start = c.(sy).(sx) in
    let rec dfs u z =
      if used.(u) >= 0 && used.(u) < z
      then raise Found;
      used.(u) <- z;
      for i = 0 to Array.length es.(u) - 1 do
	let (v, _, dz) = es.(u).(i) in
	  (*if v = start
	  then res := true;*)
	  if used.(v) < z + dz then (
	    dfs v (z + dz);
	  )
      done
    in
      try
	if start >= 0
	then dfs start 0;
      with
	| Found -> res := true
    );
    (*let cc = c.(sy).(sx) in
      if cc >= 0 then (
	for x = 0 to m - 1 do
	  let y1 = 0
	  and y2 = n - 1 in
	    if a.(y1).(x) && a.(y2).(x) &&
	      c.(y1).(x) = cc && c.(y2).(x) = cc
	    then (
	      res := true
	    )
	done;
	for y = 0 to n - 1 do
	  let x1 = 0
	  and x2 = m - 1 in
	    if a.(y).(x1) && a.(y).(x2) &&
	      c.(y).(x1) = cc && c.(y).(x2) = cc
	    then (
	      res := true
	    )
	done;
      );*)
      if !res
      then Printf.printf "YES\n"
      else Printf.printf "NO\n"

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let m = Scanf.bscanf sb "%d " (fun s -> s) in
  let a = Array.make_matrix n m false in
  let sx = ref 0 in
  let sy = ref 0 in
  let () =
    for i = 0 to n - 1 do
      let s = Scanf.bscanf sb "%s " (fun s -> s) in
	for j = 0 to m - 1 do
	  if s.[j] <> '#'
	  then a.(i).(j) <- true;
	  if s.[j] = 'S' then (
	    sy := i;
	    sx := j;
	  )
	done
    done;
  in
  let sx = !sx
  and sy = !sy in
  let c = Array.make_matrix n m (-1) in
  let color = ref 0 in
  let q1 = Array.make (n * m) (0, 0) in
  let l1 = ref 0 in
  let q2 = Array.make (n * m) (0, 0) in
  let l2 = ref 0 in
  let d = [| (-1, 0); (1, 0); (0, 1); (0, -1) |] in
  let rec bfs_step k q1 l1 q2 l2 =
    l2 := 0;
    for i = 0 to !l1 - 1 do
      let (x, y) = q1.(i) in
	for i = 0 to 3 do
	  let (dx, dy) = d.(i) in
	  let x = x + dx
	  and y = y + dy in
	    if x >= 0 && y >= 0 && x < m && y < n && a.(y).(x) then (
	      if c.(y).(x) < 0 then (
		c.(y).(x) <- !color;
		q2.(!l2) <- (x, y);
		incr l2
	      );
	    );
	done
    done;
    if !l2 > 0
    then bfs_step (k + 1) q2 l2 q1 l1
  in
    for y = 0 to n - 1 do
      let x = 0 in
	if a.(y).(x) && c.(y).(x) < 0 then (
	  c.(y).(x) <- !color;
	  l1 := 0;
	  l2 := 0;
	  q1.(!l1) <- (x, y);
	  incr l1;
	  bfs_step 1 q1 l1 q2 l2;
	  incr color;
	)
    done;
    for y = 0 to n - 1 do
      let x = m - 1 in
	if a.(y).(x) && c.(y).(x) < 0 then (
	  c.(y).(x) <- !color;
	  l1 := 0;
	  l2 := 0;
	  q1.(!l1) <- (x, y);
	  incr l1;
	  bfs_step 1 q1 l1 q2 l2;
	  incr color;
	)
    done;
    for x = 0 to m - 1 do
      let y = 0 in
	if a.(y).(x) && c.(y).(x) < 0 then (
	  c.(y).(x) <- !color;
	  l1 := 0;
	  l2 := 0;
	  q1.(!l1) <- (x, y);
	  incr l1;
	  bfs_step 1 q1 l1 q2 l2;
	  incr color;
	)
    done;
    for x = 0 to m - 1 do
      let y = n - 1 in
	if a.(y).(x) && c.(y).(x) < 0 then (
	  c.(y).(x) <- !color;
	  l1 := 0;
	  l2 := 0;
	  q1.(!l1) <- (x, y);
	  incr l1;
	  bfs_step 1 q1 l1 q2 l2;
	  incr color;
	)
    done;
    let es = Array.make !color [] in
    let () =
      for x = 0 to m - 1 do
	let y1 = 0
	and y2 = n - 1 in
	  if a.(y1).(x) && a.(y2).(x) then (
	    let c1 = c.(y1).(x)
	    and c2 = c.(y2).(x) in
	      es.(c1) <- c2 :: es.(c1);
	      es.(c2) <- c1 :: es.(c2);
	  )
      done;
    in
    let es = Array.map Array.of_list es in
    let used = Array.make !color false in
    let res = ref false in
    let start = c.(sy).(sx) in
    let rec dfs u =
      used.(u) <- true;
      for i = 0 to Array.length es.(u) - 1 do
	let v = es.(u).(i) in
	  if v = start
	  then res := true;
	  if not used.(v) then (
	    dfs v;
	  )
      done
    in
      if start >= 0
      then dfs start;
      if !res
      then Printf.printf "YES\n"
      else Printf.printf "NO\n"

exception Found

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let m = Scanf.bscanf sb "%d " (fun s -> s) in
  let a = Array.make_matrix n m false in
  let sx = ref 0 in
  let sy = ref 0 in
  let () =
    for i = 0 to n - 1 do
      let s = Scanf.bscanf sb "%s " (fun s -> s) in
if n = 20 && m = 20 then Printf.printf "%s\n" s;
	for j = 0 to m - 1 do
	  if s.[j] <> '#'
	  then a.(i).(j) <- true;
	  if s.[j] = 'S' then (
	    sy := i;
	    sx := j;
	  )
	done
    done;
  in
  let sx = !sx
  and sy = !sy in
  let c = Array.make_matrix n m (-1) in
  let color = ref 0 in
  let q1 = Array.make (n * m) (0, 0) in
  let l1 = ref 0 in
  let q2 = Array.make (n * m) (0, 0) in
  let l2 = ref 0 in
  let d = [| (-1, 0); (1, 0); (0, 1); (0, -1) |] in
  let rec bfs_step k q1 l1 q2 l2 =
    l2 := 0;
    for i = 0 to !l1 - 1 do
      let (x, y) = q1.(i) in
	for i = 0 to 3 do
	  let (dx, dy) = d.(i) in
	  let x = x + dx
	  and y = y + dy in
	  (*let x = (x + m) mod m in
	  let y = (y + n) mod n in*)
	    if x >= 0 && y >= 0 && x < m && y < n && a.(y).(x) then (
	      if c.(y).(x) < 0 then (
		c.(y).(x) <- !color;
		q2.(!l2) <- (x, y);
		incr l2
	      );
	    );
	done
    done;
    if !l2 > 0
    then bfs_step (k + 1) q2 l2 q1 l1
  in
    for y = 0 to n - 1 do
      let x = 0 in
	if a.(y).(x) && c.(y).(x) < 0 then (
	  c.(y).(x) <- !color;
	  l1 := 0;
	  l2 := 0;
	  q1.(!l1) <- (x, y);
	  incr l1;
	  bfs_step 1 q1 l1 q2 l2;
	  incr color;
	)
    done;
    for y = 0 to n - 1 do
      let x = m - 1 in
	if a.(y).(x) && c.(y).(x) < 0 then (
	  c.(y).(x) <- !color;
	  l1 := 0;
	  l2 := 0;
	  q1.(!l1) <- (x, y);
	  incr l1;
	  bfs_step 1 q1 l1 q2 l2;
	  incr color;
	)
    done;
    for x = 0 to m - 1 do
      let y = 0 in
	if a.(y).(x) && c.(y).(x) < 0 then (
	  c.(y).(x) <- !color;
	  l1 := 0;
	  l2 := 0;
	  q1.(!l1) <- (x, y);
	  incr l1;
	  bfs_step 1 q1 l1 q2 l2;
	  incr color;
	)
    done;
    for x = 0 to m - 1 do
      let y = n - 1 in
	if a.(y).(x) && c.(y).(x) < 0 then (
	  c.(y).(x) <- !color;
	  l1 := 0;
	  l2 := 0;
	  q1.(!l1) <- (x, y);
	  incr l1;
	  bfs_step 1 q1 l1 q2 l2;
	  incr color;
	)
    done;
    let res = ref false in
    (*let () =
      for y = 0 to n - 1 do
	for x = 0 to m - 1 do
	  Printf.printf "%d " c.(y).(x)
	done;
	Printf.printf "\n"
      done
    in*)
    let es = Array.make !color [] in
    let () =
      for x = 0 to m - 1 do
	let y1 = 0
	and y2 = n - 1 in
	  if a.(y1).(x) && a.(y2).(x) then (
	    let c1 = c.(y1).(x)
	    and c2 = c.(y2).(x) in
	      es.(c1) <- (c2, 1, 0) :: es.(c1);
	      es.(c2) <- (c1, -1, 0) :: es.(c2);
	  )
      done;
      for y = 0 to n - 1 do
	let x1 = 0
	and x2 = m - 1 in
	  if a.(y).(x1) && a.(y).(x2) then (
	    let c1 = c.(y).(x1)
	    and c2 = c.(y).(x2) in
	      es.(c1) <- (c2, 0, 1) :: es.(c1);
	      es.(c2) <- (c1, 0, -1) :: es.(c2);
	  )
      done;
    in
    let es = Array.map Array.of_list es in
    (let used = Array.make !color (-1) in
    let start = c.(sy).(sx) in
    let rec dfs u z =
      if used.(u) >= 0 && used.(u) < z
      then raise Found;
      used.(u) <- z;
      for i = 0 to Array.length es.(u) - 1 do
	let (v, dz, _) = es.(u).(i) in
	  (*if v = start
	  then res := true;*)
	  if used.(v) < z + dz then (
	    dfs v (z + dz);
	  )
      done
    in
      try
	if start >= 0
	then dfs start 0;
      with
	| Found -> res := true
    );
    (let used = Array.make !color (-1) in
    let start = c.(sy).(sx) in
    let rec dfs u z =
      if used.(u) >= 0 && used.(u) < z
      then raise Found;
      used.(u) <- z;
      for i = 0 to Array.length es.(u) - 1 do
	let (v, _, dz) = es.(u).(i) in
	  (*if v = start
	  then res := true;*)
	  if used.(v) < z + dz then (
	    dfs v (z + dz);
	  )
      done
    in
      try
	if start >= 0
	then dfs start 0;
      with
	| Found -> res := true
    );
    (*let cc = c.(sy).(sx) in
      if cc >= 0 then (
	for x = 0 to m - 1 do
	  let y1 = 0
	  and y2 = n - 1 in
	    if a.(y1).(x) && a.(y2).(x) &&
	      c.(y1).(x) = cc && c.(y2).(x) = cc
	    then (
	      res := true
	    )
	done;
	for y = 0 to n - 1 do
	  let x1 = 0
	  and x2 = m - 1 in
	    if a.(y).(x1) && a.(y).(x2) &&
	      c.(y).(x1) = cc && c.(y).(x2) = cc
	    then (
	      res := true
	    )
	done;
      );*)
      if !res
      then Printf.printf "YES\n"
      else Printf.printf "NO\n"

exception Found

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let m = Scanf.bscanf sb "%d " (fun s -> s) in
  let a = Array.make_matrix n m false in
  let sx = ref 0 in
  let sy = ref 0 in
  let () =
    for i = 0 to n - 1 do
      let s = Scanf.bscanf sb "%s " (fun s -> s) in
	for j = 0 to m - 1 do
	  if s.[j] <> '#'
	  then a.(i).(j) <- true;
	  if s.[j] = 'S' then (
	    sy := i;
	    sx := j;
	  )
	done
    done;
  in
  let sx = !sx
  and sy = !sy in
  let c = Array.make_matrix n m (-1) in
  let color = ref 0 in
  let q1 = Array.make (n * m) (0, 0) in
  let l1 = ref 0 in
  let q2 = Array.make (n * m) (0, 0) in
  let l2 = ref 0 in
  let d = [| (-1, 0); (1, 0); (0, 1); (0, -1) |] in
  let rec bfs_step k q1 l1 q2 l2 =
    l2 := 0;
    for i = 0 to !l1 - 1 do
      let (x, y) = q1.(i) in
	for i = 0 to 3 do
	  let (dx, dy) = d.(i) in
	  let x = x + dx
	  and y = y + dy in
	  (*let x = (x + m) mod m in
	  let y = (y + n) mod n in*)
	    if x >= 0 && y >= 0 && x < m && y < n && a.(y).(x) then (
	      if c.(y).(x) < 0 then (
		c.(y).(x) <- !color;
		q2.(!l2) <- (x, y);
		incr l2
	      );
	    );
	done
    done;
    if !l2 > 0
    then bfs_step (k + 1) q2 l2 q1 l1
  in
    for y = 0 to n - 1 do
      let x = 0 in
	if a.(y).(x) && c.(y).(x) < 0 then (
	  c.(y).(x) <- !color;
	  l1 := 0;
	  l2 := 0;
	  q1.(!l1) <- (x, y);
	  incr l1;
	  bfs_step 1 q1 l1 q2 l2;
	  incr color;
	)
    done;
    for y = 0 to n - 1 do
      let x = m - 1 in
	if a.(y).(x) && c.(y).(x) < 0 then (
	  c.(y).(x) <- !color;
	  l1 := 0;
	  l2 := 0;
	  q1.(!l1) <- (x, y);
	  incr l1;
	  bfs_step 1 q1 l1 q2 l2;
	  incr color;
	)
    done;
    for x = 0 to m - 1 do
      let y = 0 in
	if a.(y).(x) && c.(y).(x) < 0 then (
	  c.(y).(x) <- !color;
	  l1 := 0;
	  l2 := 0;
	  q1.(!l1) <- (x, y);
	  incr l1;
	  bfs_step 1 q1 l1 q2 l2;
	  incr color;
	)
    done;
    for x = 0 to m - 1 do
      let y = n - 1 in
	if a.(y).(x) && c.(y).(x) < 0 then (
	  c.(y).(x) <- !color;
	  l1 := 0;
	  l2 := 0;
	  q1.(!l1) <- (x, y);
	  incr l1;
	  bfs_step 1 q1 l1 q2 l2;
	  incr color;
	)
    done;
    let res = ref false in
    (*let () =
      for y = 0 to n - 1 do
	for x = 0 to m - 1 do
	  Printf.printf "%d " c.(y).(x)
	done;
	Printf.printf "\n"
      done
    in*)
    let es = Array.make !color [] in
    let () =
      for x = 0 to m - 1 do
	let y1 = 0
	and y2 = n - 1 in
	  if a.(y1).(x) && a.(y2).(x) then (
	    let c1 = c.(y1).(x)
	    and c2 = c.(y2).(x) in
	      es.(c1) <- (c2, 1, 0) :: es.(c1);
	      es.(c2) <- (c1, -1, 0) :: es.(c2);
	  )
      done;
      for y = 0 to n - 1 do
	let x1 = 0
	and x2 = m - 1 in
	  if a.(y).(x1) && a.(y).(x2) then (
	    let c1 = c.(y).(x1)
	    and c2 = c.(y).(x2) in
	      es.(c1) <- (c2, 0, 1) :: es.(c1);
	      es.(c2) <- (c1, 0, -1) :: es.(c2);
	  )
      done;
    in
    let es = Array.map Array.of_list es in
    (let used = Array.make !color (-1) in
    let start = c.(sy).(sx) in
    let rec dfs u z =
      if used.(u) >= 0 && used.(u) < z
      then raise Found;
      used.(u) <- z;
      for i = 0 to Array.length es.(u) - 1 do
	let (v, dz, _) = es.(u).(i) in
	  (*if v = start
	  then res := true;*)
	  if used.(v) < z + dz then (
	    dfs v (z + dz);
	  )
      done
    in
      try
	if start >= 0
	then dfs start 0;
      with
	| Found -> res := true
    );
    (let used = Array.make !color (-1) in
    let start = c.(sy).(sx) in
    let rec dfs u z =
      if used.(u) >= 0 && used.(u) < z
      then raise Found;
      used.(u) <- z;
      for i = 0 to Array.length es.(u) - 1 do
	let (v, _, dz) = es.(u).(i) in
	  (*if v = start
	  then res := true;*)
	  if used.(v) < z + dz then (
	    dfs v (z + dz);
	  )
      done
    in
      try
	if start >= 0
	then dfs start 0;
      with
	| Found -> res := true
    );
    (*let cc = c.(sy).(sx) in
      if cc >= 0 then (
	for x = 0 to m - 1 do
	  let y1 = 0
	  and y2 = n - 1 in
	    if a.(y1).(x) && a.(y2).(x) &&
	      c.(y1).(x) = cc && c.(y2).(x) = cc
	    then (
	      res := true
	    )
	done;
	for y = 0 to n - 1 do
	  let x1 = 0
	  and x2 = m - 1 in
	    if a.(y).(x1) && a.(y).(x2) &&
	      c.(y).(x1) = cc && c.(y).(x2) = cc
	    then (
	      res := true
	    )
	done;
      );*)
      if !res
      then Printf.printf "YES\n"
      else Printf.printf "NO\n"

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let m = Scanf.bscanf sb "%d " (fun s -> s) in
  let a = Array.make_matrix n m false in
  let sx = ref 0 in
  let sy = ref 0 in
  let () =
    for i = 0 to n - 1 do
      let s = Scanf.bscanf sb "%s " (fun s -> s) in
	for j = 0 to m - 1 do
	  if s.[j] <> '#'
	  then a.(i).(j) <- true;
	  if s.[j] = 'S' then (
	    sy := i;
	    sx := j;
	  )
	done
    done;
  in
  let sx = !sx
  and sy = !sy in
  let c = Array.make_matrix n m (-1) in
  let color = ref 0 in
  let q1 = Array.make (n * m) (0, 0) in
  let l1 = ref 0 in
  let q2 = Array.make (n * m) (0, 0) in
  let l2 = ref 0 in
  let d = [| (-1, 0); (1, 0); (0, 1); (0, -1) |] in
  let rec bfs_step k q1 l1 q2 l2 =
    l2 := 0;
    for i = 0 to !l1 - 1 do
      let (x, y) = q1.(i) in
	for i = 0 to 3 do
	  let (dx, dy) = d.(i) in
	  let x = x + dx
	  and y = y + dy in
	    if x >= 0 && y >= 0 && x < m && y < n && a.(y).(x) then (
	      if c.(y).(x) < 0 then (
		c.(y).(x) <- !color;
		q2.(!l2) <- (x, y);
		incr l2
	      );
	    );
	done
    done;
    if !l2 > 0
    then bfs_step (k + 1) q2 l2 q1 l1
  in
    for y = 0 to n - 1 do
      let x = 0 in
	if a.(y).(x) && c.(y).(x) < 0 then (
	  c.(y).(x) <- !color;
	  l1 := 0;
	  l2 := 0;
	  q1.(!l1) <- (x, y);
	  incr l1;
	  bfs_step 1 q1 l1 q2 l2;
	  incr color;
	)
    done;
    for y = 0 to n - 1 do
      let x = m - 1 in
	if a.(y).(x) && c.(y).(x) < 0 then (
	  c.(y).(x) <- !color;
	  l1 := 0;
	  l2 := 0;
	  q1.(!l1) <- (x, y);
	  incr l1;
	  bfs_step 1 q1 l1 q2 l2;
	  incr color;
	)
    done;
    for x = 0 to m - 1 do
      let y = 0 in
	if a.(y).(x) && c.(y).(x) < 0 then (
	  c.(y).(x) <- !color;
	  l1 := 0;
	  l2 := 0;
	  q1.(!l1) <- (x, y);
	  incr l1;
	  bfs_step 1 q1 l1 q2 l2;
	  incr color;
	)
    done;
    for x = 0 to m - 1 do
      let y = n - 1 in
	if a.(y).(x) && c.(y).(x) < 0 then (
	  c.(y).(x) <- !color;
	  l1 := 0;
	  l2 := 0;
	  q1.(!l1) <- (x, y);
	  incr l1;
	  bfs_step 1 q1 l1 q2 l2;
	  incr color;
	)
    done;
    (*let () =
      for y = 0 to n - 1 do
	for x = 0 to m - 1 do
	  Printf.printf "%d " c.(y).(x)
	done;
	Printf.printf "\n"
      done
    in*)
    let es = Array.make !color [] in
    let () =
      for x = 0 to m - 1 do
	let y1 = 0
	and y2 = n - 1 in
	  if a.(y1).(x) && a.(y2).(x) then (
	    let c1 = c.(y1).(x)
	    and c2 = c.(y2).(x) in
	      es.(c1) <- c2 :: es.(c1);
	      es.(c2) <- c1 :: es.(c2);
	  )
      done;
      for y = 0 to n - 1 do
	let x1 = 0
	and x2 = m - 1 in
	  if a.(y).(x1) && a.(y).(x2) then (
	    let c1 = c.(y).(x1)
	    and c2 = c.(y).(x2) in
	      es.(c1) <- c2 :: es.(c1);
	      es.(c2) <- c1 :: es.(c2);
	  )
      done;
    in
    let es = Array.map Array.of_list es in
    let used = Array.make !color false in
    let res = ref false in
    let start = c.(sy).(sx) in
    let rec dfs u =
      used.(u) <- true;
      for i = 0 to Array.length es.(u) - 1 do
	let v = es.(u).(i) in
	  if v = start
	  then res := true;
	  if not used.(v) then (
	    dfs v;
	  )
      done
    in
      if start >= 0
      then dfs start;
      if !res
      then Printf.printf "YES\n"
      else Printf.printf "NO\n"

exception Found

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let m = Scanf.bscanf sb "%d " (fun s -> s) in
  let a = Array.make_matrix n m false in
  let sx = ref 0 in
  let sy = ref 0 in
  let () =
    for i = 0 to n - 1 do
      let s = Scanf.bscanf sb "%s " (fun s -> s) in
	for j = 0 to m - 1 do
	  if s.[j] <> '#'
	  then a.(i).(j) <- true;
	  if s.[j] = 'S' then (
	    sy := i;
	    sx := j;
	  )
	done
    done;
  in
  let sx = !sx
  and sy = !sy in
  let c = Array.make_matrix n m (-1) in
  let color = ref 0 in
  let q1 = Array.make (n * m) (0, 0) in
  let l1 = ref 0 in
  let q2 = Array.make (n * m) (0, 0) in
  let l2 = ref 0 in
  let d = [| (-1, 0); (1, 0); (0, 1); (0, -1) |] in
  let rec bfs_step k q1 l1 q2 l2 =
    l2 := 0;
    for i = 0 to !l1 - 1 do
      let (x, y) = q1.(i) in
	for i = 0 to 3 do
	  let (dx, dy) = d.(i) in
	  let x = x + dx
	  and y = y + dy in
	  (*let x = (x + m) mod m in
	  let y = (y + n) mod n in*)
	    if x >= 0 && y >= 0 && x < m && y < n && a.(y).(x) then (
	      if c.(y).(x) < 0 then (
		c.(y).(x) <- !color;
		q2.(!l2) <- (x, y);
		incr l2
	      );
	    );
	done
    done;
    if !l2 > 0
    then bfs_step (k + 1) q2 l2 q1 l1
  in
    for y = 0 to n - 1 do
      let x = 0 in
	if a.(y).(x) && c.(y).(x) < 0 then (
	  c.(y).(x) <- !color;
	  l1 := 0;
	  l2 := 0;
	  q1.(!l1) <- (x, y);
	  incr l1;
	  bfs_step 1 q1 l1 q2 l2;
	  incr color;
	)
    done;
    for y = 0 to n - 1 do
      let x = m - 1 in
	if a.(y).(x) && c.(y).(x) < 0 then (
	  c.(y).(x) <- !color;
	  l1 := 0;
	  l2 := 0;
	  q1.(!l1) <- (x, y);
	  incr l1;
	  bfs_step 1 q1 l1 q2 l2;
	  incr color;
	)
    done;
    for x = 0 to m - 1 do
      let y = 0 in
	if a.(y).(x) && c.(y).(x) < 0 then (
	  c.(y).(x) <- !color;
	  l1 := 0;
	  l2 := 0;
	  q1.(!l1) <- (x, y);
	  incr l1;
	  bfs_step 1 q1 l1 q2 l2;
	  incr color;
	)
    done;
    for x = 0 to m - 1 do
      let y = n - 1 in
	if a.(y).(x) && c.(y).(x) < 0 then (
	  c.(y).(x) <- !color;
	  l1 := 0;
	  l2 := 0;
	  q1.(!l1) <- (x, y);
	  incr l1;
	  bfs_step 1 q1 l1 q2 l2;
	  incr color;
	)
    done;
    let res = ref false in
    (*let () =
      for y = 0 to n - 1 do
	for x = 0 to m - 1 do
	  Printf.printf "%2d " c.(y).(x)
	done;
	Printf.printf "\n"
      done
    in*)
    let es = Array.make !color [] in
    let () =
      for x = 0 to m - 1 do
	let y1 = 0
	and y2 = n - 1 in
	  if a.(y1).(x) && a.(y2).(x) then (
	    let c1 = c.(y1).(x)
	    and c2 = c.(y2).(x) in
	      es.(c1) <- (c2, 1, 0) :: es.(c1);
	      es.(c2) <- (c1, -1, 0) :: es.(c2);
	  )
      done;
      for y = 0 to n - 1 do
	let x1 = 0
	and x2 = m - 1 in
	  if a.(y).(x1) && a.(y).(x2) then (
	    let c1 = c.(y).(x1)
	    and c2 = c.(y).(x2) in
	      es.(c1) <- (c2, 0, 1) :: es.(c1);
	      es.(c2) <- (c1, 0, -1) :: es.(c2);
	  )
      done;
    in
    let es = Array.map Array.of_list es in
    (let used = Array.make !color (-1) in
    let start = c.(sy).(sx) in
    let rec dfs u z =
      if used.(u) >= 0 && used.(u) < z
      then raise Found;
      used.(u) <- z;
      for i = 0 to Array.length es.(u) - 1 do
	let (v, dz, _) = es.(u).(i) in
	  (*if v = start
	  then res := true;*)
	  if used.(v) < z + dz then (
	    dfs v (z + dz);
	  )
      done
    in
      try
	if start >= 0
	then dfs start 0;
      with
	| Found -> res := true
    );
    (let used = Array.make !color false in
     let dist = Array.make !color 0 in
     let start = c.(sy).(sx) in
     let rec dfs u z =
       if used.(u) && dist.(u) <> z
       then raise Found;
       dist.(u) <- z;
       used.(u) <- true;
       for i = 0 to Array.length es.(u) - 1 do
	 let (v, _, dz) = es.(u).(i) in
	   (*if v = start
	     then res := true;*)
	   if not used.(v) || dist.(v) <> z + dz then (
	     dfs v (z + dz);
	   )
       done
     in
       try
	 if start >= 0
	 then dfs start 0;
       with
	 | Found -> res := true
    );
    (*let cc = c.(sy).(sx) in
      if cc >= 0 then (
	for x = 0 to m - 1 do
	  let y1 = 0
	  and y2 = n - 1 in
	    if a.(y1).(x) && a.(y2).(x) &&
	      c.(y1).(x) = cc && c.(y2).(x) = cc
	    then (
	      res := true
	    )
	done;
	for y = 0 to n - 1 do
	  let x1 = 0
	  and x2 = m - 1 in
	    if a.(y).(x1) && a.(y).(x2) &&
	      c.(y).(x1) = cc && c.(y).(x2) = cc
	    then (
	      res := true
	    )
	done;
      );*)
      if !res
      then Printf.printf "YES\n"
      else Printf.printf "NO\n"