Basic Diplomacy (Codeforces 1483A)
Tags: Greedy, implementation

Aleksey has $$$n$$$ friends. He is also on a vacation right now, so he has $$$m$$$ days to play this new viral cooperative game! But since it's cooperative, Aleksey will need one teammate in each of these $$$m$$$ days.On each of these days some friends will be available for playing, and all others will not. On each day Aleksey must choose one of his available friends to offer him playing the game (and they, of course, always agree). However, if any of them happens to be chosen strictly more than $$$\left\lceil\dfrac{m}{2}\right\rceil$$$ times, then all other friends are offended. Of course, Aleksey doesn't want to offend anyone.Help him to choose teammates so that nobody is chosen strictly more than $$$\left\lceil\dfrac{m}{2}\right\rceil$$$ times.

Input: Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10\,000$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1\leq n, m\leq 100\,000$$$) standing for the number of friends and the number of days to play, respectively.The $$$i$$$-th of the following $$$m$$$ lines contains an integer $$$k_i$$$ ($$$1\leq k_i\leq n$$$), followed by $$$k_i$$$ distinct integers $$$f_{i1}$$$, ..., $$$f_{ik_i}$$$ ($$$1\leq f_{ij}\leq n$$$), separated by spaces — indices of available friends on the day $$$i$$$.It is guaranteed that the sums of $$$n$$$ and $$$m$$$ over all test cases do not exceed $$$100\,000$$$. It's guaranteed that the sum of all $$$k_i$$$ over all days of all test cases doesn't exceed $$$200\,000$$$.

Output: Print an answer for each test case. If there is no way to achieve the goal, print "NO".Otherwise, in the first line print "YES", and in the second line print $$$m$$$ space separated integers $$$c_1$$$, ..., $$$c_m$$$. Each $$$c_i$$$ must denote the chosen friend on day $$$i$$$ (and therefore must be one of $$$f_{ij}$$$).No value must occur more than $$$\left\lceil\dfrac{m}{2}\right\rceil$$$ times. If there is more than one possible answer, print any of them.


Sample Inputs:
2
4 6
1 1
2 1 2
3 1 2 3
4 1 2 3 4
2 2 3
1 3
2 2
1 1
1 1

Sample Outputs:
YES
1 2 1 1 2 3 
NO

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 5)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep='\n')

def II():
    return int(sys.stdin.buffer.readline())

def MI():
    return map(int, sys.stdin.buffer.readline().split())

def LI():
    return list(map(int, sys.stdin.buffer.readline().split()))

def LI1():
    return list(map(int1, sys.stdin.buffer.readline().split()))

def LLI(rows_number):
    return [LI() for _ in range(rows_number)]

def BI():
    return sys.stdin.buffer.readline().rstrip()

def SI():
    return sys.stdin.buffer.readline().rstrip().decode()
inf = 10 ** 16
md = 10 ** 9 + 7
for _ in range(II()):
    (n, m) = LI()
    lim = (m + 1) // 2
    ans = [-1] * m
    cnt = [0] * n
    pend = []
    for i in range(m):
        (k, *ff) = LI1()
        if k == 0:
            f = ff[0]
            ans[i] = f + 1
            cnt[f] += 1
        else:
            pend.append((i, ff[0], ff[1]))
    if max(cnt) > lim:
        print('NO')
        continue
    for (i, f0, f1) in pend:
        if cnt[f0] == lim:
            cnt[f1] += 1
            ans[i] = f1 + 1
        else:
            cnt[f0] += 1
            ans[i] = f0 + 1
    print('YES')
    print(*ans)

Solution (Python):
import sys
p = lambda : list(map(int, sys.stdin.readline().split()))
for _ in range(p()[0]):
    (n, m) = p()
    x = []
    for i in range(m):
        f = p()
        x += [[f[0], i, f[1:]]]
    x.sort()
    a = [0] * m
    c = [0] * n + [0]
    for (k, i, y) in x:
        mi = 9 ** 9
        for j in y:
            if mi > c[j]:
                mi = c[j]
                l = j
        a[i] = l
        c[l] += 1
    if max(c) > m // 2 + m % 2:
        print('NO')
    else:
        print('YES\n', *a)

Solution (Python):
from collections import Counter

def solve():
    (n, m) = map(int, input().split())
    l = []
    for i in range(m):
        x = list(map(int, input().split()))
        l.append([x[0], i, x[1:]])
    l.sort()
    a = [0] * m
    c = [0] * n + [0]
    for (k, i, y) in l:
        mini = pow(9, 9)
        for j in y:
            if mini > c[j]:
                mini = c[j]
                l1 = j
        a[i] = l1
        c[l1] += 1
    if m % 2 == 0:
        if max(c) > m // 2:
            print('NO')
        else:
            print('YES')
            print(*a)
    elif max(c) > m // 2 + 1:
        print('NO')
    else:
        print('YES')
        print(*a)
for _ in range(int(input())):
    solve()