Toy Cars (Codeforces 545A)
Tags: Implement, implementation

Little Susie, thanks to her older brother, likes to play with cars. Today she decided to set up a tournament between them. The process of a tournament is described in the next paragraph.There are n toy cars. Each pair collides. The result of a collision can be one of the following: no car turned over, one car turned over, both cars turned over. A car is good if it turned over in no collision. The results of the collisions are determined by an n × n matrix А: there is a number on the intersection of the і-th row and j-th column that describes the result of the collision of the і-th and the j-th car:    - 1: if this pair of cars never collided.  - 1 occurs only on the main diagonal of the matrix.  0: if no car turned over during the collision.  1: if only the i-th car turned over during the collision.  2: if only the j-th car turned over during the collision.  3: if both cars turned over during the collision. Susie wants to find all the good cars. She quickly determined which cars are good. Can you cope with the task?

Input: The first line contains integer n (1 ≤ n ≤ 100) — the number of cars.Each of the next n lines contains n space-separated integers that determine matrix A. It is guaranteed that on the main diagonal there are  - 1, and  - 1 doesn't appear anywhere else in the matrix.It is guaranteed that the input is correct, that is, if Aij = 1, then Aji = 2, if Aij = 3, then Aji = 3, and if Aij = 0, then Aji = 0.

Output: Print the number of good cars and in the next line print their space-separated indices in the increasing order.


Sample Inputs:
3
-1 0 0
0 -1 1
0 2 -1

4
-1 3 3 3
3 -1 3 3
3 3 -1 3
3 3 3 -1

Sample Outputs:
2
1 3

0

Solution (Python):
n = int(input())
matrix = []
good_cars = []
for i in range(n):
    row = list(map(int, input().split()))
    matrix.append(row)
for i in range(n):
    is_good = True
    for j in range(n):
        if matrix[i][j] == 1 or matrix[i][j] == 3:
            is_good = False
            break
    if is_good:
        good_cars.append(i + 1)
print(len(good_cars))
print(' '.join(map(str, good_cars)))

Solution (Python):
n = int(input())
collisions = []
for i in range(n):
    row = list(map(int, input().split()))
    collisions.append(row)
good_cars = set(range(n))
for i in range(n):
    for j in range(i + 1, n):
        if collisions[i][j] == 1:
            good_cars.discard(i)
        elif collisions[i][j] == 2:
            good_cars.discard(j)
        elif collisions[i][j] == 3:
            good_cars.discard(i)
            good_cars.discard(j)
good_cars = sorted(list(good_cars))
print(len(good_cars))
print(' '.join((str(car + 1) for car in good_cars)))

Solution (Python):
n = int(input())
grid = [list(map(int, input().split())) for _ in range(n)]
ans = set((i + 1 for i in range(n)))
for i in range(n):
    for j in range(n):
        if i == j:
            break
        if grid[i][j] == 1:
            ans.discard(i + 1)
        elif grid[i][j] == 2:
            ans.discard(j + 1)
        elif grid[i][j] == 3:
            ans.discard(i + 1)
            ans.discard(j + 1)
print(len(ans))
print(*ans)