Problem Today, the high-class restaurant "Maizu Restaurant", which is completely…
- User
Problem
Today, the high-class restaurant "Maizu Restaurant", which is completely invited, has finally opened. Only members of this restaurant have the right to invite new members, initially only the owner of the restaurant with membership number 0.
Due to its luxury, the Maze Restaurant is only open for days from the first day of its opening. During that period, every day one of the members invites only one of his friends as a new member. Members invited on the day will be assigned a membership number , and the members will visit the store every day thereafter, including the day they are invited.
Since there is only a table for two in this restaurant, members eat in pairs as much as possible. Since the owner also participates in the meal, other members can also pair with the owner. However, all members are shy and only want to pair with the friend they invited or the friend they invited. Each member eats lonely if he / she cannot pair with a friend.
For each day of the day, find out how many pairs you can make when paired to maximize the number of pairs of friends.
Constraints
The input satisfies the following conditions.
Assistant
pythonn = int(input()) nodes = [{'parent': None, 'children': [], 'a': 0, 'b': 0}] for i in range(1, n+1): p_i = int(input()) nodes.append({'parent': p_i, 'children': [], 'a': 0, 'b': 0}) parent_node = p_i nodes[parent_node]['children'].append(i) curr_node = parent_node while curr_node is not None: sum_max = 0 for child in nodes[curr_node]['children']: sum_max += max(nodes[child]['a'], nodes[child]['b']) best_pair = 0 for child in nodes[curr_node]['children']: current = 1 + nodes[child]['a'] + (sum_max - max(nodes[child]['a'], nodes[child]['b'])) if current > best_pair: best_pair = current new_a = sum_max new_b = best_pair if nodes[curr_node]['children'] else 0 if nodes[curr_node]['a'] == new_a and nodes[curr_node]['b'] == new_b: break nodes[curr_node]['a'] = new_a nodes[curr_node]['b'] = new_b curr_node = nodes[curr_node]['parent'] print(max(nodes[0]['a'], nodes[0]['b']))
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.