EaglePB2's Competitive Programming Writeups
  • Home Page
  • Why Python?
  • Why 10^9 + 7?
  • General Formatting Title
  • 🇲🇾CodeNection
    • 2021
      • Closed Cateogry
        • Attend Talks
        • Distant Relatives
        • Concert
        • Mamak
        • Fair Contest
      • Open Preliminary Round
        • f(Aibohphobia)^-1
        • Did they cheat?
        • Semester Breaks
      • Open Final Round
        • Ways 1
        • Circular Campus
        • A joke
        • 🥰Last year when life was better
        • Thank You Pizza
    • 2023
      • Test Round
        • Codey and Alphabetical Scoring
        • Codey and Binary Guesser
      • Preliminary Round
        • Codey and CodeNection
        • Codey and Hide-and-Seek
        • Codey and Math
        • Codey and Textbooks
        • Codey and Money
        • Codey and Team Selection
        • Codey and Painted Tree
        • Codey and Number Grid
        • Codey and Crimes
      • Final Round
        • Codey and CodeNection 2
        • Codey and Connection
        • Codey and Schedule
        • Codey and School Supplies
        • Codey and Zombies
        • Codey and Sightseeing
        • Codey and Apples
        • Codey and Facto
        • Codey and Zoey
    • 2024
      • Test Round
        • Codey and Sunday
        • Codey and Takoyaki
      • Preliminary Round
        • Codey and CodeNection
        • Codey and Pebbles
        • Codey and Spam
        • Codey and Coins
        • Codey and Rectangles
        • Codey and Manuscript
        • Codey and Recipes
        • Codey and Toy Kingdom
        • Codey and Peaks
      • Final Round
        • Codey and Exit
        • Codey and Gardening
        • Codey and Symbol
        • Codey and Rectangles 2
        • Codey and Jutsu
        • Codey and Toy Kingdom 2
        • Codey and Speeches
  • ABaKoDa
    • 2023
      • Round 1
        • Problem Letters
        • Problem Statistics
        • Rankings Order
        • Rankings Search
      • Round 2
        • Abakoda Letters
        • Borrowed Words
        • Kensorship
        • Duel Languages
  • Meta Coding Competitions
    • 2011
      • Qualification Round
        • Double Squares
        • Peg Game
        • Studious Student
      • Round 1A
        • Diversity Number
        • Turn on the Lights
        • Wine Tasting
      • Round 1B
        • Chess 2
        • Diminishing Circle
        • Slot Machine Hacker
      • Round 1C
        • N-Factorful
        • Polynomial Factoring
        • Risky Slide
      • Round 2
        • Bonus Assignments
        • Scott's New Trick
        • Studious Student II
      • Final Round
        • Alien Game
        • Party Time
        • Safest Place
  • EaglePB2's Special
    • Hong Kong Identity card
    • Cycle Prefix Averages
    • Word Squares
Powered by GitBook
On this page
  • Question
  • Input Format
  • Constraints
  • Output Format
  • Sample Inputs:
  1. CodeNection
  2. 2021
  3. Closed Cateogry

Distant Relatives

https://www.hackerrank.com/contests/codenection-2021-closed-category/challenges/distant-relatives

Question

Kyle has a family tree. It all started from a mysterious person, we call this person 1, the person had children, their children have children and so on. Any two children who share a common ancestor are called relatives, Kyle got an assignment from MMU to find how wide his family tree has spanned over the years, your task it to find the farthest relatives in his family tree to help him out.

(Edit : In the input, relationship means they have a connection. 1 4 means 1 and 4 have a connection. Either 1 is an ancestor of 4 or 4 is an ancestor of 1)

Input Format

Input consists of an integer N , the number of relatives and next N-1 lines contain relationship between children.

Constraints

1≤n≤2∗1051 \le n \le 2*10^51≤n≤2∗105
1≤a,b≤n1 \le a, b \le n1≤a,b≤n

Output Format

Output a single integer, the farthest distance of any two relatives on the family tree.

Sample Inputs:

Input:

10
4 1
6 5
7 2
6 3
1 7
2 10
10 9
3 8
8 9

Output:

9

Solution - Graph Traversal

A double breadth-first search-based graph traversal is possible to solve the problem.

from collections import defaultdict, deque

def longest_path(edges, n):
    graph = defaultdict(list)
    for start, end in edges:
        graph[start].append(end)
        graph[end].append(start)
    
    def bfs(start):
        visited = set()
        queue = deque([(start, 1)])
        visited.add(start)
        max_dist = 1
        furthest_node = start
        
        while queue:
            node, dist = queue.popleft()
            if dist > max_dist:
                max_dist = dist
                furthest_node = node
                
            for neighbor in graph[node]:
                if neighbor not in visited:
                    visited.add(neighbor)
                    queue.append((neighbor, dist + 1))
                    
        return furthest_node, max_dist

    start_node, _ = bfs(1)
    
    _, max_length = bfs(start_node)
    
    return max_length

n = int(input())
edges = []
for _ in range(n-1):
    a, b = map(int, input().split())
    edges.append((a, b))

print(longest_path(edges, n) - 1) # Why additional -1?

Note:

  • This solution uses collections, which sometimes is not allowed in contest.

  • This requires a magic number, "-1" to get all test cases.

Why DFS won't work? (yet)

Simple because, recursive depth limit exceeded for large values.

I had tried to use tree building method with DFS, yet 3 of the test cases hit runtime error due to recursive limit reached. Although that, I decide to share the current DFS method, just in case if someone has another good idea to optimize the recursive depth.

def longest(start):
        max_d = [0]
        
        def dfs2(u, p):
            max_len = [0, 0]
            for v in g[u]:
                if v != p:
                    curr = dfs2(v, u)
                    if curr + 1 > max_len[0]:
                        max_len[1] = max_len[0]
                        max_len[0] = curr + 1
                    elif curr + 1 > max_len[1]:
                        max_len[1] = curr + 1
            max_d[0] = max(max_d[0], max_len[0] + max_len[1])
            return max_len[0]
        
        dfs2(start, -1)
        return max_d[0]

n = int(input())
g = [[] for _ in range(n+1)]
    
for _ in range(n-1):
    a, b = map(int, input().split())
    g[a].append(b)
    g[b].append(a)

print(longest(1))

PreviousAttend TalksNextConcert

Last updated 2 months ago

🇲🇾