// File: /workspace/github_cpp_makefile_star2_O2/xukmin[P]rase/routing-metric-calculators.cc
// Function: LatencyCalculator::CalculateMetric(SensorNetwork const&)
double LatencyCalculator::CalculateMetric(const SensorNetwork& network) {
  // TODO: Make sensing_range and position arguments.
  const double sensing_range = 15.0;
  const Position position(50.0, 50.0);

  std::vector<int> triggered;
  if (!network.FindSensorsWithinRange(position, sensing_range, &triggered)) {
    // No sensor will ever be triggered by the event.
    return 0.0;
  }

  std::vector<bool> active(network.num_sensors());
  for (int i = 0; i < triggered.size(); i++) {
    int parent = triggered[i];
    while (parent >= 0 && !active[parent]) {
      active[parent] = true;
      parent = network.GetParent(parent);
    }
  }

  std::vector<int> children(network.num_sensors());
  // Skip the base station which does not have a parent.
  for (int i = 1; i < network.num_sensors(); i++) {
    assert(network.GetParent(i) >= 0);
    if (active[i]) {
      children[network.GetParent(i)]++;
    }
  }

  std::vector<int> timestamps(network.num_sensors());
  std::vector<bool> visited(network.num_sensors());
  int num_to_visit = network.num_sensors();
  // Ignore the inactive sensors by marking them as visited.
  for (int i = 0; i < network.num_sensors(); i++) {
    visited[i] = !active[i];
    num_to_visit -= visited[i];
  }
  while (num_to_visit > 0) {
    std::vector<int> leaves;
    for (int i = 0; i < network.num_sensors(); i++) {
      if (!visited[i] && children[i] == 0) {
        int parent = network.GetParent(i);
        if (parent >= 0) {
          assert(active[parent]);
          timestamps[parent] = std::max(timestamps[parent], timestamps[i]) + 1;
        }
        leaves.push_back(i);
        visited[i] = true;
        num_to_visit--;
      }
    }
    for (int i = 0; i < leaves.size(); i++) {
      int parent = network.GetParent(leaves[i]);
      if (parent >= 0) {
        children[parent]--;
      }
    }
  }

  return timestamps[0];
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %rsi, %rbx
movaps 0x2701(%rip), %xmm0     # 0xc1b0
leaq 0xd0(%rsp), %rsi
movaps %xmm0, (%rsi)
xorps %xmm0, %xmm0
leaq 0x70(%rsp), %rdx
movaps %xmm0, (%rdx)
andq $0x0, 0x10(%rdx)
movsd 0x26ee(%rip), %xmm0     # 0xc1c0
movq %rbx, %rdi
callq 0x5c6e
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
testb %al, %al
je 0x9dfb
movq 0x8(%rbx), %rax
subq (%rbx), %rax
pushq $0x48
popq %r13
cqto
idivq %r13
movslq %eax, %rsi
leaq 0x90(%rsp), %rdi
leaq 0x48(%rsp), %rdx
callq 0x662e
xorl %r12d, %r12d
leaq 0x90(%rsp), %r14
movq 0x70(%rsp), %rax
movq 0x78(%rsp), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
cmpq %r12, %rcx
jbe 0x9b71
leaq (%rax,%r12,4), %rax
movslq (%rax), %r15
testq %r15, %r15
js 0x9b6c
movq %r14, %rdi
movq %r15, %rsi
callq 0x665e
testq %rdx, (%rax)
jne 0x9b6c
movq %r14, %rdi
movq %r15, %rsi
callq 0x665e
orq %rdx, (%rax)
movq (%rbx), %rax
imulq $0x48, %r15, %rcx
addq %rcx, %rax
addq $0x44, %rax
jmp 0x9b36
incq %r12
jmp 0x9b1c
movq 0x8(%rbx), %rax
subq (%rbx), %rax
cqto
idivq %r13
movslq %eax, %rsi
leaq 0x30(%rsp), %rdi
leaq 0x48(%rsp), %rdx
callq 0x9ed4
pushq $0x1
popq %r14
movl $0x8c, %r12d
leaq 0x90(%rsp), %r15
movq (%rbx), %rcx
movq 0x8(%rbx), %rax
subq %rcx, %rax
cqto
idivq %r13
movslq %eax, %rdx
cmpq %rdx, %r14
jge 0x9beb
cmpl $0x0, (%rcx,%r12)
js 0x9e3c
movq %r15, %rdi
movq %r14, %rsi
callq 0x665e
testq %rdx, (%rax)
je 0x9be2
movq (%rbx), %rax
movslq (%rax,%r12), %rax
movq 0x30(%rsp), %rcx
incl (%rcx,%rax,4)
incq %r14
addq $0x48, %r12
jmp 0x9ba1
movslq %eax, %rsi
leaq 0xb8(%rsp), %rdi
leaq 0x48(%rsp), %rdx
callq 0x9ed4
movq 0x8(%rbx), %rax
subq (%rbx), %rax
cqto
idivq %r13
movslq %eax, %rsi
leaq 0x48(%rsp), %rdi
leaq 0x10(%rsp), %rdx
callq 0x662e
movq (%rbx), %rsi
movq 0x8(%rbx), %rcx
movq %rcx, %rax
subq %rsi, %rax
cqto
idivq %r13
movq %rax, %r14
xorl %r15d, %r15d
subq %rsi, %rcx
movq %rcx, %rax
cqto
idivq %r13
cltq
cmpq %rax, %r15
jge 0x9ca2
leaq 0x90(%rsp), %rdi
movq %r15, %rsi
callq 0x665e
movq %rdx, %rbp
movq (%rax), %r12
leaq 0x48(%rsp), %rdi
movq %r15, %rsi
callq 0x665e
testq %rbp, %r12
je 0x9c78
notq %rdx
andq (%rax), %rdx
jmp 0x9c7b
orq (%rax), %rdx
movq %rdx, (%rax)
leaq 0x48(%rsp), %rdi
movq %r15, %rsi
callq 0x665e
andq (%rax), %rdx
cmpq $0x1, %rdx
adcl $-0x1, %r14d
incq %r15
movq (%rbx), %rsi
movq 0x8(%rbx), %rcx
jmp 0x9c36
leaq 0x48(%rsp), %r15
leaq 0x10(%rsp), %rbp
testl %r14d, %r14d
jle 0x9dbd
xorpd %xmm0, %xmm0
movapd %xmm0, 0x10(%rsp)
andq $0x0, 0x20(%rsp)
xorl %ecx, %ecx
movl %ecx, 0x4(%rsp)
movq (%rbx), %rsi
movq 0x8(%rbx), %rax
subq %rsi, %rax
cqto
idivq %r13
cmpl %eax, %ecx
jge 0x9d78
movslq %ecx, %rsi
movq %r15, %rdi
callq 0x665e
movl 0x4(%rsp), %ecx
testq %rdx, (%rax)
jne 0x9d71
movslq %ecx, %rax
movq 0x30(%rsp), %rdx
cmpl $0x0, (%rdx,%rax,4)
jne 0x9d71
movq (%rbx), %rcx
imulq $0x48, %rax, %rax
movslq 0x44(%rcx,%rax), %r12
testq %r12, %r12
js 0x9d4d
leaq 0x90(%rsp), %rdi
movq %r12, %rsi
callq 0x665e
testq %rdx, (%rax)
je 0x9e1d
movq 0xb8(%rsp), %rax
movslq 0x4(%rsp), %rcx
movl (%rax,%r12,4), %edx
movl (%rax,%rcx,4), %ecx
cmpl %ecx, %edx
cmovgl %edx, %ecx
incl %ecx
movl %ecx, (%rax,%r12,4)
movq %rbp, %rdi
leaq 0x4(%rsp), %rsi
callq 0x65f2
movslq 0x4(%rsp), %rsi
movq %r15, %rdi
callq 0x665e
orq %rdx, (%rax)
decl %r14d
movl 0x4(%rsp), %ecx
incl %ecx
jmp 0x9cc7
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
movq 0x30(%rsp), %rdx
xorl %edi, %edi
cmpq %rdi, %rcx
je 0x9db0
movslq (%rax,%rdi,4), %r8
imulq $0x48, %r8, %r8
movslq 0x44(%rsi,%r8), %r8
testq %r8, %r8
js 0x9dab
decl (%rdx,%r8,4)
incq %rdi
jmp 0x9d90
movq %rbp, %rdi
callq 0x7156
jmp 0x9cac
leaq 0xb8(%rsp), %rbx
movq (%rbx), %rax
cvtsi2sdl (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
leaq 0x48(%rsp), %rdi
callq 0x667c
movq %rbx, %rdi
callq 0x7156
leaq 0x30(%rsp), %rdi
callq 0x7156
leaq 0x90(%rsp), %rdi
callq 0x667c
leaq 0x70(%rsp), %rdi
callq 0x7156
movsd 0x8(%rsp), %xmm0
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2576(%rip), %rdi      # 0xc39a
leaq 0x23cf(%rip), %rsi      # 0xc1fa
leaq 0x251f(%rip), %rcx      # 0xc351
movl $0xf0, %edx
callq 0x3110
leaq 0x24f4(%rip), %rdi      # 0xc337
leaq 0x23b0(%rip), %rsi      # 0xc1fa
leaq 0x2500(%rip), %rcx      # 0xc351
movl $0xdc, %edx
callq 0x3110
movq %rax, %rbx
jmp 0x9e9d
jmp 0x9e74
jmp 0x9e81
jmp 0x9e66
movq %rax, %rbx
jmp 0x9ec1
jmp 0x9e6f
jmp 0x9e6f
movq %rax, %rbx
jmp 0x9e93
movq %rax, %rbx
jmp 0x9eaa
jmp 0x9e86
jmp 0x9e86
jmp 0x9e86
jmp 0x9e81
movq %rax, %rbx
jmp 0x9eb4
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x7156
leaq 0x48(%rsp), %rdi
callq 0x667c
leaq 0xb8(%rsp), %rdi
callq 0x7156
leaq 0x30(%rsp), %rdi
callq 0x7156
leaq 0x90(%rsp), %rdi
callq 0x667c
leaq 0x70(%rsp), %rdi
callq 0x7156
movq %rbx, %rdi
callq 0x3360
nop