human
I found an interesting problem on https://codeforces.com/problemset/problem/566/B:
A Large Software Company develops its own social network. Analysts have found that during the holidays, major sporting events and other significant events users begin to enter the network more frequently, resulting in great load increase on the infrastructure.

As part of this task, we assume that the social network is 4n processes running on the n servers. All servers are absolutely identical machines, each of which has a volume of RAM of 1 GB = 1024 MB ^{(1)}. Each process takes 100 MB of RAM on the server. At the same time, the needs of maintaining the viability of the server takes about 100 more megabytes of RAM. Thus, each server may have up to 9 different processes of social network.

Now each of the n servers is running exactly 4 processes. However, at the moment of peak load it is sometimes necessary to replicate the existing 4n processes by creating 8n new processes instead of the old ones. More formally, there is a set of replication rules, the i-th (1 ≤ i ≤ 4n) of which has the form of a_{i} → (b_{i}, c_{i}), where a_{i}, b_{i} and c_{i} (1 ≤ a_{i}, b_{i}, c_{i} ≤ n) are the numbers of servers. This means that instead of an old process running on server a_{i}, there should appear two new copies of the process running on servers b_{i} and c_{i}. The two new replicated processes can be on the same server (i.e., b_{i} may be equal to c_{i}) or even on the same server where the original process was (i.e. a_{i} may be equal to b_{i} or c_{i}). During the implementation of the rule a_{i} → (b_{i}, c_{i}) first the process from the server a_{i} is destroyed, then appears a process on the server b_{i}, then appears a process on the server c_{i}.

There is a set of 4n rules, destroying all the original 4n processes from n servers, and creating after their application 8n replicated processes, besides, on each of the n servers will be exactly 8 processes. However, the rules can only be applied consecutively, and therefore the amount of RAM of the servers imposes limitations on the procedure for the...

I tried it in Python, but could not do it. Can you solve it?
assistant
#Here is the Python program you requested.

from collections import deque
import random

class CodeforcesTask566BSolution:
    def __init__(self):
        self.result = ''
        self.n = 0
        self.rules = []

    def read_input(self):
        self.n = int(input())
        self.rules = [[int(x) for x in input().split(" ")] + [y + 1] for y in range(self.n * 4)]

    def process_task(self):

        loads = [4] * self.n
        random.shuffle(self.rules)
        to_use = deque(self.rules)
        order = []
        res = True
        ba = 0
        while to_use and res:
            moving = to_use.popleft()
            loads[moving[0] - 1] -= 1
            if loads[moving[1] - 1] < 9 and loads[moving[2] - 1] < 9 + (-1 if moving[2] == moving[1] else 0):
                ba = 0
                loads[moving[1] - 1] += 1
                loads[moving[2] - 1] += 1
                order.append(moving[3])
            else:
                ba += 1
                loads[moving[0] - 1] += 1
                to_use.append(moving)
            if ba > self.n * 12:
                res = False
        self.result = "NO" if not res else f"YES\n{' '.join([str(x) for x in order])}"

    def get_result(self):
        return self.result

def __starting_point():
    Solution = CodeforcesTask566BSolution()
    Solution.read_input()
    Solution.process_task()
    print(Solution.get_result())

__starting_point()