On the way to school, Karen became fixated on the puzzle game on her phone!  The game is played as follows. In each level, you have a grid with n rows and m columns. Each cell originally contains the number 0.One move consists of choosing one row or column, and adding 1 to all of the cells in that row or column.To win the level, after all the moves, the number in the cell at the i-th row and j-th column should be equal to gi, j.Karen is stuck on one level, and wants to know a way to beat this level using the minimum number of moves. Please, help her with this task!

The first line of input contains two integers, n and m (1 ≤ n, m ≤ 100), the number of rows and the number of columns in the grid, respectively. The next n lines each contain m integers. In particular, the j-th integer in the i-th of these rows contains gi, j (0 ≤ gi, j ≤ 500).

If there is an error and it is actually not possible to beat the level, output a single integer -1. Otherwise, on the first line, output a single integer k, the minimum number of moves necessary to beat the level. The next k lines should each contain one of the following, describing the moves in the order they must be done:   row x, (1 ≤ x ≤ n) describing a move of the form "choose the x-th row".  col x, (1 ≤ x ≤ m) describing a move of the form "choose the x-th column".  If there are multiple optimal solutions, output any one of them.

NoteIn the first test case, Karen has a grid with 3 rows and 5 columns. She can perform the following 4 moves to beat the level:  In the second test case, Karen has a grid with 3 rows and 3 columns. It is clear that it is impossible to beat the level; performing any move will create three 1s on the grid, but it is required to only have one 1 in the center.In the third test case, Karen has a grid with 3 rows and 3 columns. She can perform the following 3 moves to beat the level:  Note that this is not the only solution; another solution, among others, is col 1, col 2, col 3.

open Scanf;;
scanf "%d %d\n" (fun n m ->
  let mat=Array.make_matrix n m 0 in
  let set i j x= mat.(i).(j) <- x in
  for i=0 to n-1 do
    for j=0 to m-2 do
      scanf "%d " (set i j)
    done;
    scanf "%d\n" (set i (m-1))
  done;
  let all_N' = List.fold_left (fun f x->f&&x>0) true in
  let rec r_to_list x j=
    if j=m then [] else mat.(x).(j) :: r_to_list x (j+1) in
  let rec c_to_list y i=
    if i=n then [] else mat.(i).(y) :: c_to_list y (i+1) in
  let test_r r=r_to_list r 0 |> all_N' in
  let test_c c=c_to_list c 0 |> all_N' in
  let do_r r=
    for j=0 to m-1 do
      mat.(r).(j)<-mat.(r).(j)-1
    done; "row "^(string_of_int (r+1)) in
  let do_c c=
    for i=0 to n-1 do
      mat.(i).(c)<-mat.(i).(c)-1
    done; "col "^(string_of_int (c+1)) in
  let solve() =
    let rec solve_r r acc=match test_r r with
    | true -> solve_r r (do_r r ::acc)
    | false -> if r=n-1 then acc else solve_r (r+1) acc in
    let rec solve_c c acc=match test_c c with
    | true -> solve_c c (do_c c ::acc)
    | false -> if c=m-1 then acc else solve_c (c+1) acc in
    if n>m then solve_r 0 [] @ solve_c 0 [] else solve_c 0 [] @ solve_r 0 [] in
  let flag_N'=ref false in
  let test_N'() =
    for i=0 to n-1 do
      for j=0 to m-1 do
        if mat.(i).(j)>0 then flag_N':=true
      done
    done in
  let write l=
    let rec print_list =function
    | [] -> ()
    | h :: t -> print_endline h;print_list t in
    print_int (List.length l);
    print_newline();
    print_list l in
  let ans=solve() in
    test_N'();
    if !flag_N' then print_endline "-1" else write ans
)

open Scanf;;
scanf "%d %d" (fun n m ->
  let mat=Array.make_matrix n m 0 in
  for i=0 to n-1 do
    for j=0 to m-1 do
      scanf " %d" (fun x->mat.(i).(j) <- x)
    done;
  done;
  let all_N' =List.for_all (fun x->x>0) in
  let rec r_to_list x j=
    if j=m then [] else mat.(x).(j) :: r_to_list x (j+1) in
  let rec c_to_list y i=
    if i=n then [] else mat.(i).(y) :: c_to_list y (i+1) in
  let test_r r=r_to_list r 0 |> all_N' and test_c c=c_to_list c 0 |> all_N' in
  let dec i j = mat.(i).(j)<-mat.(i).(j)-1 in
  let do_r r=
    for j=0 to m-1 do
      dec r j
    done; "row "^(string_of_int (r+1))
  and do_c c=
    for i=0 to n-1 do
      dec i c
    done; "col "^(string_of_int (c+1)) in
  let solve() =
    let rec solve_r r acc=match test_r r with
    | true -> solve_r r (do_r r ::acc)
    | false -> if r=n-1 then acc else solve_r (r+1) acc
    and solve_c c acc=match test_c c with
    | true -> solve_c c (do_c c ::acc)
    | false -> if c=m-1 then acc else solve_c (c+1) acc in
    if n<m then let rl=solve_r 0 [] in let cl= solve_c 0 [] in rl@cl
      else let cl=solve_c 0 [] in let rl= solve_r 0 [] in rl@cl in
  let flag_N'=ref false in
  let test_N'() =
    for i=0 to n-1 do
      for j=0 to m-1 do
        if mat.(i).(j)>0 then flag_N':=true
      done
    done in
  let write l=
    let rec print_list =function
    | [] -> ()
    | h :: t -> print_endline h;print_list t in
    print_int (List.length l);
    print_newline();
    print_list l in
  let ans=solve() in
    test_N'();
    if !flag_N' then print_endline "-1" else write ans
)

open Scanf;;
scanf "%d %d\n" (fun n m ->
  let mat=Array.make_matrix n m 0 in
  let set i j x= mat.(i).(j) <- x in
  for i=0 to n-1 do
    for j=0 to m-2 do
      scanf "%d " (set i j)
    done;
    scanf "%d\n" (set i (m-1))
  done;
  let has_N' = List.fold_left (fun f x->f&&x>0) true in
  let rec r_to_list x j=
    if j=m-1 then [] else mat.(x).(j) :: r_to_list x (j+1) in
  let rec c_to_list y i=
    if i=n-1 then [] else mat.(i).(y) :: c_to_list y (i+1) in
  let test_r r=r_to_list r 0 |> has_N' in
  let test_c c=c_to_list c 0 |> has_N' in
  let do_r r=
    for j=0 to m-1 do
      mat.(r).(j)<-mat.(r).(j)-1
    done; "row "^(string_of_int (r+1)) in
  let do_c c=
    for i=0 to n-1 do
      mat.(i).(c)<-mat.(i).(c)-1
    done; "col "^(string_of_int (c+1)) in
  let do_point_rf r c =match test_r r, test_c c with
    | true ,_ -> do_r r
    | _ , true -> do_c c
    | _  ->raise Not_found  in
  let do_point_cf r c =match test_r r, test_c c with
    | _ , true -> do_c c
    | true ,_ -> do_r r
    | _  ->raise Not_found  in
  let do_point r c = if r<c then do_point_rf r c else do_point_cf r c in
  let next =function
    |r,c when r=n-1 && c=m-1 -> (-1,-1)
    |r,c when c=m-1 -> (r+1,0)
    |r,c -> (r,c+1) in
  let test_point r c = mat.(r).(c)>0 in
  let solve() =
    let rec solve_inn r c acc=match test_point r c with
    | true -> solve_inn r c (do_point r c ::acc)
    | false ->
      match next (r,c) with
      | -1,-1 ->acc
      | nr,nc ->solve_inn nr nc acc  in solve_inn 0 0 [] in
  let write l=
    let rec print_list =function
    | [] -> ()
    | h :: t -> print_endline h;print_list t in
    print_int (List.length l);
    print_newline();
    print_list l in
  try write (solve()) with
    Not_found -> print_endline "-1"
)

open Scanf;;
scanf "%d %d\n" (fun n m ->
  let mat=Array.make_matrix n m 0 in
  let set i j x= mat.(i).(j) <- x in
  for i=0 to n-1 do
    for j=0 to m-2 do
      scanf "%d " (set i j)
    done;
    scanf "%d\n" (set i (m-1))
  done;
  let all_N' = List.fold_left (fun f x->f&&x>0) true in
  let rec r_to_list x j=
    if j=m then [] else mat.(x).(j) :: r_to_list x (j+1) in
  let rec c_to_list y i=
    if i=n then [] else mat.(i).(y) :: c_to_list y (i+1) in
  let test_r r=r_to_list r 0 |> all_N' in
  let test_c c=c_to_list c 0 |> all_N' in
  let do_r r=
    for j=0 to m-1 do
      mat.(r).(j)<-mat.(r).(j)-1
    done; "row "^(string_of_int (r+1)) in
  let do_c c=
    for i=0 to n-1 do
      mat.(i).(c)<-mat.(i).(c)-1
    done; "col "^(string_of_int (c+1)) in
  let solve() =
    let rec solve_r r acc=match test_r r with
    | true -> solve_r r (do_r r ::acc)
    | false -> if r=n-1 then acc else solve_r (r+1) acc in
    let rec solve_c c acc=match test_c c with
    | true -> solve_c c (do_c c ::acc)
    | false -> if c=m-1 then acc else solve_c (c+1) acc in
    if n<m then solve_r 0 [] @ solve_c 0 [] else solve_c 0 [] @ solve_r 0 [] in
  let flag_N'=ref false in
  let test_N'() =
    for i=0 to n-1 do
      for j=0 to m-1 do
        if mat.(i).(j)>0 then flag_N':=true
      done
    done in
  let write l=
    let rec print_list =function
    | [] -> ()
    | h :: t -> print_endline h;print_list t in
    print_int (List.length l);
    print_newline();
    print_list l in
  let ans=solve() in
    test_N'();
    if !flag_N' then print_endline "-1" else write ans
)

open Scanf;;
scanf "%d %d\n" (fun n m ->
  let mat=Array.make_matrix n m 0 in
  let set i j x= mat.(i).(j) <- x in
  for i=0 to n-1 do
    for j=0 to m-2 do
      scanf "%d " (set i j)
    done;
    scanf "%d\n" (set i (m-1))
  done;
  let has_N' = List.fold_left (fun f x->f&&x>0) true in
  let rec r_to_list x j=
    if j=m-1 then [] else mat.(x).(j) :: r_to_list x (j+1) in
  let rec c_to_list y i=
    if i=n-1 then [] else mat.(i).(y) :: c_to_list y (i+1) in
  let test_r r=r_to_list r 0 |> has_N' in
  let test_c c=c_to_list c 0 |> has_N' in
  let do_r r=
    for j=0 to m-1 do
      mat.(r).(j)<-mat.(r).(j)-1
    done; "row "^(string_of_int (r+1)) in
  let do_c c=
    for i=0 to n-1 do
      mat.(i).(c)<-mat.(i).(c)-1
    done; "col "^(string_of_int (c+1)) in
  let do_point r c =match test_r r, test_c c with
    | true ,_ -> do_r r
    | _ , true -> do_c c
    | _  ->raise Not_found  in
  let next =function
    |r,c when r=n-1 && c=m-1 -> (-1,-1)
    |r,c when c=m-1 -> (r+1,0)
    |r,c -> (r,c+1) in
  let test_point r c = mat.(r).(c)>0 in
  let solve() =
    let rec solve_inn r c acc=match test_point r c with
    | true -> solve_inn r c (do_point r c ::acc)
    | false ->
      match next (r,c) with
      | -1,-1 ->acc
      | nr,nc ->solve_inn nr nc acc  in solve_inn 0 0 [] in
  let write l=
    let rec print_list =function
    | [] -> ()
    | h :: t -> print_endline h;print_list t in
    print_int (List.length l);
    print_newline();
    print_list l in
  try write (solve()) with
    Not_found -> print_endline "-1"
)


User ainta has a permutation p1, p2, ..., pn. As the New Year is coming, he wants to make his permutation as pretty as possible.Permutation a1, a2, ..., an is prettier than permutation b1, b2, ..., bn, if and only if there exists an integer k (1 ≤ k ≤ n) where a1 = b1, a2 = b2, ..., ak - 1 = bk - 1 and ak < bk all holds.As known, permutation p is so sensitive that it could be only modified by swapping two distinct elements. But swapping two elements is harder than you think. Given an n × n binary matrix A, user ainta can swap the values of pi and pj (1 ≤ i, j ≤ n, i ≠ j) if and only if Ai, j = 1.Given the permutation p and the matrix A, user ainta wants to know the prettiest permutation that he can obtain.

The first line contains an integer n (1 ≤ n ≤ 300) — the size of the permutation p. The second line contains n space-separated integers p1, p2, ..., pn — the permutation p that user ainta has. Each integer between 1 and n occurs exactly once in the given permutation. Next n lines describe the matrix A. The i-th line contains n characters '0' or '1' and describes the i-th row of A. The j-th character of the i-th line Ai, j is the element on the intersection of the i-th row and the j-th column of A. It is guaranteed that, for all integers i, j where 1 ≤ i < j ≤ n, Ai, j = Aj, i holds. Also, for all integers i where 1 ≤ i ≤ n, Ai, i = 0 holds.

In the first and only line, print n space-separated integers, describing the prettiest permutation that can be obtained.

NoteIn the first sample, the swap needed to obtain the prettiest permutation is: (p1, p7).In the second sample, the swaps needed to obtain the prettiest permutation is (p1, p3), (p4, p5), (p3, p4).   A permutation p is a sequence of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. The i-th element of the permutation p is denoted as pi. The size of the permutation p is denoted as n.

open Printf
open Scanf

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

let () =
  let n = read_int () in
  let p = Array.init n (fun _ -> read_int()) in
  let a = Array.make n "" in

  for i=0 to n-1 do
    a.(i) <- read_string()
  done;

  let s = Array.make n (-1) in  (* the sets *)
  let size = Array.make n 1 in  (* sizes of the sets *)

  let rec find i = if s.(i) < 0 then i else
    let r = find s.(i) in
      s.(i) <- r;
      r
  in

  let union i j =
    let (i,j) = ((find i), (find j)) in
      if i <> j then (
	let (i,j) = if size.(i) < size.(j) then (i,j) else (j,i) in
	s.(i) <- j; (* union by rank *)
	size.(j) <- size.(i) + size.(j);
      )
  in

  for i=0 to n-1 do
    for j=0 to n-1 do
      if a.(i).[j] = '1' then union i j
    done
  done;

  let sets = Array.make n [] in

  for i=0 to n-1 do
    let j = find i in
    sets.(j) <- p.(i)::sets.(j)
  done;

  for j=0 to n-1 do
    sets.(j) <- List.sort compare sets.(j)
  done;

  for i=0 to n-1 do
    let j = find i in
    printf "%d " (List.hd sets.(j));
    sets.(j) <- List.tl sets.(j)
  done;

  print_newline()


A company has n employees numbered from 1 to n. Each employee either has no immediate manager or exactly one immediate manager, who is another employee with a different number. An employee A is said to be the superior of another employee B if at least one of the following is true:  Employee A is the immediate manager of employee B  Employee B has an immediate manager employee C such that employee A is the superior of employee C. The company will not have a managerial cycle. That is, there will not exist an employee who is the superior of his/her own immediate manager.Today the company is going to arrange a party. This involves dividing all n employees into several groups: every employee must belong to exactly one group. Furthermore, within any single group, there must not be two employees A and B such that A is the superior of B.What is the minimum number of groups that must be formed?

The first line contains integer n (1 ≤ n ≤ 2000) — the number of employees. The next n lines contain the integers pi (1 ≤ pi ≤ n or pi = -1). Every pi denotes the immediate manager for the i-th employee. If pi is -1, that means that the i-th employee does not have an immediate manager.  It is guaranteed, that no employee will be the immediate manager of him/herself (pi ≠ i). Also, there will be no managerial cycles.

Print a single integer denoting the minimum number of groups that will be formed in the party.

NoteFor the first example, three groups are sufficient, for example:   Employee 1  Employees 2 and 4  Employees 3 and 5

type tree = Leaf of int
          | Node of tree list

let rec makeList numList num =
  match numList with
  | h :: t -> (num, h) :: (makeList t (num+1))
  | _ -> []

let rec findOrigin numList =
  match numList with
  | (h, n) :: t -> if (n == -1) then h :: (findOrigin t)
                    else findOrigin t
  | _ -> []

let rec findNum numList num =
  match numList with
  | (h, n) :: t -> if (n == num) then h :: (findNum t num)
                    else findNum t num
  | _ -> []

let makeTree numList =
  let originList = findOrigin numList in
  let rec makeList nList =
    match nList with
    | h :: t ->
      let newList = findNum numList h in
      Node(Leaf(h) :: makeList newList) :: (makeList t)
    | _ -> [] in
  makeList originList

let nodeCount tree =
  let rec node tree count =
    match tree with
    | Node(h::t) :: tail ->
      let c = node (h::t) (count+1) in
      c @ (node tail count)
    | Leaf(h) :: t ->
      node t count
    | _ -> count ::[] in
  let countList = node tree 1 in
  let rec maxCount cList max =
    match cList with
    | h :: t -> if (max > h) then maxCount t max
                else maxCount t h
    | _ -> max in
  maxCount countList 1

let printLeaf leaf =
  match leaf with
  | Leaf(h) -> let _ = print_string ("Leaf ") in
    let _ = print_int h in
    print_string " "
  | _ -> print_string " "

let rec printNode node =
  match node with
  | Node(h :: t) :: tail -> let _ = print_string ("[Node ") in
    let _ = printLeaf h in
    let _ = printNode t in
    let _ = print_string "] " in
    printNode tail
  | Leaf(h) :: tail -> let _ = printLeaf (Leaf(h)) in
    printNode tail
  | _ -> print_string " "

let num = read_int()

let rec inputList num =
  if (num != 0) then
    let k = read_int() in
    k :: (inputList (num-1))
  else
    []
let numList = makeList (inputList num) 1
let k = makeTree numList
(*let numList = makeList (-1::1::2::3::2::5::1::[]) 1

let k = makeTree numList

let _ = printNode k

let _ = print_string"\n\n"
*)
let _ = print_int((nodeCount k)-1)

open Scanf
open Printf

let n = Scanf.scanf "%d " ( fun x -> x )

type 'a employee = 'a * 'a list (*int is employee's immediate manager; int list are her subordinates*)

let a = Array.make (n+1) (0,[]);;

let manager (z:int employee) =
  match z with
  | (i,_) -> i

let subo (z:int employee) =
  match z with
  | (_,il) -> il

let adds (t:int employee) x =
  match t with
  (_,z) -> x::z

for i = 1 to n do
  let j = Scanf.scanf "%d " ( fun x -> x ) in
  if j = -1 then
    begin
      let il = subo a.(i) in
      let i2 = adds a.(0) i in
      a.(i) <- (manager a.(i),il); a.(0) <- (manager a.(0),i2)
    end
  else
    begin
      a.(i) <- (j,subo a.(i)); a.(j) <- (manager a.(j), adds a.(j) i)
    end
done

(*
let printe (z:int employee) =
  match z with
  | (i,il) -> printf "%d " i; List.iter (fun x -> printf "%d " x) il

let () = printe a.(11)

let () = print_endline ""
*)

let rec height (z:int employee) =
  match z with
  |(_,[]) -> 1
  |(_,il) ->
      List.fold_right (fun x y -> max x y)
      ( List.map (fun i -> 1 + (height a.(i))) il) (-1)
in
let m = height a.(0) in
printf "%d\n" (m-1)

type tree = Leaf of int
          | Node of tree list

let rec makeList numList num =
  match numList with
  | h :: t -> (num, h) :: (makeList t (num+1))
  | _ -> []

let rec findOrigin numList =
  match numList with
  | (h, n) :: t -> if (n == -1) then h :: (findOrigin t)
                    else findOrigin t
  | _ -> []

let rec findNum numList num =
  match numList with
  | (h, n) :: t -> if (n == num) then h :: (findNum t num)
                    else findNum t num
  | _ -> []

let makeTree numList =
  let originList = findOrigin numList in
  let rec makeList nList =
    match nList with
    | h :: t ->
      let newList = findNum numList h in
      Node(Leaf(h) :: makeList newList) :: (makeList t)
    | _ -> [] in
  makeList originList

let nodeCount tree =
  let rec node tree count =
    match tree with
    | Node(h::t) :: tail ->
      let c = node (h::t) (count+1) in
      c @ (node tail count)
    | Leaf(h) :: t ->
      node t count
    | _ -> count ::[] in
  let countList = node tree 1 in
  let rec maxCount cList max =
    match cList with
    | h :: t -> if (max > h) then maxCount t max
                else maxCount t h
    | _ -> max in
  let rec printList m =
    match m with
    | h :: t -> let _ = print_int h in
    let _ = print_string " " in
      printList t
    | _ -> print_string " \n\n" in
  let _ = printList countList in
  maxCount countList 1

let printLeaf leaf =
  match leaf with
  | Leaf(h) -> let _ = print_string ("Leaf ") in
    let _ = print_int h in
    print_string " "
  | _ -> print_string " "

let rec printNode node =
  match node with
  | Node(h :: t) :: tail -> let _ = print_string ("[Node ") in
    let _ = printLeaf h in
    let _ = printNode t in
    let _ = print_string "] " in
    printNode tail
  | Leaf(h) :: tail -> let _ = printLeaf (Leaf(h)) in
    printNode tail
  | _ -> print_string " "

let num = read_int()

let rec inputList num =
  if (num != 0) then
    let k = read_int() in
    k :: (inputList (num-1))
  else
    []
let numList = makeList (inputList num) 1
let k = makeTree numList
(*let numList = makeList (-1::1::2::3::2::5::1::[]) 1

let k = makeTree numList

let _ = printNode k

let _ = print_string"\n\n"
*)
let _ = print_int((nodeCount k)-1)

type tree = Leaf of int
          | Node of tree list

let rec makeList numList num =
  match numList with
  | h :: t -> (num, h) :: (makeList t (num+1))
  | _ -> []

let rec findOrigin numList =
  match numList with
  | (h, n) :: t -> if (n == -1) then h :: (findOrigin t)
                    else findOrigin t
  | _ -> []

let rec findNum numList num =
  match numList with
  | (h, n) :: t -> if (n == num) then h :: (findNum t num)
                    else findNum t num
  | _ -> []

let makeTree numList =
  let originList = findOrigin numList in
  let rec makeList nList =
    match nList with
    | h :: t ->
      let newList = findNum numList h in
      Node(Leaf(h) :: makeList newList) :: (makeList t)
    | _ -> [] in
  makeList originList

let nodeCount tree =
  let rec node tree count =
    match tree with
    | Node(h::t) :: tail ->
      let c = node t count in
      c @ (node t (count+1))
    | _ -> count ::[] in
  let countList = node tree 1 in
  let rec maxCount cList max =
    match cList with
    | h :: t -> if (max > h) then maxCount t max
                else maxCount t h
    | _ -> max in
  maxCount countList 1

let printLeaf leaf =
  match leaf with
  | Leaf(h) -> let _ = print_string ("Leaf ") in
    let _ = print_int h in
    print_string " "
  | _ -> print_string " "

let rec printNode node =
  match node with
  | Node(h :: t) :: tail -> let _ = print_string ("[Node ") in
    let _ = printLeaf h in
    let _ = printNode t in
    let _ = print_string "] " in
    printNode tail
  | Leaf(h) :: tail -> let _ = printLeaf (Leaf(h)) in
    printNode tail
  | _ -> print_string " "

let num = read_int()

let rec inputList num =
  if (num != 0) then
    let k = read_int() in
    k :: (inputList (num-1))
  else
    []
let numList = makeList (inputList num) 1
let k = makeTree numList

(*let numList = makeList (-1::1::2::3::2::5::1::[]) 1

let k = makeTree numList

let _ = printNode k

let _ = print_string"\n\n"
*)
let _ = print_int((nodeCount k)-1)

open Scanf
open Printf

let n = Scanf.scanf "%d " ( fun x -> x )

type 'a employee = 'a * 'a list (*int is employee's immediate manager; int list are her subordinates*)

let a = Array.make (n+1) (0,[]);;

let manager (z:int employee) =
  match z with
  | (i,_) -> i

let subo (z:int employee) =
  match z with
  | (_,il) -> il

let adds (t:int employee) x =
  match t with
  (_,z) -> x::z

for i = 1 to n do
  let j = Scanf.scanf "%d " ( fun x -> x ) in
  if j = -1 then
    begin
      let il = subo a.(i) in
      let i2 = adds a.(0) i in
      a.(i) <- (0,il); a.(0) <- (0,i2)
    end
  else
    begin
      a.(i) <- (j,[]); a.(j) <- (manager a.(j), adds a.(j) i)
    end
done

(*
let printe (z:int employee) =
  match z with
  | (i,il) -> printf "%d " i; List.iter (fun x -> printf "%d " x) il

let () = printe a.(0)
*)

let rec height (z:int employee) =
  match z with
  |(_,[]) -> 1
  |(_,il) ->
      List.fold_right (fun x y -> max x y)
      ( List.map (fun i -> 1 + (height a.(i))) il) (-1)
in
let m = height a.(0) in
printf "%d\n" (m-1)


For a vector $$$\vec{v} = (x, y)$$$, define $$$|v| = \sqrt{x^2 + y^2}$$$.Allen had a bit too much to drink at the bar, which is at the origin. There are $$$n$$$ vectors $$$\vec{v_1}, \vec{v_2}, \cdots, \vec{v_n}$$$. Allen will make $$$n$$$ moves. As Allen's sense of direction is impaired, during the $$$i$$$-th move he will either move in the direction $$$\vec{v_i}$$$ or $$$-\vec{v_i}$$$. In other words, if his position is currently $$$p = (x, y)$$$, he will either move to $$$p + \vec{v_i}$$$ or $$$p - \vec{v_i}$$$.Allen doesn't want to wander too far from home (which happens to also be the bar). You need to help him figure out a sequence of moves (a sequence of signs for the vectors) such that his final position $$$p$$$ satisfies $$$|p| \le 1.5 \cdot 10^6$$$ so that he can stay safe.

The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of moves. Each of the following lines contains two space-separated integers $$$x_i$$$ and $$$y_i$$$, meaning that $$$\vec{v_i} = (x_i, y_i)$$$. We have that $$$|v_i| \le 10^6$$$ for all $$$i$$$.

Output a single line containing $$$n$$$ integers $$$c_1, c_2, \cdots, c_n$$$, each of which is either $$$1$$$ or $$$-1$$$. Your solution is correct if the value of $$$p = \sum_{i = 1}^n c_i \vec{v_i}$$$, satisfies $$$|p| \le 1.5 \cdot 10^6$$$. It can be shown that a solution always exists under the given constraints.

(*input
8
-67761 603277
640586 -396671
46147 -122580
569609 -2112
400 914208
131792 309779
-850150 -486293
5272 721899
*)
let scan_int() = Scanf.scanf " %d" (fun x -> x);;
let print_int x = Printf.printf "%d" x;;
let puts s = Printf.printf s;;
let n = Scanf.scanf "%d" (fun x -> x);;
type sgn = POS | NEG;;
let mul x y = match (x, y) with
	| (POS, x) -> x
	| (NEG, POS) -> NEG
	| (NEG, NEG) -> POS
;;
type vect =
	| Leaf of (int * int) * int
	| Combine of (int * int) * (sgn * (vect * vect))
;;
let xy l = match l with
	| Leaf((x, y), _) -> (x, y)
	| Combine((x, y), _) -> (x, y)
;;
let plus (a, b) (x, y) = (a + x, b + y);;
let minus (a, b) (x, y) = (a - x, b - y);;
let rec read i =
	if i >= n then
		[]
	else
		let x = scan_int() in
		let y = scan_int() in
		Leaf((x, y), i)::(read (i + 1));;
let r (x, y) =
	let a = Int64.of_int x in
	let b = Int64.of_int y in
	Int64.add (Int64.mul a a) (Int64.mul b b)
;;
let rec min l = match l with
	| [] -> failwith "empty list"
	| [x] -> x
	| a::b -> let c = (min b) in if a <= c then a else c
;;
let rec fix l = match l with
	| [] -> failwith "empty list"
	| [a] -> a
	| [a; b] ->
		if ((r (plus (xy a) (xy b)))) < (r (minus (xy a) (xy b))) then
			Combine((plus (xy a) (xy b)), (POS, (a, b)))
		else
			Combine((minus (xy a) (xy b)), (NEG, (a, b)))
	| a::b::c::l -> (
		let x = xy a in
		let y = xy b in
		let z = xy c in
		if (r (plus x y)) <= 1000000000000L then
			fix (Combine((plus x y), (POS, (a, b)))::c::l)
		else if (r (plus x z)) <= 1000000000000L then
			fix (Combine((plus x z), (POS, (a, c)))::b::l)
		else if (r (plus y z)) <= 1000000000000L then
			fix (Combine((plus y z), (POS, (b, c)))::a::l)
		else if (r (minus x y)) <= 1000000000000L then
			fix (Combine((minus x y), (NEG, (a, b)))::c::l)
		else if (r (minus x z)) <= 1000000000000L then
			fix (Combine((minus x z), (NEG, (a, c)))::b::l)
		else if (r (minus y z)) <= 1000000000000L then
			fix (Combine((minus y z), (NEG, (b, c)))::a::l)
		else
			failwith "lochas"
	)
;;
let ret = Array.make n NEG;;
let rec find w l = match l with
	| Leaf(_, i) -> (match w with POS -> (ret.(i) <- POS) | NEG -> ())
	| Combine(_, (w2, (l1, l2))) -> find w l1;find (mul w w2) l2
;;
let c = fix (read 0);;
let (x, y) = xy c;;
find POS c;;
Array.iter (fun x -> match x with POS -> puts "1 " | NEG -> puts "-1 ") ret;;

(*input
8
-67761 603277
640586 -396671
46147 -122580
569609 -2112
400 914208
131792 309779
-850150 -486293
5272 721899
*)
let scan_int() = Scanf.scanf " %d" (fun x -> x);;
let print_int x = Printf.printf "%d" x;;
let puts s = Printf.printf s;;
let n = Scanf.scanf "%d" (fun x -> x);;
type sgn = POS | NEG;;
let mul x y = match (x, y) with
	| (POS, x) -> x
	| (NEG, POS) -> NEG
	| (NEG, NEG) -> POS
;;
type vect =
	| Leaf of (int * int) * int
	| Combine of (int * int) * (sgn * (vect * vect))
;;
let xy l = match l with
	| Leaf((x, y), _) -> (x, y)
	| Combine((x, y), _) -> (x, y)
;;
let plus (a, b) (x, y) = (a + x, b + y);;
let minus (a, b) (x, y) = (a - x, b - y);;
let rec read i =
	if i >= n then
		[]
	else
		let x = scan_int() in
		let y = scan_int() in
		Leaf((x, y), i)::(read (i + 1));;
let r (x, y) =
	let a = Int64.of_int x in
	let b = Int64.of_int y in
	Int64.add (Int64.mul a a) (Int64.mul b b)
;;
let rec min l = match l with
	| [] -> failwith "empty list"
	| [x] -> x
	| a::b -> let c = (min b) in if a <= c then a else c
;;
let rec fix l = match l with
	| [] -> failwith "empty list"
	| [a] -> a
	| [a; b] ->
		if ((r (plus (xy a) (xy b)))) < (r (minus (xy a) (xy b))) then
			Combine((plus (xy a) (xy b)), (POS, (a, b)))
		else
			Combine((minus (xy a) (xy b)), (NEG, (a, b)))
	| a::b::l -> (
		let x = xy a in
		let y = xy b in
		if (r (plus x y)) <= 1000000000000L then
			fix (Combine((plus x y), (POS, (a, b)))::l)
		else if (r (minus x y)) <= 1000000000000L then
			fix (Combine((minus x y), (NEG, (a, b)))::l)
		else (
			let c::l = l in
			let z = xy c in
			if (r (plus x z)) <= 1000000000000L then
				fix (Combine((plus x z), (POS, (a, c)))::b::l)
			else if (r (minus x z)) <= 1000000000000L then
				fix (Combine((minus x z), (NEG, (a, c)))::b::l)
			else
				fix ((
					if (r (plus y z)) <= 1000000000000L then
						Combine((plus y z), (POS, (b, c)))
					else
						Combine((minus y z), (NEG, (b, c)))
				)::a::l)
		)
	)
;;
let ret = Array.make n NEG;;
let rec find w l = match l with
	| Leaf(_, i) -> (match w with POS -> (ret.(i) <- POS) | NEG -> ())
	| Combine(_, (w2, (l1, l2))) -> find w l1;find (mul w w2) l2
;;
find POS (fix (read 0));;
Array.iter (fun x -> match x with POS -> puts "1 " | NEG -> puts "-1 ") ret;;

(*input
8
-67761 603277
640586 -396671
46147 -122580
569609 -2112
400 914208
131792 309779
-850150 -486293
5272 721899
*)
let scan_int() = Scanf.scanf " %d" (fun x -> x);;
let print_int x = Printf.printf "%d" x;;
let puts s = Printf.printf s;;
let n = Scanf.scanf "%d" (fun x -> x);;
type vect =
	| Leaf of (int * int) * int
	| Combine of (int * int) * (int * (vect * vect))
;;
let xy l = match l with
	| Leaf((x, y), _) -> (x, y)
	| Combine((x, y), _) -> (x, y)
;;
let plus (a, b) (x, y) = (a + x, b + y);;
let minus (a, b) (x, y) = (a - x, b - y);;
let rec read i =
	if i >= n then
		[]
	else
		let x = scan_int() in
		let y = scan_int() in
		Leaf((x, y), i)::(read (i + 1));;
let r (x, y) =
	let a = Int64.of_int x in
	let b = Int64.of_int y in
	Int64.add (Int64.mul a a) (Int64.mul b b)
;;
let rec min l = match l with
	| [] -> failwith "empty list"
	| [x] -> x
	| a::b -> let c = (min b) in if a <= c then a else c
;;
let rec fix l = match l with
	| [] -> failwith "empty list"
	| [a] -> a
	| [a; b] ->
		if ((r (plus (xy a) (xy b)))) < (r (minus (xy a) (xy b))) then
			Combine((plus (xy a) (xy b)), (1, (a, b)))
		else
			Combine((minus (xy a) (xy b)), (-1, (a, b)))
	| a::b::c::l -> (
		let x = xy a in
		let y = xy b in
		let z = xy c in
		if (r (plus x y)) <= 1000000000000L then
			fix (Combine((plus x y), (1, (a, b)))::c::l)
		else if (r (plus x z)) <= 1000000000000L then
			fix (Combine((plus x z), (1, (a, c)))::b::l)
		else if (r (plus y z)) <= 1000000000000L then
			fix (Combine((plus y z), (1, (b, c)))::a::l)
		else if (r (minus x y)) <= 1000000000000L then
			fix (Combine((minus x y), (-1, (a, b)))::c::l)
		else if (r (minus x z)) <= 1000000000000L then
			fix (Combine((minus x z), (-1, (a, c)))::b::l)
		else if (r (minus y z)) <= 1000000000000L then
			fix (Combine((minus y z), (-1, (b, c)))::a::l)
		else
			failwith "lochas"
	)
;;
let ret = Array.make n 0;;
let rec find w l = match l with
	| Leaf(_, i) -> (ret.(i) <- w)
	| Combine(_, (w2, (l1, l2))) -> find w l1;find (w * w2) l2
;;
let c = fix (read 0);;
let (x, y) = xy c;;
find 1 c;;
Array.iter (fun x -> print_int x;puts " ") ret;;

(*input
8
-67761 603277
640586 -396671
46147 -122580
569609 -2112
400 914208
131792 309779
-850150 -486293
5272 721899
*)
let scan_int() = Scanf.scanf " %d" (fun x -> x);;
let print_int x = Printf.printf "%d" x;;
let puts s = Printf.printf s;;
let n = Scanf.scanf "%d" (fun x -> x);;
type vect =
	| Leaf of (int * int) * int
	| Combine of (int * int) * (int * (vect * vect))
;;
let xy l = match l with
	| Leaf((x, y), _) -> (x, y)
	| Combine((x, y), _) -> (x, y)
;;
let plus (a, b) (x, y) = (a + x, b + y);;
let minus (a, b) (x, y) = (a - x, b - y);;
let rec read i =
	if i >= n then
		[]
	else
		let x = scan_int() in
		let y = scan_int() in
		Leaf((x, y), i)::(read (i + 1));;
let r (x, y) =
	let a = Int64.of_int x in
	let b = Int64.of_int y in
	Int64.add (Int64.mul a a) (Int64.mul b b)
;;
let rec min l = match l with
	| [] -> failwith "empty list"
	| [x] -> x
	| a::b -> let c = (min b) in if a <= c then a else c
;;
let rec fix l = match l with
	| [] -> failwith "empty list"
	| [a] -> a
	| [a; b] ->
		if ((r (plus (xy a) (xy b)))) < (r (minus (xy a) (xy b))) then
			Combine((plus (xy a) (xy b)), (1, (a, b)))
		else
			Combine((minus (xy a) (xy b)), (-1, (a, b)))
	| a::b::c::l -> (
		let x = xy a in
		let y = xy b in
		let z = xy c in
		let rxyp = r (plus x y) in
		let rxzp = r (plus x z) in
		let ryzp = r (plus y z) in
		let rxym = r (minus x y) in
		let rxzm = r (minus x z) in
		let ryzm = r (minus y z) in
		let rmin = min [rxyp; rxzp; ryzp; rxym; rxzm; ryzm] in
		if rxyp = rmin then
			fix (Combine((plus x y), (1, (a, b)))::c::l)
		else if rxzp = rmin then
			fix (Combine((plus x z), (1, (a, c)))::b::l)
		else if ryzp = rmin then
			fix (Combine((plus y z), (1, (b, c)))::a::l)
		else if rxym = rmin then
			fix (Combine((minus x y), (-1, (a, b)))::c::l)
		else if rxzm = rmin then
			fix (Combine((minus x z), (-1, (a, c)))::b::l)
		else if ryzm = rmin then
			fix (Combine((minus y z), (-1, (b, c)))::a::l)
		else
			failwith "lochas"
	)
;;
let ret = Array.make n 0;;
let rec find w l = match l with
	| Leaf(_, i) -> (ret.(i) <- w)
	| Combine(_, (w2, (l1, l2))) -> find w l1;find (w * w2) l2
;;
let c = fix (read 0);;
let (x, y) = xy c;;
find 1 c;;
Array.iter (fun x -> print_int x;puts " ") ret;;

(*input
8
-67761 603277
640586 -396671
46147 -122580
569609 -2112
400 914208
131792 309779
-850150 -486293
5272 721899
*)
let scan_int() = Scanf.scanf " %d" (fun x -> x);;
let print_int x = Printf.printf "%d" x;;
let puts s = Printf.printf s;;
let n = Scanf.scanf "%d" (fun x -> x);;
type sgn = POS | NEG;;
let mul x y = match (x, y) with
	| (POS, x) -> x
	| (NEG, POS) -> NEG
	| (NEG, NEG) -> POS
;;
type vect =
	| Leaf of (int * int) * int
	| Combine of (int * int) * (sgn * (vect * vect))
;;
let xy l = match l with
	| Leaf((x, y), _) -> (x, y)
	| Combine((x, y), _) -> (x, y)
;;
let plus (a, b) (x, y) = (a + x, b + y);;
let minus (a, b) (x, y) = (a - x, b - y);;
let rec read i =
	if i >= n then
		[]
	else
		let x = scan_int() in
		let y = scan_int() in
		Leaf((x, y), i)::(read (i + 1));;
let r (x, y) =
	let a = Int64.of_int x in
	let b = Int64.of_int y in
	Int64.add (Int64.mul a a) (Int64.mul b b)
;;
let rec min l = match l with
	| [] -> failwith "empty list"
	| [x] -> x
	| a::b -> let c = (min b) in if a <= c then a else c
;;
let rec fix l = match l with
	| [] -> failwith "empty list"
	| [a] -> a
	| [a; b] ->
		if ((r (plus (xy a) (xy b)))) < (r (minus (xy a) (xy b))) then
			Combine((plus (xy a) (xy b)), (POS, (a, b)))
		else
			Combine((minus (xy a) (xy b)), (NEG, (a, b)))
	| a::b::l -> (
		let x = xy a in
		let y = xy b in
		if (r (plus x y)) <= 1000000000000L then
			fix (Combine((plus x y), (POS, (a, b)))::l)
		else if (r (minus x y)) <= 1000000000000L then
			fix (Combine((minus x y), (NEG, (a, b)))::l)
		else (
			let c::l = l in
			let z = xy c in
			if (r (plus x z)) <= 1000000000000L then
				fix (Combine((plus x z), (POS, (a, c)))::b::l)
			else if (r (minus x z)) <= 1000000000000L then
				fix (Combine((minus x z), (NEG, (a, c)))::b::l)
			else if (r (plus y z)) <= 1000000000000L then
				fix (Combine((plus y z), (POS, (b, c)))::a::l)
			else if (r (minus y z)) <= 1000000000000L then
				fix (Combine((minus y z), (NEG, (b, c)))::a::l)
			else
				failwith "lochas"
		)
	)
;;
let ret = Array.make n NEG;;
let rec find w l = match l with
	| Leaf(_, i) -> (match w with POS -> (ret.(i) <- POS) | NEG -> ())
	| Combine(_, (w2, (l1, l2))) -> find w l1;find (mul w w2) l2
;;
find POS (fix (read 0));;
Array.iter (fun x -> match x with POS -> puts "1 " | NEG -> puts "-1 ") ret;;

(*input
8
-67761 603277
640586 -396671
46147 -122580
569609 -2112
400 914208
131792 309779
-850150 -486293
5272 721899
*)
let scan_int() = Scanf.scanf " %d" (fun x -> x);;
let print_int x = Printf.printf "%d" x;;
let puts s = Printf.printf s;;
let n = Scanf.scanf "%d" (fun x -> x);;
type vect =
	| Leaf of (int * int) * int
	| Combine of (int * int) * (int * (vect * vect))
;;
let xy l = match l with
	| Leaf((x, y), _) -> (x, y)
	| Combine((x, y), _) -> (x, y)
;;
let plus (a, b) (x, y) = (a + x, b + y);;
let minus (a, b) (x, y) = (a - x, b - y);;
let rec read i =
	if i >= n then
		[]
	else
		let x = scan_int() in
		let y = scan_int() in
		Leaf((x, y), i)::(read (i + 1));;
let r (x, y) =
	let a = Int64.of_int x in
	let b = Int64.of_int y in
	Int64.add (Int64.mul a a) (Int64.mul b b)
;;
let rec min l = match l with
	| [] -> failwith "empty list"
	| [x] -> x
	| a::b -> let c = (min b) in if a <= c then a else c
;;
let rec fix l = match l with
	| [] -> failwith "empty list"
	| [a] -> a
	| [a; b] ->
		if ((r (plus (xy a) (xy b)))) < (r (minus (xy a) (xy b))) then
			Combine((plus (xy a) (xy b)), (1, (a, b)))
		else
			Combine((minus (xy a) (xy b)), (-1, (a, b)))
	| a::b::c::l -> (
		let x = xy a in
		let y = xy b in
		let z = xy c in
		if (r (plus x y)) <= 1000000000000L then
			fix (Combine((plus x y), (1, (a, b)))::c::l)
		else if (r (plus x z)) <= 1000000000000L then
			fix (Combine((plus x z), (1, (a, c)))::b::l)
		else if (r (plus y z)) <= 1000000000000L then
			fix (Combine((plus y z), (1, (b, c)))::a::l)
		else if (r (minus x y)) <= 1000000000000L then
			fix (Combine((minus x y), (-1, (a, b)))::c::l)
		else if (r (minus x z)) <= 1000000000000L then
			fix (Combine((minus x z), (-1, (a, c)))::b::l)
		else if (r (minus y z)) <= 1000000000000L then
			fix (Combine((minus y z), (-1, (b, c)))::a::l)
		else
			failwith "lochas"
	)
;;
let ret = Array.make n 0;;
let rec find w l = match l with
	| Leaf(_, i) -> (ret.(i) <- w)
	| Combine(_, (w2, (l1, l2))) -> find w l1;find (w * w2) l2
;;
let c = fix (read 0);;
let (x, y) = xy c;;
find 1 c;;
Array.iter (fun x -> if x = 1 then puts "1 " else puts "-1 ") ret;;

(*input
8
-67761 603277
640586 -396671
46147 -122580
569609 -2112
400 914208
131792 309779
-850150 -486293
5272 721899
*)
open Printf
open Scanf
let scan_int() = bscanf Scanning.stdib " %d " (fun x -> x);;
let print_int x = printf "%d" x;;
let puts s = printf s;;
let n = scanf "%d" (fun x -> x);;
type sgn = POS | NEG;;
let mul x y = match (x, y) with
	| (POS, x) -> x
	| (NEG, POS) -> NEG
	| (NEG, NEG) -> POS
;;
type vect =
	| Leaf of (int * int) * int
	| Combine of (int * int) * (sgn * (vect * vect))
;;
let xy l = match l with
	| Leaf((x, y), _) -> (x, y)
	| Combine((x, y), _) -> (x, y)
;;
let plus (a, b) (x, y) = (a + x, b + y);;
let minus (a, b) (x, y) = (a - x, b - y);;
let rec read i =
	if i >= n then
		[]
	else
		let x = scan_int() in
		let y = scan_int() in
		Leaf((x, y), i)::(read (i + 1));;
let r (x, y) =
	let a = Int64.of_int x in
	let b = Int64.of_int y in
	Int64.add (Int64.mul a a) (Int64.mul b b)
;;
let rec min l = match l with
	| [] -> failwith "empty list"
	| [x] -> x
	| a::b -> let c = (min b) in if a <= c then a else c
;;
let rec fix l = match l with
	| [] -> failwith "empty list"
	| [a] -> a
	| [a; b] ->
		if ((r (plus (xy a) (xy b)))) < (r (minus (xy a) (xy b))) then
			Combine((plus (xy a) (xy b)), (POS, (a, b)))
		else
			Combine((minus (xy a) (xy b)), (NEG, (a, b)))
	| a::b::l -> (
		let x = xy a in
		let y = xy b in
		if (r (plus x y)) <= 1000000000000L then
			fix (Combine((plus x y), (POS, (a, b)))::l)
		else if (r (minus x y)) <= 1000000000000L then
			fix (Combine((minus x y), (NEG, (a, b)))::l)
		else (
			let c::l = l in
			let z = xy c in
			if (r (plus x z)) <= 1000000000000L then
				fix (Combine((plus x z), (POS, (a, c)))::b::l)
			else if (r (minus x z)) <= 1000000000000L then
				fix (Combine((minus x z), (NEG, (a, c)))::b::l)
			else
				fix ((
					if (r (plus y z)) <= 1000000000000L then
						Combine((plus y z), (POS, (b, c)))
					else
						Combine((minus y z), (NEG, (b, c)))
				)::a::l)
		)
	)
;;
let ret = Array.make n NEG;;
let rec find w l = match l with
	| Leaf(_, i) -> (match w with POS -> (ret.(i) <- POS) | NEG -> ())
	| Combine(_, (w2, (l1, l2))) -> find w l1;find (mul w w2) l2
;;
find POS (fix (read 0));;
Array.iter (fun x -> match x with POS -> puts "1 " | NEG -> puts "-1 ") ret;;

(*input
8
-67761 603277
640586 -396671
46147 -122580
569609 -2112
400 914208
131792 309779
-850150 -486293
5272 721899
*)
let scan_int() = Scanf.scanf " %d" (fun x -> x);;
let print_int x = Printf.printf "%d" x;;
let puts s = Printf.printf s;;
let n = Scanf.scanf "%d" (fun x -> x);;
type vect =
	| Leaf of (int * int) * int
	| Combine of (int * int) * ((int * int) * (vect * vect))
;;
let xy l = match l with
	| Leaf((x, y), _) -> (x, y)
	| Combine((x, y), _) -> (x, y)
;;
let plus (a, b) (x, y) = (a + x, b + y);;
let minus (a, b) (x, y) = (a - x, b - y);;
let rec read i =
	if i >= n then
		[]
	else
		let x = scan_int() in
		let y = scan_int() in
		Leaf((x, y), i)::(read (i + 1));;
let r (x, y) =
	let a = Int64.of_int x in
	let b = Int64.of_int y in
	Int64.add (Int64.mul a a) (Int64.mul b b)
;;
let rec min l = match l with
	| [] -> failwith "empty list"
	| [x] -> x
	| a::b -> let c = (min b) in if a <= c then a else c
;;
let rec fix l = match l with
	| [] -> Leaf((-1, -1), -1)
	| [a] -> a
	| [a; b] ->
		if ((r (plus (xy a) (xy b)))) < (r (minus (xy a) (xy b))) then
			Combine((plus (xy a) (xy b)), ((1, 1), (a, b)))
		else
			Combine((minus (xy a) (xy b)), ((1, -1), (a, b)))
	| a::b::c::l -> (
		let x = xy a in
		let y = xy b in
		let z = xy c in
		let rxyp = r (plus x y) in
		let rxzp = r (plus x z) in
		let ryzp = r (plus y z) in
		let rxym = r (minus x y) in
		let rxzm = r (minus x z) in
		let ryzm = r (minus y z) in
		let rmin = min [rxyp; rxzp; ryzp; rxym; rxzm; ryzm] in
		if rxyp = rmin then
			fix (Combine((plus x y), ((1, 1), (a, b)))::c::l)
		else if rxzp = rmin then
			fix (Combine((plus x z), ((1, 1), (a, c)))::b::l)
		else if ryzp = rmin then
			fix (Combine((plus y z), ((1, 1), (b, c)))::a::l)
		else if rxym = rmin then
			fix (Combine((minus x y), ((1, -1), (a, b)))::c::l)
		else if rxzm = rmin then
			fix (Combine((minus x z), ((1, -1), (a, c)))::b::l)
		else if ryzm = rmin then
			fix (Combine((minus y z), ((1, -1), (b, c)))::a::l)
		else
			failwith "lochas"
	)
;;
let ret = Array.make n 0;;
let rec find w l = match l with
	| Leaf(_, i) -> (ret.(i) <- w)
	| Combine(_, ((w1, w2), (l1, l2))) -> find (w * w1) l1;find (w * w2) l2
;;
let rec check l = match l with
	| Leaf((x,y), i) -> puts "(";print_int y;puts ")";
	| Combine((x, y), ((w1, w2), (l1, l2)))->
	puts "(";
	check l1;
	puts (if(w2==1) then "+" else "-");
	check l2;
	puts ")";
;;
let c = fix (read 0);;
if r (xy c) > (Int64.mul 1500000L 1500000L)
	then failwith "Oh shit";;
let (x, y) = xy c;;
(*Printf.printf "%d %d\n" x y;;
puts "\n";
check c;;*)
find 1 c;;
Array.iter (fun x -> print_int x;puts " ") ret;;

(*input
8
1000000 0
0 1000000
1000000 0
0 1000000
1000000 0
0 1000000
1000000 0
0 1000000
*)
let scan_int() = Scanf.scanf " %d" (fun x -> x);;
let print_int x = Printf.printf "%d" x;;
let puts s = Printf.printf s;;
let n = scan_int();;
type vect =
	| Leaf of (int * int) * int
	| Combine of (int * int) * ((int * int) * (vect * vect))
;;
let xy l = match l with
	| Leaf((x, y), _) -> (x, y)
	| Combine((x, y), _) -> (x, y)
;;
let plus (a, b) (x, y) = (a + x, b + y);;
let minus (a, b) (x, y) = (a - x, b - y);;
let rec read i =
	if i >= n then
		[]
	else
		Leaf((scan_int(), scan_int()), i)::(read (i + 1));;
let r (x, y) =
	let a = Int64.of_int x in
	let b = Int64.of_int y in
	Int64.add (Int64.mul a a) (Int64.mul b b)
;;
let rec min l = match l with
	| [] -> failwith "empty list"
	| [x] -> x
	| a::b -> let c = (min b) in if a < c then a else c
;;
let rec fix l = match l with
	| [] -> Leaf((-1, -1), -1)
	| [Leaf(x, y)] -> Leaf(x, y)
	| [Combine(x, y)] -> Combine(x, y)
	| a::b::[] ->
		if ((r (plus (xy a) (xy b)))) < (r (minus (xy a) (xy b))) then
			Combine((plus (xy a) (xy b)), ((1, 1), (a, b)))
		else
			Combine((minus (xy a) (xy b)), ((1, -1), (a, b)))
	| a::b::c::l -> (
		let x = xy a in
		let y = xy b in
		let z = xy c in
		let rxyp = r (plus x y) in
		let rxzp = r (plus x z) in
		let ryzp = r (plus y z) in
		let rxym = r (minus x y) in
		let rxzm = r (minus x z) in
		let ryzm = r (minus y z) in
		let rmin = min [rxyp; rxzp; ryzp; rxym; rxzm; ryzm] in
		if rxyp = rmin then
			fix (Combine((plus x y), ((1, 1), (a, b)))::c::l)
		else if rxzp = rmin then
			fix (Combine((plus x z), ((1, 1), (a, c)))::b::l)
		else if ryzp = rmin then
			fix (Combine((plus y z), ((1, 1), (b, c)))::a::l)
		else if rxym = rmin then
			fix (Combine((minus x y), ((1, -1), (a, b)))::c::l)
		else if rxzm = rmin then
			fix (Combine((minus x z), ((1, -1), (a, c)))::b::l)
		else
			fix (Combine((minus y z), ((1, -1), (b, c)))::a::l)
	)
;;
let ret = Array.make n 1;;
let rec find w l = match l with
	| Leaf(_, i) -> (ret.(i) <- w)
	| Combine(_, ((w1, w2), (l1, l2))) -> find (w * w1) l1;find (w * w2) l2
;;
let c = fix (read 0);;
if r (xy c) > (Int64.mul 1500000L 1500000L)
	then failwith "Oh shit";;
find 1 c;;
Array.iter (fun x -> print_int x;puts " ") ret;;

(*input
8
-67761 603277
640586 -396671
46147 -122580
569609 -2112
400 914208
131792 309779
-850150 -486293
5272 721899
*)
let scan_int() = Scanf.scanf " %d" (fun x -> x);;
let print_int x = Printf.printf "%d" x;;
let puts s = Printf.printf s;;
let n = scan_int();;
type vect =
	| Leaf of (int * int) * int
	| Combine of (int * int) * ((int * int) * (vect * vect))
;;
let xy l = match l with
	| Leaf((x, y), _) -> (x, y)
	| Combine((x, y), _) -> (x, y)
;;
let plus (a, b) (x, y) = (a + x, b + y);;
let minus (a, b) (x, y) = (a - x, b - y);;
let rec read i =
	if i >= n then
		[]
	else
		Leaf((scan_int(), scan_int()), i)::(read (i + 1));;
let r (x, y) = x * x + y * y;;
let rec min l = match l with
	| [] -> failwith "empty list minimum"
	| [x] -> x
	| a::b -> let c = (min b) in if a < c then a else c
;;
let rec fix l = match l with
	| [] -> Leaf((-1, -1), -1)
	| [Leaf(x, y)] -> Leaf(x, y)
	| [Combine(x, y)] -> Combine(x, y)
	| a::b::[] ->
		if (r (plus (xy a) (xy b))) < (r (minus (xy a) (xy b))) then
			Combine((plus (xy a) (xy b)), ((1, 1), (a, b)))
		else
			Combine((minus (xy a) (xy b)), ((1, -1), (a, b)))
	| a::b::c::l -> (
		let x = xy a in
		let y = xy b in
		let z = xy c in
		let rxyp = r (plus x y) in
		let rxzp = r (plus x z) in
		let ryzp = r (plus y z) in
		let rxym = r (minus x y) in
		let rxzm = r (minus x z) in
		let ryzm = r (minus y z) in
		let rmin = min [rxyp; rxzp; ryzp; rxym; rxzm; ryzm] in
		if rxyp = rmin then
			fix (Combine((plus x y), ((1, 1), (a, b)))::c::l)
		else if rxzp = rmin then
			fix (Combine((plus x z), ((1, 1), (a, c)))::b::l)
		else if ryzp = rmin then
			fix (Combine((plus y z), ((1, 1), (b, c)))::a::l)
		else if rxym = rmin then
			fix (Combine((minus x y), ((1, -1), (a, b)))::c::l)
		else if rxzm = rmin then
			fix (Combine((minus x z), ((1, -1), (a, c)))::b::l)
		else
			fix (Combine((minus y z), ((1, -1), (b, c)))::a::l)
	)
;;
let ret = Array.make n 1;;
let rec find w l = match l with
	| Leaf(_, i) -> (ret.(i) <- w)
	| Combine(_, ((w1, w2), (l1, l2))) -> find (w * w1) l1;find (w * w2) l2
;;
find 1 (fix (read 0));;
Array.iter (fun x -> print_int x;puts " ") ret;;

(*input
8
-67761 603277
640586 -396671
46147 -122580
569609 -2112
400 914208
131792 309779
-850150 -486293
5272 721899
*)
let scan_int() = Scanf.scanf " %d" (fun x -> x);;
let print_int x = Printf.printf "%d" x;;
let puts s = Printf.printf s;;
let n = scan_int();;
type vect =
	| Leaf of (int * int) * int
	| Combine of (int * int) * ((int * int) * (vect * vect))
;;
let xy l = match l with
	| Leaf((x, y), _) -> (x, y)
	| Combine((x, y), _) -> (x, y)
;;
let plus (a, b) (x, y) = (a + x, b + y);;
let minus (a, b) (x, y) = (a - x, b - y);;
let rec read i =
	if i >= n then
		[]
	else
		Leaf((scan_int(), scan_int()), i)::(read (i + 1));;
let r (x, y) =
	let a = Int64.of_int x in
	let b = Int64.of_int y in
	Int64.add (Int64.mul a a) (Int64.mul b b)
;;
let rec min l = match l with
	| [] -> failwith "empty list"
	| [x] -> x
	| a::b -> let c = (min b) in if a < c then a else c
;;
let rec fix l = match l with
	| [] -> Leaf((-1, -1), -1)
	| [Leaf(x, y)] -> Leaf(x, y)
	| [Combine(x, y)] -> Combine(x, y)
	| a::b::[] ->
		if (r (plus (xy a) (xy b))) < (r (minus (xy a) (xy b))) then
			Combine((plus (xy a) (xy b)), ((1, 1), (a, b)))
		else
			Combine((minus (xy a) (xy b)), ((1, -1), (a, b)))
	| a::b::c::l -> (
		let x = xy a in
		let y = xy b in
		let z = xy c in
		let rxyp = r (plus x y) in
		let rxzp = r (plus x z) in
		let ryzp = r (plus y z) in
		let rxym = r (minus x y) in
		let rxzm = r (minus x z) in
		let ryzm = r (minus y z) in
		let rmin = min [rxyp; rxzp; ryzp; rxym; rxzm; ryzm] in
		if rxyp = rmin then
			fix (Combine((plus x y), ((1, 1), (a, b)))::c::l)
		else if rxzp = rmin then
			fix (Combine((plus x z), ((1, 1), (a, c)))::b::l)
		else if ryzp = rmin then
			fix (Combine((plus y z), ((1, 1), (b, c)))::a::l)
		else if rxym = rmin then
			fix (Combine((minus x y), ((1, -1), (a, b)))::c::l)
		else if rxzm = rmin then
			fix (Combine((minus x z), ((1, -1), (a, c)))::b::l)
		else
			fix (Combine((minus y z), ((1, -1), (b, c)))::a::l)
	)
;;
let ret = Array.make n 1;;
let rec find w l = match l with
	| Leaf(_, i) -> (ret.(i) <- w)
	| Combine(_, ((w1, w2), (l1, l2))) -> find (w * w1) l1;find (w * w2) l2
;;
let c = fix (read 0);;
let (x, y) = xy c;;
Printf.eprintf "%d %d\n" x y;;
find 1 c;;
Array.iter (fun x -> print_int x;puts " ") ret;;

(*input
8
-67761 603277
640586 -396671
46147 -122580
569609 -2112
400 914208
131792 309779
-850150 -486293
5272 721899
*)
let scan_int() = Scanf.scanf " %d" (fun x -> x);;
let print_int x = Printf.printf "%d" x;;
let puts s = Printf.printf s;;
let n = scan_int();;
type vect =
	| Leaf of (int * int) * int
	| Combine of (int * int) * ((int * int) * (vect * vect))
;;
let xy l = match l with
	| Leaf((x, y), _) -> (x, y)
	| Combine((x, y), _) -> (x, y)
;;
let plus (a, b) (x, y) = (a + x, b + y);;
let minus (a, b) (x, y) = (a - x, b - y);;
let rec read i =
	if i >= n then
		[]
	else
		Leaf((scan_int(), scan_int()), i)::(read (i + 1));;
let r (x, y) =
	let a = Int64.of_int x in
	let b = Int64.of_int y in
	Int64.add (Int64.mul a a) (Int64.mul b b)
;;
let rec min l = match l with
	| [] -> failwith "empty list minimum"
	| [x] -> x
	| a::b -> let c = (min b) in if a < c then a else c
;;
let rec fix l = match l with
	| [] -> Leaf((-1, -1), -1)
	| [Leaf(x, y)] -> Leaf(x, y)
	| [Combine(x, y)] -> Combine(x, y)
	| a::b::[] ->
		if (r (plus (xy a) (xy b))) < (r (minus (xy a) (xy b))) then
			Combine((plus (xy a) (xy b)), ((1, 1), (a, b)))
		else
			Combine((minus (xy a) (xy b)), ((1, -1), (a, b)))
	| a::b::c::l -> (
		let x = xy a in
		let y = xy b in
		let z = xy c in
		let rxyp = r (plus x y) in
		let rxzp = r (plus x z) in
		let ryzp = r (plus y z) in
		let rxym = r (minus x y) in
		let rxzm = r (minus x z) in
		let ryzm = r (minus y z) in
		let rmin = min [rxyp; rxzp; ryzp; rxym; rxzm; ryzm] in
		if rxyp = rmin then
			fix (Combine((plus x y), ((1, 1), (a, b)))::c::l)
		else if rxzp = rmin then
			fix (Combine((plus x z), ((1, 1), (a, c)))::b::l)
		else if ryzp = rmin then
			fix (Combine((plus y z), ((1, 1), (b, c)))::a::l)
		else if rxym = rmin then
			fix (Combine((minus x y), ((1, -1), (a, b)))::c::l)
		else if rxzm = rmin then
			fix (Combine((minus x z), ((1, -1), (a, c)))::b::l)
		else
			fix (Combine((minus y z), ((1, -1), (b, c)))::a::l)
	)
;;
let ret = Array.make n 1;;
let rec find w l = match l with
	| Leaf(_, i) -> (ret.(i) <- w)
	| Combine(_, ((w1, w2), (l1, l2))) -> find (w * w1) l1;find (w * w2) l2
;;
find 1 (fix (read 0));;
Array.iter (fun x -> print_int x;puts " ") ret;;


A piece of paper contains an array of n integers a1, a2, ..., an. Your task is to find a number that occurs the maximum number of times in this array.However, before looking for such number, you are allowed to perform not more than k following operations — choose an arbitrary element from the array and add 1 to it. In other words, you are allowed to increase some array element by 1 no more than k times (you are allowed to increase the same element of the array multiple times).Your task is to find the maximum number of occurrences of some number in the array after performing no more than k allowed operations. If there are several such numbers, your task is to find the minimum one.

The first line contains two integers n and k (1 ≤ n ≤ 105; 0 ≤ k ≤ 109) — the number of elements in the array and the number of operations you are allowed to perform, correspondingly. The third line contains a sequence of n integers a1, a2, ..., an (|ai| ≤ 109) — the initial array. The numbers in the lines are separated by single spaces.

In a single line print two numbers — the maximum number of occurrences of some number in the array after at most k allowed operations are performed, and the minimum number that reaches the given maximum. Separate the printed numbers by whitespaces.

NoteIn the first sample your task is to increase the second element of the array once and increase the fifth element of the array twice. Thus, we get sequence 6, 4, 4, 0, 4, where number 4 occurs 3 times.In the second sample you don't need to perform a single operation or increase each element by one. If we do nothing, we get array 5, 5, 5, if we increase each by one, we get 6, 6, 6. In both cases the maximum number of occurrences equals 3. So we should do nothing, as number 5 is less than number 6.In the third sample we should increase the second array element once and the fifth element once. Thus, we get sequence 3, 2, 2, 2, 2, where number 2 occurs 4 times.

let read1 () = Scanf.scanf "%d\n" ( fun x -> x );;
let read2 () = Scanf.scanf "%d %d\n" ( fun x y -> x, y );;
let read3 () = Scanf.scanf "%d %d %d\n" ( fun x y z -> x, y, z);;
let read x = (if x = 0 then Scanf.scanf "%d\n" else Scanf.scanf "%d ") ( fun x -> x );;

let (+$) x y =
  if x > max_int - y then max_int
  else x + y;;

let rec ( *$ ) x y =
  if y = 0 then 0 else
  if y mod 2 = 1 then x *$ (y-1) +$ x
  else let a = x *$ (y/2) in a +$ a;;

let _ =
  let (n, k) = read2() in
  let tab = Array.init n ( fun x -> read (n-x-1) )
  and max = ref 0
  and gdzie = ref 0
  and kol = Queue.create ()
  and dodaj = ref 0 in
  Array.sort compare tab;
  for i=0 to (n-1) do
    let x = tab.(i)
    and ost = if i=0 then tab.(i) else tab.(i-1) in
    while !dodaj +$ ( (x-ost) *$ (Queue.length kol) ) > k do
      let y = Queue.take kol in
      dodaj := !dodaj - (ost-y)
    done;
    dodaj := !dodaj +$ ( (x-ost) *$ (Queue.length kol) );
    Queue.add x kol;
    if Queue.length kol > !max then begin
      max := Queue.length kol;
      gdzie := x;
    end
  done;
  Printf.printf "%d %d\n" !max !gdzie;;

let read1 () = Scanf.scanf "%d\n" ( fun x -> x );;
let read2 () = Scanf.scanf "%d %d\n" ( fun x y -> x, y );;
let read3 () = Scanf.scanf "%d %d %d\n" ( fun x y z -> x, y, z);;
let read x = (if x = 0 then Scanf.scanf "%d\n" else Scanf.scanf "%d ") ( fun x -> x );;

let _ =
  let (n, k) = read2() in
  let tab = Array.init n ( fun x -> read (n-x-1) )
  and max = ref 0
  and gdzie = ref 0
  and kol = Queue.create ()
  and dodaj = ref 0 in
  Array.sort compare tab;
  for i=0 to (n-1) do
    let x = tab.(i)
    and ost = if i=0 then 0 else tab.(i-1) in
    dodaj := !dodaj + (x-ost)*(Queue.length kol);
    while !dodaj > k do
      let y = Queue.take kol in
      dodaj := !dodaj - (x-y)
    done;
    Queue.add x kol;
    if Queue.length kol > !max then begin
      max := Queue.length kol;
      gdzie := x;
    end
  done;
  Printf.printf "%d %d\n" !max !gdzie;;

let read1 () = Scanf.scanf "%d\n" ( fun x -> x );;
let read2 () = Scanf.scanf "%d %d\n" ( fun x y -> x, y );;
let read3 () = Scanf.scanf "%d %d %d\n" ( fun x y z -> x, y, z);;
let read x = (if x = 0 then Scanf.scanf "%d\n" else Scanf.scanf "%d ") ( fun x -> x );;

let (+$) x y =
  if x > max_int - y then max_int
  else x + y;;

let rec ( *$ ) x y =
  if y = 0 then 0 else
  if y mod 2 = 1 then x *$ (y-1) +$ x
  else let a = x *$ (y/2) in a +$ a;;

let _ =
  let (n, k) = read2() in
  let tab = Array.init n ( fun x -> read (n-x-1) )
  and max = ref 0
  and gdzie = ref 0
  and kol = Queue.create ()
  and dodaj = ref 0 in
  Array.sort compare tab;
  for i=0 to (n-1) do
    let x = tab.(i)
    and ost = if i=0 then tab.(i) else tab.(i-1) in
    dodaj := !dodaj +$ ( (x-ost) *$ (Queue.length kol) );
    while !dodaj > k do
      let y = Queue.take kol in
      dodaj := !dodaj - (x-y)
    done;
    Queue.add x kol;
    if Queue.length kol > !max then begin
      max := Queue.length kol;
      gdzie := x;
    end
  done;
  Printf.printf "%d %d\n" !max !gdzie;;


You are given sequence a1, a2, ..., an of integer numbers of length n. Your task is to find such subsequence that its sum is odd and maximum among all such subsequences. It's guaranteed that given sequence contains subsequence with odd sum.Subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.You should write a program which finds sum of the best subsequence.

The first line contains integer number n (1 ≤ n ≤ 105). The second line contains n integer numbers a1, a2, ..., an ( - 104 ≤ ai ≤ 104). The sequence contains at least one subsequence with odd sum.

NoteIn the first example sum of the second and the fourth elements is 3.

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

let () =
	let n = read_int ()
	and suma = ref 0
	and mini = ref 10007
	and maxi = ref (-10007) in

	for i = 1 to n do
		let a = read_int () in
		if a > 0 then begin
			suma := !suma + a;
			if a mod 2 = 1 then
				mini := min !mini a
		end else if abs (a mod 2) = 1 then
			maxi := max !maxi a;
	done;

	if !suma mod 2 = 1 then
		printf "%d\n" !suma
	else
		printf "%d\n" (max (!suma - !mini) (!suma + !maxi))

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

let () =
    let n = read_int () in
    let a = Array.init n read_int in
    let sum = Array.fold_left (fun acc x -> if x > 0 then acc + x else acc) 0 a in
    let max_neg = Array.fold_left (fun acc x -> if x < 0 && abs x mod 2 = 1 then max acc x else acc) (-1000000000) a in
    let min_pos = Array.fold_left (fun acc x -> if x > 0 && abs x mod 2 = 1 then min acc x else acc) (1000000000) a in
    let ans =
        if sum mod 2 = 1 then sum
        else max (sum - min_pos) (sum + max_neg) in
    printf "%d\n" ans


For a connected undirected weighted graph G, MST (minimum spanning tree) is a subgraph of G that contains all of G's vertices, is a tree, and sum of its edges is minimum possible.You are given a graph G. If you run a MST algorithm on graph it would give you only one MST and it causes other edges to become jealous. You are given some queries, each query contains a set of edges of graph G, and you should determine whether there is a MST containing all these edges or not.

The first line contains two integers n, m (2  ≤ n, m  ≤ 5·105, n - 1 ≤ m) — the number of vertices and edges in the graph and the number of queries. The i-th of the next m lines contains three integers ui, vi, wi (ui ≠ vi, 1 ≤ wi ≤ 5·105) — the endpoints and weight of the i-th edge. There can be more than one edges between two vertices. It's guaranteed that the given graph is connected. The next line contains a single integer q (1 ≤ q ≤ 5·105) — the number of queries. q lines follow, the i-th of them contains the i-th query. It starts with an integer ki (1 ≤ ki ≤ n - 1) — the size of edges subset and continues with ki distinct space-separated integers from 1 to m — the indices of the edges. It is guaranteed that the sum of ki for 1 ≤ i ≤ q does not exceed 5·105.

For each query you should print "YES" (without quotes) if there's a MST containing these edges and "NO" (of course without quotes again) otherwise.

NoteThis is the graph of sample:  Weight of minimum spanning tree on this graph is 6.MST with edges (1, 3, 4, 6), contains all of edges from the first query, so answer on the first query is "YES".Edges from the second query form a cycle of length 3, so there is no spanning tree including these three edges. Thus, answer is "NO".

open Printf
open Scanf

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

let graph_cyclic edge_list =
  let map = Hashtbl.create 5 in
  let vn = ref 0 in
  let set_map u =
    if not (Hashtbl.mem map u) then (
      Hashtbl.add map u !vn;
      vn := !vn+1
    )
  in
  let get_map u = Hashtbl.find map u in
  List.iter (fun (u,v) -> set_map u; set_map v) edge_list;

  let parent = Array.make !vn (-1) in
  let rec find i = if parent.(i) < 0 then i else (
    parent.(i) <- find (parent.(i));
    parent.(i)
  ) in
  let union u v = parent.(u) <- v in

  let rec find_cycle li =
    match li with
      |	[] -> false
      | (u,v)::tail ->
	let cu = find (get_map u) in
	let cv = find (get_map v) in
	if cu = cv then true else (
	  union cu cv;
	  find_cycle tail
	)
  in
  find_cycle edge_list

let () =
  let (n,m) = read_2 () in

  let v1 = Array.make m 0 in
  let v2 = Array.make m 0 in
  let w = Array.make m 0 in

  for i=0 to m-1 do
    v1.(i) <- read_int() -1;
    v2.(i) <- read_int() -1;
    w.(i) <- read_int();
  done;

  let q = read_int() in
  let query = Array.make q ([],[[]]) in

  for i=0 to q-1 do
    let k = read_int() in
    let rec loop j ac = if j=k then ac else loop (j+1) ((read_int()-1) :: ac) in
    let qlist = loop 0 [] in

    let h = Hashtbl.create 5 in
    List.iter (fun e ->
      let li = try Hashtbl.find h w.(e) with Not_found -> [] in
      Hashtbl.replace h w.(e) (e :: li)
    ) qlist;

    query.(i) <- (qlist, Hashtbl.fold (fun _ v ac -> v::ac) h [])
    (* it's a list of lists of edges of the same weight *)
  done;

  let parent = Array.make n (-1) in
  let size = Array.make n 1 in
  let edge_no = Array.make n (-1) in

  let union a b edge =
  (* assume a and b are canonoical elements *)
    let (a,b) = if size.(a) <= size.(b) then (a,b) else (b,a) in
    parent.(a) <- b;
    size.(b) <- size.(b) + size.(a);
    edge_no.(a) <- edge
  in
  let rec find i = if parent.(i) < 0 then i else find parent.(i) in

  let perm = Array.init m (fun i -> i) in

  Array.sort (fun a b -> compare w.(a) w.(b)) perm;

  for i=0 to m-1 do
    let i = perm.(i) in
    let (a,b) = (find v1.(i), find v2.(i)) in
    if a<>b then union a b i
  done;

  for i=0 to q-1 do
    let (qlist, qlistlist) = query.(i) in
    (* first determine if the queries form a cycle *)
    let graph = List.map (fun e -> (v1.(e), v2.(e))) qlist in
    if graph_cyclic graph then printf "NO\n" else (
      let edge_set_valid es =
	let rec find_canon j ew =
	  if parent.(j) >= 0 && w.(edge_no.(j)) < ew then find_canon parent.(j) ew
	  else j
	in

	let graph = List.map (fun e -> (find_canon v1.(e) w.(e), find_canon v2.(e) w.(e))) es in
	not (graph_cyclic graph)
      in

      if List.for_all edge_set_valid qlistlist then printf "YES\n" else printf "NO\n"
    )
  done

(* keywords: union-find, MST
   Really nice problem involving understainding the MST algorithm, and
   how to use the union/find data structure without path compression.
*)

open Printf
open Scanf

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

let graph_cyclic edge_list =
  let map = Hashtbl.create 5 in
  let vn = ref 0 in
  let set_map u =
    if not (Hashtbl.mem map u) then (
      Hashtbl.add map u !vn;
      vn := !vn+1
    )
  in
  let get_map u = Hashtbl.find map u in
  List.iter (fun (u,v) -> set_map u; set_map v) edge_list;

  let parent = Array.make !vn (-1) in
  let rec find i = if parent.(i) < 0 then i else (
    parent.(i) <- find (parent.(i));
    parent.(i)
  ) in
  let union u v = parent.(u) <- v in

  let rec find_cycle li =
    match li with
      |	[] -> false
      | (u,v)::tail ->
	let cu = find (get_map u) in
	let cv = find (get_map v) in
	if cu = cv then true else (
	  union cu cv;
	  find_cycle tail
	)
  in
  find_cycle edge_list

let () =
  let (n,m) = read_2 () in

  let v1 = Array.make m 0 in
  let v2 = Array.make m 0 in
  let w = Array.make m 0 in

  for i=0 to m-1 do
    v1.(i) <- read_int() -1;
    v2.(i) <- read_int() -1;
    w.(i) <- read_int();
  done;

  let q = read_int() in
  let query = Array.make q [] in

  for i=0 to q-1 do
    let k = read_int() in
    let rec loop j ac = if j=k then ac else loop (j+1) ((read_int()-1) :: ac) in
    query.(i) <- loop 0 []
  done;

  let parent = Array.make n (-1) in
  let size = Array.make n 1 in
  let edge_no = Array.make n (-1) in

  let union a b edge =
  (* assume a and b are canonoical elements *)
    let (a,b) = if size.(a) <= size.(b) then (a,b) else (b,a) in
    parent.(a) <- b;
    size.(b) <- size.(b) + size.(a);
    edge_no.(a) <- edge
  in
  let rec find i = if parent.(i) < 0 then i else find parent.(i) in

  let perm = Array.init m (fun i -> i) in

  Array.sort (fun a b -> compare w.(a) w.(b)) perm;

  for i=0 to m-1 do
    let i = perm.(i) in
    let (a,b) = (find v1.(i), find v2.(i)) in
    if a<>b then union a b i
  done;

  for i=0 to q-1 do
    (* first determine if the queries form a cycle *)
    let graph = List.map (fun e -> (v1.(e), v2.(e))) query.(i) in
    if graph_cyclic graph then printf "NO\n" else (
      let rec find_canon j ew =
	if parent.(j) >= 0 && w.(edge_no.(j)) < ew then find_canon parent.(j) ew
	else j
      in

      let graph = List.map (fun e -> (find_canon v1.(e) w.(e), find_canon v2.(e) w.(e))) query.(i) in
      if graph_cyclic graph then printf "NO\n" else printf "YES\n"
    )
  done