-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTeadsSponsoredContest.fs
More file actions
40 lines (33 loc) · 1.36 KB
/
Copy pathTeadsSponsoredContest.fs
File metadata and controls
40 lines (33 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
module TeadsSponsoredContest
open System
open System.Collections.Generic
let R() = Console.In.ReadLine()
type Node(id: int) =
let mutable links: Node list = []
member this.Id = id
member this.Links = links
member this.Add (link: Node) =
links <- link::links
let rec GetMaxDistance (from: Node) (a: Node) =
a.Links |> Seq.filter (fun l -> l <> from) |> Seq.map (fun l -> 1 + (GetMaxDistance a l)) |> Seq.append [0] |> Seq.max
let linkCount = int(R()) (* the number of adjacency relations *)
let nodes = new Dictionary<int, Node>()
for i in 0 .. linkCount - 1 do
let inputs = R().Split [|' '|]
let xi = int(inputs.[0])
let yi = int(inputs.[1]); // the ID of a person which is adjacent to xi
let mutable nodeX:Node = Unchecked.defaultof<Node>
let mutable nodeY:Node = Unchecked.defaultof<Node>
if not <| nodes.TryGetValue(xi, &nodeX) then
nodeX <- new Node(xi)
nodes.Add(xi, nodeX)
if not <| nodes.TryGetValue(yi, &nodeY) then
nodeY <- new Node(yi)
nodes.Add(yi, nodeY)
nodeX.Add nodeY
nodeY.Add nodeX
()
let leaf = nodes.Values |> Seq.find (fun n -> n.Links.Length = 1)
let maxDist = GetMaxDistance leaf leaf
(* The minimal amount of steps required to completely propagate the advertisement *)
printfn "%d" <| int(Math.Round(float(maxDist)/float(2), MidpointRounding.AwayFromZero))