Sort by

recency

|

8 Discussions

|

  • + 0 comments

    import sys

    def solve(): data = sys.stdin.read().split() n = int(data[0]); m = int(data[1]); d = int(data[2])

    # Precompute edge masks: edge[bit][u] = bitmask of neighbors via edge labeled `bit`
    edge = [0] * n
    edge0 = edge[:]
    edge1 = edge[:]
    
    idx = 3
    for _ in range(m):
        u = int(data[idx]) - 1
        v = int(data[idx+1]) - 1
        bit = int(data[idx+2])
        if bit == 0:
            edge0[u] |= (1 << v)
            if u != v:
                edge0[v] |= (1 << u)
        else:
            edge1[u] |= (1 << v)
            if u != v:
                edge1[v] |= (1 << u)
        idx += 3
    
    # dp[mask] = bitmask of nodes reachable with binary path `mask`
    dp = [0] * (1 << d)
    dp[0] = 1  # Start at node 0 (house 1)
    
    # Build DP for each path length
    for i in range(d):
        new_dp = [0] * (1 << (i + 2))
        for mask in range(len(dp)):
            if not dp[mask]:
                continue
            nodes = dp[mask]
            # Append 0
            next_nodes0 = 0
            for u in range(n):
                if nodes & (1 << u):
                    next_nodes0 |= edge0[u]
            new_dp[mask << 1] |= next_nodes0
    
            # Append 1
            next_nodes1 = 0
            for u in range(n):
                if nodes & (1 << u):
                    next_nodes1 |= edge1[u]
            new_dp[(mask << 1) | 1] |= next_nodes1
        dp = new_dp
    
    # Count distinct masks of length d that are reachable
    result = sum(1 for i in range(1 << d) if dp[i])
    print(result)
    

    solve()

  • + 1 comment

    Here is Clues on a Binary Path problem solution - https://programs.programmingoneonone.com/2021/07/hackerrank-clues-on-a-binary-path-problem-solution.html

  • + 0 comments

    I does not understand the problem can anyone explain it clearly, what is it

  • + 0 comments

    Why does my last case does not get submitted? 524288

  • + 0 comments

    interesting problem, had a lot of fun micro-optimizing it, though with that said it really should be marked advanced, overall neither I nor the editorial could reduce the time complexity to 2^d or lower, i.e. constant overhead; the best I got was a factor of ceil(n/w) where w is the largest machine word in bits while the editorial is at n; note that the editorial has little to no micro-optimizations, as such the likelyhood of actually passing all the testscases without compile optimization flags is very low, its still a good start, good luck.