// File: /workspace/github_cpp_makefile_star2_O2/bobverity[P]MavericK/Hungarian.cpp
// Function: hungarian(std::vector<std::vector<double, std::allocator<double>>, std::allocator<std::vector<double, std::allocator<double>>>>&, std::vector<int, std::allocator<int>>&, std::vector<int, std::allocator<int>>&, std::vector<int, std::allocator<int>>&, std::vector<int, std::allocator<int>>&, bool, std::basic_ofstream<char, std::char_traits<char>>&)
vector<int> hungarian(vector< vector<double> > &M, vector<int> &edgesLeft, vector<int> &edgesRight, vector<int> &blockedLeft, vector<int> &blockedRight, bool outputLog_on, ofstream &outputLog_fileStream) {
    int n = int(M.size());
    
    // define maximum number of reps in Hungarian algorithm before aborting
    int maxReps= int(1e6);
    
    // initialise assignment objects
    int numberAssigned;
    
    // search for solution until maxReps reached
    for (int rep=0; rep<maxReps; rep++) {
        
        // zero assignment objects
        for (int i=0; i<n; i++) {
            edgesLeft[i] = -1;
            edgesRight[i] = -1;
        }
        numberAssigned = 0;
        
        // subtract smallest element from all rows and columns
        double minRow;
		vector<double> minCol = M[0];
        for (int i=0; i<int(M.size()); i++) {
            minRow = *min_element(begin(M[i]),end(M[i]));
            for (int j=0; j<int(M[i].size()); j++) {
                M[i][j] -= minRow;
                if (M[i][j]<minCol[j])
                    minCol[j] = M[i][j];
            }
        }
        for (int i=0; i<int(M.size()); i++) {
            for (int j=0; j<int(M[i].size()); j++) {
                M[i][j] -= minCol[j];
            }
        }
        
        // generate an initial matching
        for (int i=0; i<n; i++) {
            for (int j=0; j<n; j++) {
                if (M[i][j]==0 && edgesRight[j]<0) {
                    edgesLeft[i] = j;
                    edgesRight[j] = i;
                    numberAssigned++;
                    break;
                }
            }
        }
        
        // if this matching is perfect then we are done
        if (numberAssigned==n)
            return(edgesLeft);
        
        // continue augmenting paths until no more possible
        //vector<int> blockedLeft(n);
        //vector<int> blockedRight(n);
        bool continueAugmenting = true;
        while (continueAugmenting) {
            continueAugmenting = false;
            
            // search all unmatched nodes
            for (int i=0; i<n; i++) {
                if (edgesLeft[i]<0) {
                    
                    // attempt to find augmented path
                    blockedLeft = vector<int>(n);
                    blockedRight = vector<int>(n);
                    vector<int> path = augmentLeft(i, M, edgesRight, blockedLeft, blockedRight);
                    
                    // if successful then augment
                    if (path[0]>=0) {
                        continueAugmenting = true;
                        numberAssigned ++;
                        for (int j=0; j<int(path.size()/2); j++) {
                            edgesLeft[path[j*2+1]] = path[j*2];
                            edgesRight[path[j*2]] = path[j*2+1];
                        }
                        
                        // if best matching found then finish
                        if (numberAssigned==n) {
                            return(edgesLeft);
                        }
                    }
                }
            }
        }
        
        // find minimum value in cost matrix, looking at all elements in which neither the row or the column is part of the minimum vertex cover
        double minVal = -log(double(0));
        for (int i=0; i<n; i++) {
            for (int j=0; j<n; j++) {
                if (blockedLeft[i]==1 && blockedRight[j]==0 && M[i][j]<minVal) {
                    minVal = M[i][j];
                }
            }
        }
        
        // add or subtract this value from cost matrix as required
        for (int i=0; i<n; i++) {
            for (int j=0; j<n; j++) {
                if (blockedLeft[i]==1 && blockedRight[j]==0) {
                    M[i][j] -= minVal;
                }
                if (blockedLeft[i]==0 && blockedRight[j]==1) {
                    M[i][j] += minVal;
                }
            }
        }
        
        // at this point we have a new cost matrix and can repeat the process from the top
        
    } // rep loop
    
    cerrAndLog("\nError: failed to solve label switching problem. Hungarian algorithm failed to find best assignment within " + to_string((long long)maxReps) + string(" iterations given the following cost matrix:\n"), outputLog_on, outputLog_fileStream);
    for (int i=0; i<n; i++) {
        for (int j=0; j<n; j++) {
            cerrAndLog(to_string((long long)M[i][j])+string(" "), outputLog_on, outputLog_fileStream);
        }
        cerrAndLog("\n", outputLog_on, outputLog_fileStream);
    }
    
    exit(1);
    
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movq %r9, 0x38(%rsp)
movq %r8, 0x40(%rsp)
movq %rcx, 0x18(%rsp)
movq %rdx, %r13
movq %rdi, 0x30(%rsp)
movq 0x8(%rsi), %rax
movq %rsi, 0x10(%rsp)
subq (%rsi), %rax
pushq $0x18
popq %rcx
cqto
idivq %rcx
movslq %eax, %rbx
testl %ebx, %ebx
movl $0x0, %ebp
movq %rax, 0x28(%rsp)
cmovgl %eax, %ebp
movl %ebx, %r14d
xorl %esi, %esi
movq %rbp, 0x60(%rsp)
movq %r14, 0x50(%rsp)
cmpl $0xf4240, %esi          # imm = 0xF4240
je 0x5cb9
movq (%r13), %rax
movq 0x18(%rsp), %rcx
movq (%rcx), %rcx
xorl %edx, %edx
cmpq %rdx, %rbp
je 0x5893
orl $-0x1, (%rax,%rdx,4)
orl $-0x1, (%rcx,%rdx,4)
incq %rdx
jmp 0x5881
movl %esi, 0x4c(%rsp)
movq 0x10(%rsp), %r15
movq (%r15), %rsi
leaq 0x68(%rsp), %rdi
callq 0x537e
movq (%r15), %rcx
xorl %r15d, %r15d
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rax
subq %rcx, %rax
cqto
pushq $0x18
popq %rsi
idivq %rsi
movslq %eax, %rdx
cmpq %rdx, %r15
jge 0x593f
imulq $0x18, %r15, %r12
movq (%rcx,%r12), %rdi
movq 0x8(%rcx,%r12), %rsi
callq 0x63eb
movsd (%rax), %xmm0
movq 0x10(%rsp), %rax
movq (%rax), %rcx
movq (%rcx,%r12), %rax
movq 0x8(%rcx,%r12), %rdx
subq %rax, %rdx
shrq $0x3, %rdx
movq 0x68(%rsp), %rsi
testl %edx, %edx
movl $0x0, %edi
cmovlel %edi, %edx
xorl %edi, %edi
movq 0x50(%rsp), %r14
cmpq %rdi, %rdx
je 0x5937
movsd (%rax,%rdi,8), %xmm1
subsd %xmm0, %xmm1
movsd %xmm1, (%rax,%rdi,8)
movsd (%rsi,%rdi,8), %xmm2
ucomisd %xmm1, %xmm2
jbe 0x5932
movsd %xmm1, (%rsi,%rdi,8)
incq %rdi
jmp 0x590f
incq %r15
jmp 0x58af
testl %eax, %eax
movl $0x0, %edx
cmovlel %edx, %eax
xorl %edx, %edx
leaq 0x88(%rsp), %r15
cmpq %rax, %rdx
je 0x59a1
imulq $0x18, %rdx, %rdi
movq (%rcx,%rdi), %rsi
movq 0x8(%rcx,%rdi), %rdi
subq %rsi, %rdi
shrq $0x3, %rdi
movq 0x68(%rsp), %r8
testl %edi, %edi
movl $0x0, %r9d
cmovlel %r9d, %edi
xorl %r9d, %r9d
cmpq %r9, %rdi
je 0x599c
movsd (%rsi,%r9,8), %xmm0
subsd (%r8,%r9,8), %xmm0
movsd %xmm0, (%rsi,%r9,8)
incq %r9
jmp 0x5980
incq %rdx
jmp 0x5953
movq (%r13), %rax
xorl %edx, %edx
movl $0x0, 0x8(%rsp)
cmpq %rbp, %rdx
je 0x59fd
imulq $0x18, %rdx, %rdi
addq %rcx, %rdi
movq 0x18(%rsp), %rsi
movq (%rsi), %rsi
xorl %r8d, %r8d
cmpq %r8, %r14
je 0x59f8
movq (%rdi), %r9
movsd (%r9,%r8,8), %xmm0
ucomisd 0x2169c(%rip), %xmm0    # 0x27078
jne 0x59e7
jp 0x59e7
cmpl $0x0, (%rsi,%r8,4)
js 0x59ec
incq %r8
jmp 0x59c6
movl %r8d, (%rax,%rdx,4)
movl %edx, (%rsi,%r8,4)
incl 0x8(%rsp)
incq %rdx
jmp 0x59af
movl 0x8(%rsp), %eax
cmpl 0x28(%rsp), %eax
je 0x5c8b
movb $0x1, %al
movb %al, 0x7(%rsp)
testb $0x1, %al
je 0x5bad
xorl %r12d, %r12d
movq $0x0, 0x58(%rsp)
leaq 0xa8(%rsp), %r14
cmpq %rbp, %r12
je 0x5b7a
movq (%r13), %rax
cmpl $0x0, (%rax,%r12,4)
jns 0x5b72
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x5f7c
movq 0x40(%rsp), %rbp
movq %rbp, %rdi
movq %r15, %rsi
callq 0x6266
movq %r15, %rdi
callq 0x61d0
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x5f7c
movq %rbp, %r14
movq 0x38(%rsp), %rbp
movq %rbp, %rdi
movq %r15, %rsi
callq 0x6266
movq %r15, %rdi
callq 0x61d0
movq %r15, %rdi
movl %r12d, %esi
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %rcx
movq %r14, %r8
movq %rbp, %r9
callq 0x561c
movq 0x88(%rsp), %rax
cmpl $0x0, (%rax)
js 0x5b44
movq 0x90(%rsp), %rcx
subq %rax, %rcx
shrq $0x3, %rcx
movq (%r13), %rdx
movq 0x18(%rsp), %rsi
movq (%rsi), %rsi
testl %ecx, %ecx
movl $0x0, %edi
cmovlel %edi, %ecx
xorl %edi, %edi
leaq 0x88(%rsp), %r15
leaq 0xa8(%rsp), %r14
cmpq %rdi, %rcx
je 0x5b17
movslq (%rax,%rdi,8), %r8
movslq 0x4(%rax,%rdi,8), %r9
movl %r8d, (%rdx,%r9,4)
movl 0x4(%rax,%rdi,8), %r9d
movl %r9d, (%rsi,%r8,4)
incq %rdi
jmp 0x5af7
movl 0x8(%rsp), %eax
incl %eax
movb $0x1, %cl
movq %rcx, 0x58(%rsp)
cmpl 0x28(%rsp), %eax
jne 0x5b59
movq 0x30(%rsp), %rdi
movq %r13, %rsi
callq 0x5fce
xorl %ebp, %ebp
movq 0x28(%rsp), %rax
movl %eax, 0x8(%rsp)
jmp 0x5b60
movb $0x1, %bpl
leaq 0x88(%rsp), %r15
leaq 0xa8(%rsp), %r14
jmp 0x5b60
movl %eax, 0x8(%rsp)
movb $0x1, %bpl
movq %r15, %rdi
callq 0x61d0
testb %bpl, %bpl
movq 0x60(%rsp), %rbp
je 0x5b7a
incq %r12
jmp 0x5a2d
cmpq %rbx, %r12
movq 0x50(%rsp), %r14
movq 0x58(%rsp), %rax
jge 0x5a0d
leaq 0x68(%rsp), %rdi
callq 0x51a2
movl 0x4c(%rsp), %esi
incl %esi
testb $0x1, 0x7(%rsp)
je 0x5867
jmp 0x5ca2
xorpd %xmm0, %xmm0
callq 0x32d0
xorpd 0x21472(%rip), %xmm0    # 0x27030
movq 0x40(%rsp), %rax
movq (%rax), %rax
movq 0x38(%rsp), %rcx
movq (%rcx), %rcx
movq 0x10(%rsp), %rdx
movq (%rdx), %rdx
xorl %esi, %esi
cmpq %rbp, %rsi
je 0x5c16
imulq $0x18, %rsi, %rdi
addq %rdx, %rdi
xorl %r8d, %r8d
cmpq %r8, %r14
je 0x5c11
cmpl $0x1, (%rax,%rsi,4)
jne 0x5c0c
cmpl $0x0, (%rcx,%r8,4)
jne 0x5c0c
movq (%rdi), %r9
movsd (%r9,%r8,8), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x5c0c
movapd %xmm1, %xmm0
incq %r8
jmp 0x5be7
incq %rsi
jmp 0x5bd8
xorl %eax, %eax
cmpq %rbp, %rax
je 0x5b8d
movq 0x40(%rsp), %rcx
movq (%rcx), %rcx
movq 0x38(%rsp), %rdx
movq (%rdx), %rdx
imulq $0x18, %rax, %rsi
movq 0x10(%rsp), %rdi
addq (%rdi), %rsi
xorl %edi, %edi
cmpq %rdi, %r14
je 0x5c86
movl (%rcx,%rax,4), %r8d
testl %r8d, %r8d
je 0x5c68
cmpl $0x1, %r8d
jne 0x5c81
cmpl $0x0, (%rdx,%rdi,4)
jne 0x5c81
movq (%rsi), %r8
movsd (%r8,%rdi,8), %xmm1
subsd %xmm0, %xmm1
jmp 0x5c7b
cmpl $0x1, (%rdx,%rdi,4)
jne 0x5c81
movq (%rsi), %r8
movsd (%r8,%rdi,8), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, (%r8,%rdi,8)
incq %rdi
jmp 0x5c3f
incq %rax
jmp 0x5c18
movq 0x30(%rsp), %rdi
movq %r13, %rsi
callq 0x5fce
leaq 0x68(%rsp), %rdi
callq 0x51a2
movq 0x30(%rsp), %rax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x88(%rsp), %rbx
movl $0xf4240, %esi          # imm = 0xF4240
movq %rbx, %rdi
callq 0x60a6
leaq 0x213ab(%rip), %rsi     # 0x27080
leaq 0x68(%rsp), %rdi
movq %rbx, %rdx
callq 0x6082
leaq 0x21403(%rip), %rsi     # 0x270ec
leaq 0xa8(%rsp), %rdi
leaq 0x27(%rsp), %rdx
callq 0x3130
leaq 0xc8(%rsp), %rdi
leaq 0x68(%rsp), %rsi
leaq 0xa8(%rsp), %rdx
callq 0x601b
movzbl 0x160(%rsp), %esi
leaq 0xc8(%rsp), %rdi
movl %esi, 0x18(%rsp)
movq 0x168(%rsp), %rdx
callq 0x176c7
leaq 0xc8(%rsp), %rdi
callq 0x3170
leaq 0xa8(%rsp), %rdi
callq 0x3170
leaq 0x68(%rsp), %r15
movq %r15, %rdi
callq 0x3170
leaq 0x88(%rsp), %r12
movq %r12, %rdi
callq 0x3170
movl 0x28(%rsp), %ebp
xorl %ebx, %ebx
leaq 0x108(%rsp), %r14
cmpq 0x60(%rsp), %rbx
je 0x5e49
movq %rbx, 0x8(%rsp)
imulq $0x18, %rbx, %r13
xorl %ebx, %ebx
cmpq %rbx, %rbp
je 0x5e06
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq (%rax,%r13), %rax
cvttsd2si (%rax,%rbx,8), %rsi
movq %r15, %rdi
callq 0x60a6
movq %r12, %rdi
leaq 0x23c89(%rip), %rsi     # 0x29a43
leaq 0xa8(%rsp), %rdx
callq 0x3130
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x601b
movq %r14, %rdi
movl 0x18(%rsp), %esi
movq 0x168(%rsp), %rdx
callq 0x176c7
movq %r14, %rdi
callq 0x3170
movq %r12, %rdi
callq 0x3170
movq %r15, %rdi
callq 0x3170
incq %rbx
jmp 0x5d91
leaq 0xe8(%rsp), %r13
movq %r13, %rdi
leaq 0x21aa3(%rip), %rsi     # 0x278bb
movq %r15, %rdx
callq 0x3130
movq %r13, %rdi
movl 0x18(%rsp), %esi
movq 0x168(%rsp), %rdx
callq 0x176c7
movq 0x8(%rsp), %rbx
movq %r13, %rdi
callq 0x3170
incq %rbx
jmp 0x5d7b
pushq $0x1
popq %rdi
callq 0x33e0
movq %rax, %rbx
leaq 0xe8(%rsp), %rdi
jmp 0x5ed4
movq %rax, %rbx
jmp 0x5f00
movq %rax, %rbx
jmp 0x5e8d
movq %rax, %rbx
leaq 0x108(%rsp), %rdi
callq 0x3170
jmp 0x5e80
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x3170
leaq 0x68(%rsp), %rdi
jmp 0x5ed4
jmp 0x5ef3
movq %rax, %rbx
leaq 0xc8(%rsp), %rdi
callq 0x3170
jmp 0x5eab
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0x3170
jmp 0x5ebd
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x3170
jmp 0x5ecc
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x3170
jmp 0x5f00
jmp 0x5ef3
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x61d0
jmp 0x5ef6
jmp 0x5ef3
jmp 0x5ef3
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x51a2
movq %rbx, %rdi
callq 0x35a0