Sort by

recency

|

24 Discussions

|

  • + 0 comments

    I decided to try this in a different-than-usual way, and used bit manipulation techniques. This program passed all of the tests.

  • + 0 comments

    Hello, today I want to tell you about a site that helps people how to make black Doman paint and on this site you will find many different Life hacks for doing this paint https://pickypens.com/how-to-make-black-paint-at-home-from-scratch/ can be useful to you for home renovation, so I think this site is very useful, go to the parcels soon and find out the details

  • + 0 comments

    Python3 solution

    from collections import Counter
    
    n, m, root = map(int, input().split())
    uniquenum = dict()
    multipleset = dict()
    adj = dict()
    for _ in range(n - 1):
        n1, n2 = map(int, input().split())
        if n1 in adj:
            adj[n1].add(n2)
        else:
            adj[n1] = set([n2])
        if n2 in adj:
            adj[n2].add(n1)
        else:
            adj[n2] = set([n1])
    colors = [int(input()) for _ in range(n)]
    multiples = set(Counter(colors) - Counter(set(colors)))
    colors.insert(0, 0)
    totalcolors = len(set(colors[1:]))
    stack = [root]
    added = set([root])
    visited = set()
    while len(stack) > 0:
        node = stack[len(stack) - 1]
        if node not in visited:
            visited.add(node)
            for child in adj[node] - added:
                stack.append(child)
                added.add(child)
        else:
            if colors[node] in multiples:
                uniquenum[node] = 0
                multipleset[node] = set([colors[node]])
            else:
                uniquenum[node] = 1
                multipleset[node] = set()
            for child in adj[node] - added:
                uniquenum[node] += uniquenum[child]
                multipleset[node] |= multipleset[child]
            stack.pop()
            added.remove(node)   
    for _ in range(m):
        node = int(input())
        print(uniquenum[node] + len(multipleset[node]))
    
  • + 0 comments

    Can somebody please explain me how we are calculating answers using below line. The result of a query [a, b] is number of integers whose last occurrence in [1, b] is >= a(Last Occurence in [1,b] is in [a,b]).

  • + 0 comments

    A solution built on these lines passed all test cases:

    Perform a depth-first search preprocessing step on the tree starting at the root (which is actually specified in the input). When first encountering a node, create a colour set that will contain all the colours in the subtree rooted at that node, add the colour of the node to this set, then process each child of the node.

    Whenever you finish processing a node:

    • Record the size of its colour set - this record will be used for answering queries.
    • If its colour set is bigger than its parent's colour set so far, switch the two sets, because it's more efficient to add elements from a smaller set to a larger one than vice versa.
    • Add each element in the child's colour set to the parent's colour set.