Problem:


品質検査

問題

 あなたはある機械の製造工場で品質管理の責任者をしている． この機械には， 部品として電源とモーターとケーブルが必要である． 製造工場には電源が a 個， モーターが b 個， ケーブルが c 個あり， それぞれ 1 から a まで， a+1 から a+b まで， a+b+1 から a+b+c までの番号が付いている． 困ったことに， 部品の中に故障しているものがあるかもしれない． 工場ではどの部品が故障していてどの部品が正常であるかを知りたい．

そこで， 工場では次の方法で部品を検査した． 電源とモーターとケーブルを１つずつ持ってきてつなぎ， 動作させてみる． このとき， ３つの部品がすべて正常であるときは正しく動作して「合格」とわかる． ３つの中に故障している部品が１つでも入っているときは正しく動作しないので「不合格」とわかる． （工場で作っている機械はとても精密なので， 故障した部品がまざっているのに偶然正しく動作してしまうなどということは起きないのだ．）

あなたには検査結果のリストが渡される． 検査結果のリストの各行には， 検査に使った電源とモーターとケーブルの番号と， 検査が合格だったか不合格だったかが書かれている．

検査結果のリストが与えられたとき， すべての部品を， 検査結果から確実に故障しているとわかる部品と， 確実に正常とわかる部品と， 検査結果からは故障しているとも正常であるとも決まらない部品に分類するプログラムを作成せよ． 

入力

入力は複数のデータセットからなる．各データセットの形式は以下の通りである．入力は３つのゼロを含む行で終了する．

1 行目には 3 個の整数が空白区切りで書かれており， 順に電源の個数 a, モーターの個数 b, ケーブルの個数 c を表す．

2 行目には 1 個の整数が書かれており， 検査結果のリストに含まれる検査の回数 N が書かれている．

続く N 行は検査結果のリストを表す． 各行には， 4 個の整数 i, j, k, r が１つの空白を区切りとして書かれており， 電源 i とモーター j とケーブル k をつないで検査した結果が， 「合格」 (r=1 のとき) か「不合格」 (r=0 のとき) だったことを表す.

a, b, c, N は 1 ≤ a, b, c ≤ 100, 1 ≤ N ≤ 1000 を満たす. 

データセットの数は 5 を超えない．

出力

データセットごとに以下の形式で出力する．各データセットの出力は a+b+c 行からなる.

 i 行目 (1 ≤ i ≤ a+b+c)：

	
	

    
 検査結果から部品 i が故障しているとわかる場合は 0 を出力する.

    
 検査結果から部品 i が正常とわかる場合は 1 を出力する.

    
 検査結果から部品 i が故障しているか正常であるかが決まらない場合は 2 を出力する.

入出力例

入力例
 

2 2 2   

4   

2 4 5 0   

2 3 6 0   

1 4 5 0   

2 3 5 1   

  　

  出力例

2 

1 

1 

0 

1 

0 

-->

入力例

2 2 2
4
2 4 5 0
2 3 6 0
1 4 5 0
2 3 5 1
0 0 0

出力例

2
1
1
0
1
0

上記問題文と自動審判に使われるデータは、情報オリンピック日本委員会が作成し公開している問題文と採点用テストデータです。



Sample input:
2 2 2
4
2 4 5 0
2 3 6 0
1 4 5 0
2 3 5 1
0 0 0


Sample output:
2
1
1
0
1
0

Submission status: Wrong Answer

Solution code:
while 1:
    a, b, c = map(int, raw_input().split())
    if not a:
        break
    I = []
    res = [2]*(a+b+c)
    for p in xrange(int(raw_input())):
        i, j, k, r = map(int, raw_input().split())
        i -= 1; j -= 1; k -= 1
        if r:
            res[i] = res[j] = res[k] = 1
        else:
            I.append((i, j, k))
    for e in I:
        i, j, k = e
        if res[i]+res[j]+res[k]!=4:
            continue
        if res[i]==2:
            res[i] = 0
        elif res[j]==2:
            res[j] = 0
        else:
            res[k] = 0
    for i in xrange(a+b+c):
        print res[i]

Submission status: Wrong Answer

Solution code:
while True:
    m = sum(map(int, input().split()))
    if not m:
        break
    n = int(input())
    fixed = [2] * m
    failures = []
    for _ in range(n):
        i, j, k, r = map(int, input().split())
        i, j, k = (x - 1 for x in (i, j, k))
        if r:
            fixed[i] = fixed[j] = fixed[k] = 1
        else:
            failures.append((i, j, k))

    for i, j, k in failures:
        fi, fj, fk = (fixed[x] for x in (i, j, k))
        if fi == 1:
            if fj == 1:
                fixed[k] = 0
            elif fk == 1:
                fixed[j] = 0
        elif fj == 1 and fk == 1:
            fi = 0

    print('\n'.join(str(x) for x in fixed) + '\n')

Submission status: Wrong Answer

Solution code:
while True:
    m = sum(map(int, input().split()))
    if not m:
        break
    n = int(input())
    fixed = [2] * m
    failures = []
    for _ in range(n):
        i, j, k, r = map(int, input().split())
        i, j, k = (x - 1 for x in (i, j, k))
        if r:
            fixed[i] = fixed[j] = fixed[k] = 1
        else:
            failures.append((i, j, k))

    for i, j, k in failures:
        fi, fj, fk = (fixed[x] for x in (i, j, k))
        if fi == 1:
            if fj == 1:
                fixed[k] = 0
            elif fk == 1:
                fixed[j] = 0
        elif fj == 1 and fk == 1:
            fi = 0

    print('\n'.join(str(x) for x in fixed))

Submission status: Wrong Answer

Solution code:
while True:
    a, b, c = map(int, input().split())
    if a == 0:
        break
    N = int(input())
    end = a+b+c+1
    parts = [0]*(end)
    tmp = {k:[] for k in range(end)}
    for _ in range(N):
        i, j, k, r = map(int, input().split())
        for v in [i, j, k]:
            parts[v] += r
            if r == 0:
                tmp[v].append([i, j, k])

    for i in range(1, end):
        if parts[i]:
            print(1)
        else:
            flag = True
            for j in range(len(tmp[i])):
                tmp[i][j].remove(i)
                a, b = tmp[i][j][0], tmp[i][j][1]
                if parts[a] and parts[b]:
                    flag = True
                    break
                else:
                    flag = False
            print(0 if flag else 2)

Submission status: Wrong Answer

Solution code:
while True:
    a, b, c = map(int, input().split())
    if a == 0:
        break
    N = int(input())
    end = a+b+c+1
    parts = [0]*(end)
    tmp = {k:[] for k in range(end)}
    for _ in range(N):
        i, j, k, r = map(int, input().split())
        for v in [i, j, k]:
            parts[v] += r
            if r == 0:
                tmp[v].append([i, j, k])

    for i in range(1, end):
        if parts[i]:
            print("1")
        else:
            flag = True
            for j in range(len(tmp[i])):
                tmp[i][j].remove(i)
                a, b = tmp[i][j][0], tmp[i][j][1]
                if parts[a] and parts[b]:
                    flag = True
                    break
                else:
                    flag = False
            print("0" if flag else "2")

Submission status: Wrong Answer

Solution code:
for e in iter(input,'0 0 0'):
 d=[2]*-~sum(map(int,e.split()))
 f=[]
 for _ in[0]*int(input()):
  s,t,u,v=map(int,input().split())
  if v:d[s]=d[t]=d[u]=1
  else:f+=[(s,t,u)]
 for s,t,u in f:
  if d[t]*d[u]<2:d[s]=0
  if d[u]*d[s]<2:d[t]=0
  if d[s]*d[t]<2:d[u]=0
 for x in d[1:]:print(x)

Submission status: Wrong Answer

Solution code:
for e in iter(input,'0 0 0'):
 d=[2]*-~sum(map(int,e.split()))
 f=[]
 for _ in[0]*int(input()):
  s,t,u,v=map(int,input().split())
  if v:d[s]=d[t]=d[u]=1
  else:f+=[(s,t,u)]
 for s,t,u in f:
  d[s]=2*(d[t]*d[u]!=1)
  d[t]=2*(d[u]*d[s]!=1)
  d[u]=2*(d[s]*d[t]!=1)
 for x in d[1:]:print(x)

Submission status: Wrong Answer

Solution code:
for e in iter(input,'0 0 0'):
 d=[2]*-~sum(map(int,e.split()))
 f=[]
 for _ in[0]*int(input()):
  s,t,u,v=map(int,input().split())
  if v:d[s]=d[t]=d[u]=1
  else:f+=[(s,t,u)]
 for s,t,u in f:
  if d[t]*d[u]!=1:d[s]=0
  if d[u]*d[s]!=1:d[t]=0
  if d[s]*d[t]!=1:d[u]=0
 print(d[1:],sep='\n')

Submission status: Wrong Answer

Solution code:
xs = [2] * sum(map(int, raw_input().split()))
ngs = []
for i in range(int(raw_input())):
    vs = map(int, raw_input().split())
    ijk = vs[:3]
    if vs[3] == 1:
        for v in ijk:
            xs[v - 1] = 1
    else:
        ngs.append(ijk)
for ijk in ngs:
    c = 0
    unk = -1
    for v in ijk:
        x = xs[v - 1]
        if x == 1:
            c += 1
        elif x == 2:
            unk = v
    if c == 2:
        xs[unk - 1] = 0
print '\n'.join(map(str, xs))

Submission status: Wrong Answer

Solution code:
import sys

#fp = open("input.txt", "r")
fp = sys.stdin

gli = []

def operate(li):
    if li[3] == 1:
        gli[li[0]-1] = 1
        gli[li[1]-1] = 1
        gli[li[2]-1] = 1
        li[4] = True
        return True
    elif li[3] == 0:
        if gli[li[0]-1] == 1 and gli[li[1]-1] == 1:
            gli[li[2]-1] = 0
            li[4] = True
            return True
        elif gli[li[0]-1] == 1 and gli[li[2]-1] == 1:
            gli[li[1]-1] = 0
            li[4] = True
            return True
        elif gli[li[1]-1] == 1 and gli[li[2]-1] == 1:
            gli[li[0]-1] = 0
            li[4] = True
            return True
    return False

while True:
    buf = fp.readline()[:-1].split(" ")
    numli = map(int, buf)
    n = numli[0] + numli[1] + numli[2]
    if n == 0:
        break
    for i in xrange(n):
        gli.append(2)

    m = int(fp.readline()[:-1])
    opli = []
    for i in xrange(m):
        op = map(int, fp.readline()[:-1].split(" "))
        op.append(False)
        opli.append(op)

    flg = True
    while flg:
        flg = False
        for i in xrange(m):
            op = opli[i]
            if op[4]:
                continue
            if operate(op):
                flg = True

    for i in xrange(n):
        print gli[i]

Submission status: Accepted

Solution code:
while(1):
    [a,b,c]=[int(x) for x in raw_input().split()]
    if a==0:
        break
    else:
        quality=[2 for x in range(a+b+c+1)]
        N=int(raw_input())
        test=[]
        for i in range(N):
            test.append([int(x) for x in raw_input().split()])
        for t in test:
            if t[3]:
               for x in t[:3]:
                   quality[x]=1
        for t in test:
            qcheck=[quality[x] for x in t[:3]]
            if qcheck==[1,1,2]:
                quality[t[2]]=0
            elif qcheck==[1,2,1]:
                quality[t[1]]=0
            elif qcheck==[2,1,1]:
                quality[t[0]]=0               
        for q in quality[1:]:
            print q

Submission status: Accepted

Solution code:
while 1:
    a, b, c = map(int, raw_input().split())
    if not a:
        break
    n = input()

    I = []; J = []; K = []
    res = [2]*(a+b+c)
    for p in xrange(n):
        i, j, k, r = map(int, raw_input().split())
        i -= 1; j -= 1; k -= 1
        if r:
            res[i] = res[j] = res[k] = 1
        else:
            I.append(i); J.append(j); K.append(k)
    for p in xrange(len(I)):
        i, j, k = I[p], J[p], K[p]
        if res[i]==res[j]==res[k]==1:
            continue
        if res[i]==res[j]==1:
            res[k] = 0
        elif res[j]==res[k]==1:
            res[i] = 0
        elif res[k]==res[i]==1:
            res[j] = 0
    for i in xrange(a+b+c):
        print res[i]

Submission status: Accepted

Solution code:
while 1:
    a, b, c = map(int, raw_input().split())
    if not a:
        break
    I = []
    res = [2]*(a+b+c)
    for p in xrange(int(raw_input())):
        i, j, k, r = map(int, raw_input().split())
        i -= 1; j -= 1; k -= 1
        if r:
            res[i] = res[j] = res[k] = 1
        else:
            I.append((i, j, k))
    for e in I:
        i, j, k = e
        if res[i]+res[j]+res[k]!=4:
            continue
        if res[i]==2:
            res[i] = -1
        elif res[j]==2:
            res[j] = -1
        else:
            res[k] = -1
    for i in xrange(a+b+c):
        print res[i] if res[i]>=0 else 0

Submission status: Accepted

Solution code:
while True:
    i,j,k = map(int, raw_input().split())
    if i==0 and j==0 and k==0: break
    n = i+j+k
    s = []
    a = set(range(n))
    b = set()
    c = set()
    for i in range(input()):
        x, y, z, r = map(int, raw_input().split())
        x-=1;y-=1;z-=1;
        if r==0: s.append(set([x,y,z]))
        else:
            c.add(x);c.add(y);c.add(z);
    while True:
        f = True
        for lst in s:
            if len(lst & c) >=2:
                s.remove(lst)
                lst = lst - c
                if len(lst)>=1: b.add(lst.pop())
                f = False
        if f : break
    a = a - b; a = a - c;
    for i in range(n):
        if i in a: print 2
        elif i in b: print 0
        else: print 1

Submission status: Accepted

Solution code:
while True:
    m = sum(map(int, input().split()))
    if not m:
        break
    n = int(input())
    fixed = [2] * m
    failures = []
    for _ in range(n):
        i, j, k, r = map(int, input().split())
        i, j, k = (x - 1 for x in (i, j, k))
        if r:
            fixed[i] = fixed[j] = fixed[k] = 1
        else:
            failures.append((i, j, k))

    for i, j, k in failures:
        fi, fj, fk = (fixed[x] for x in (i, j, k))
        if fi == 1:
            if fj == 1:
                fixed[k] = 0
            elif fk == 1:
                fixed[j] = 0
        elif fj == 1 and fk == 1:
            fixed[i] = 0

    print('\n'.join(str(x) for x in fixed))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0514

"""
import sys
from sys import stdin
input = stdin.readline


def main(args):
    while True:
        a, b, c = map(int, input().split())
        if a == 0 and b == 0 and c == 0:
            break
        N = int(input())

        ok_items = set()
        ng_items = set()
        pendings = []
        for _ in range(N):
            x, y, z, r = map(int, input().split())
            if r == 1:
                ok_items.add(x)
                ok_items.add(y)
                ok_items.add(z)
            else:
                pendings.append([x, y, z])

        for x, y, z in pendings:
            p = set([x, y, z])
            und = p - ok_items
            if len(und) == 1:
                for i in und:
                    ng_items.add(i)

        for i in range(1, (a+b+c+1)):
            if i in ok_items:
                print(1)
            elif i in ng_items:
                print(0)
            else:
                print(2)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
# coding: utf-8

while True:
	a, b, c = map(int, raw_input().split())
	kosuu = a + b + c
	if kosuu == 0:
		break
	else:
		ans = [5]
		for i in range(1, kosuu+1):
			ans.append(2)

		N = int(raw_input())
		kekka_list = []
		for kensa in range(N):
			kekka = map(int, raw_input().split())
			kekka_list.append(kekka)
		for kekka in kekka_list:
			if kekka[3] == 1:
				ans[kekka[0]] = 1
				ans[kekka[1]] = 1
				ans[kekka[2]] = 1
		for kekka in kekka_list:
			if kekka[3] == 0:
				if ans[kekka[0]] * ans[kekka[1]] * ans[kekka[2]] == 2:
					for l in [kekka[0], kekka[1], kekka[2]]:
						if ans[l] == 2:
							ans[l] = 0
	for i in range(1, len(ans)):
		print(ans[i])

Submission status: Accepted

Solution code:
while True:
  a,b,c = map(int,input().split())
  if a == 0 and b == 0 and c == 0:
    break
  lst = [2 for i in range(a + b + c)]
  ilst = []
  n = int(input())
  for i in range(n):
    i,j,k,r = map(int,input().split())
    if r == 0:
      ilst.append((i,j,k))
    else:
      lst[i - 1] = 1
      lst[j - 1] = 1
      lst[k - 1] = 1
  for (i,j,k) in ilst:
    if lst[i - 1] == 1 and lst[j - 1] == 1:
      lst[k - 1] = 0
    elif lst[i - 1] == 1 and lst[k - 1] == 1:
      lst[j - 1] = 0
    elif lst[j - 1] == 1 and lst[k - 1] == 1:
      lst[i - 1] = 0
  for i in lst:
    print(i)

Submission status: Accepted

Solution code:
for e in iter(input,'0 0 0'):
 d=[2]*-~sum(map(int,e.split()))
 f=[]
 for _ in[0]*int(input()):
  s,t,u,v=map(int,input().split())
  if v:d[s]=d[t]=d[u]=1
  else:f+=[(s,t,u)]
 for s,t,u in f:
  if d[t]*d[u]==1:d[s]=0
  if d[u]*d[s]==1:d[t]=0
  if d[s]*d[t]==1:d[u]=0
 for x in d[1:]:print(x)

Submission status: Accepted

Solution code:
for e in iter(input,'0 0 0'):
 d=[2]*-~sum(map(int,e.split()))
 f=[]
 for _ in[0]*int(input()):
  s,t,u,v=map(int,input().split())
  if v:d[s]=d[t]=d[u]=1
  else:f+=[(s,t,u)]
 for s,t,u in f:
  if d[t]*d[u]==1:d[s]=0
  if d[u]*d[s]==1:d[t]=0
  if d[s]*d[t]==1:d[u]=0
 print(*d[1:],sep='\n')

Submission status: Accepted

Solution code:
# AOJ 0514: Quality Checking
# Python3 2018.6.30 bal4u

while True:
	m = sum(list(map(int, input().split())))
	if m == 0: break
	p = [2]*(m+1)
	tbl = []
	for _ in range(int(input())):
		i, j, k, r = map(int, input().split())
		if r: p[i] = p[j] = p[k] = 1;
		else: tbl.append((i, j, k))
	for t in tbl:
		r = 0
		for j in range(3):
			if p[t[j]] == 1: r += (1<<j)
		if   r == 6: p[t[0]] = 0
		elif r == 5: p[t[1]] = 0
		elif r == 3: p[t[2]] = 0
	del p[0]
	print(*p, sep='\n')

Submission status: Accepted

Solution code:
#!/usr/bin/python
# -*- coding=utf8 -*-

"""
 a, b, c, N は 1 &#8804; a, b, c &#8804; 100, 1 &#8804; N &#8804; 1000 を満たす. 
"""

def readints(line):
    return map(int, line.split())

def solve(constraint, a, b, c):
    status = [2 for i in range(a+b+c+1)]
    for x, y, z, ok in constraint:
        if ok:
            status[x] = 1
            status[y] = 1
            status[z] = 1

    for x, y, z, ok in constraint:
        if not ok:
            if status[x] == 1 and status[y] == 1:
                status[z] = 0
            if status[y] == 1 and status[z] == 1:
                status[x] = 0
            if status[z] == 1 and status[x] == 1:
                status[y] = 0

    return status

def bar(f):
    a, b, c = readints(f.readline())
    while a and b and c:
        N, = readints(f.readline())
        constraint =  [readints(f.readline()) for i in range(N)]
        for r in solve(constraint, a, b, c)[1:]:
            print r
        a, b, c = readints(f.readline())

if True:
    import sys
    bar(sys.stdin)
else:
    sample_in = """2 2 2
4
2 4 5 0
2 3 6 0
1 4 5 0
2 3 5 1
0 0 0
"""
    # expected output
    """
2
1
1
0
1
0
"""
    from StringIO import StringIO
    foo = StringIO(sample_in)
    bar(foo)

Submission status: Accepted

Solution code:
while 1:
    a, b, c = map(int, raw_input().split())
    if a == b == c == 0:
        break
    xs = [2] * (a + b + c)
    ngs = []
    for i in range(int(raw_input())):
        vs = map(int, raw_input().split())
        ijk = vs[:3]
        if vs[3] == 1:
            for v in ijk:
                xs[v - 1] = 1
        else:
            ngs.append(ijk)
    for ijk in ngs:
        c = 0
        unk = -1
        for v in ijk:
            x = xs[v - 1]
            if x == 1:
                c += 1
            elif x == 2:
                unk = v
        if c == 2:
            xs[unk - 1] = 0
    print '\n'.join(map(str, xs))

Submission status: Accepted

Solution code:
while True:
    a,b,c=map(int,raw_input().split())
    if a==b==c==0:break
    L=[2]*(a+b+c+1)
    N=input()
    T=[]
    for t in range(N):
        i,j,k,r=map(int,raw_input().split())
        if r==1:
            L[i]=L[j]=L[k]=1
        else:
            T.append((i,j,k))
    for i,j,k in T:
        if L[i]==L[j]==1:
            L[k]=0
        elif L[i]==L[k]==1:
            L[j]=0
        elif L[j]==L[k]==1:
            L[i]=0
    for i in range(1,a+b+c+1):
        print L[i]

Submission status: Accepted

Solution code:
import sys

#fp = open("input.txt", "r")
fp = sys.stdin

gli = []

def operate(li):
    if li[3] == 1:
        gli[li[0]-1] = 1
        gli[li[1]-1] = 1
        gli[li[2]-1] = 1
        li[4] = True
        return True
    elif li[3] == 0:
        if gli[li[0]-1] == 1 and gli[li[1]-1] == 1:
            gli[li[2]-1] = 0
            li[4] = True
            return True
        elif gli[li[0]-1] == 1 and gli[li[2]-1] == 1:
            gli[li[1]-1] = 0
            li[4] = True
            return True
        elif gli[li[1]-1] == 1 and gli[li[2]-1] == 1:
            gli[li[0]-1] = 0
            li[4] = True
            return True
    return False

while True:
    buf = fp.readline()[:-1].split(" ")
    numli = map(int, buf)
    n = numli[0] + numli[1] + numli[2]
    if n == 0:
        break
    gli = []
    for i in xrange(n):
        gli.append(2)

    m = int(fp.readline()[:-1])
    opli = []
    for i in xrange(m):
        op = map(int, fp.readline()[:-1].split(" "))
        op.append(False)
        opli.append(op)

    flg = True
    while flg:
        flg = False
        for i in xrange(m):
            op = opli[i]
            if op[4]:
                continue
            if operate(op):
                flg = True

    for i in xrange(n):
        print gli[i]

Submission status: Accepted

Solution code:
while 1:
	a,b,c=map(int,raw_input().split())
	if a==0:break
	t=[2]*(a+b+c)
	m=input()
	tests=sorted([map(int,raw_input().split()) for i in range(m)],key=lambda x:x[3])[::-1]
	for test in tests:
		na,nb,nc=test[0]-1,test[1]-1,test[2]-1
		if test[3]==1:
			t[na],t[nb],t[nc]=1,1,1
		else:
			ls=[t[na],t[nb],t[nc]]
			if ls.count(1)==2 and 2 in ls:
				if t[na]==2:t[na]=0
				if t[nb]==2:t[nb]=0
				if t[nc]==2:t[nc]=0
	for i in t:
		print i

Submission status: Accepted

Solution code:
while 1:
    a,b,c = map(int, input().split())
    if a == 0:
        break
    n = int(input())
    d = {}
    ans = [2]*(a+b+c)

    for _ in range(n):
        i, j, k, r = map(int, input().split())
        d[(i, j, k)] = r
        if r == 1:
            ans[i-1] = ans[j-1] = ans[k-1] = 1
    for i in d:
        if d[i] == 0:
            if ans[i[0]-1] == 1 and ans[i[1]-1] == 1:
                ans[i[2]-1] = 0
            elif ans[i[1]-1] == 1 and ans[i[2]-1] == 1:
                ans[i[0]-1] = 0
            elif ans[i[2]-1] == 1 and ans[i[0]-1] == 1:
                ans[i[1]-1] = 0
    for i in range(a+b+c):
        print(ans[i])

Submission status: Accepted

Solution code:
import heapq
from collections import deque
from enum import Enum
import sys
import math
from _heapq import heappush, heappop
import copy
from test.support import _MemoryWatchdog
 
BIG_NUM = 2000000000
HUGE_NUM = 99999999999999999
MOD = 1000000007
EPS = 0.000000001
sys.setrecursionlimit(100000)
 
 
class Type(Enum):
    UNKOWN = 0
    OK = 1
    NOT = 2
 
class Info:
    def __init__(self,arg_a,arg_b,arg_c):
        self.work = [arg_a,arg_b,arg_c]
 
 
while True:
    A,B,C = map(int,input().split())
    if A == 0 and B == 0 and C == 0:
        break
 
    table = [Type.UNKOWN]*((A+B+C)+1)
 
    N = int(input())
    info = []
 
    for _ in range(N):
        a,b,c,result = map(int,input().split())
        if result == 1:
            table[a] = Type.OK
            table[b] = Type.OK
            table[c] = Type.OK
 
        else:
            info.append(Info(a,b,c))
 
    for i in range(len(info)):
        count = 0
        tmp = -1
        for k in range(3):
            if table[info[i].work[k]] == Type.OK:
                count += 1
            elif table[info[i].work[k]] == Type.UNKOWN:
                tmp = info[i].work[k]
        if count != 2 or tmp == -1:
            continue
        table[tmp] = Type.NOT
 
    for i in range(1,(A+B+C)+1):
        if table[i] == Type.OK:
            print("1")
        elif table[i] == Type.NOT:
            print("0")
        else:
            print("2")

Submission status: Accepted

Solution code:
while True:
    supply,motor,cable=(int(x) for x in input().split())
    if supply==0 and motor==0:
        break
    checknum=int(input())
    
    reslist=[2]*(supply+motor+cable)
    stack=[]
    for i in range(checknum):
        no1,no2,no3,result=(int(x)-1 for x in input().split())
        result+=1
        if result==1:
            reslist[no1], reslist[no2], reslist[no3]=1,1,1
        else:
            stack.append((no1,no2,no3))

    for i in range(len(stack)):
        t=stack[i]
        no1,no2,no3=t[0],t[1],t[2]
        if reslist[no1]==1 and reslist[no2]==1:
            reslist[no3]=0
        elif reslist[no3]==1 and reslist[no2]==1:
            reslist[no1]=0
        elif reslist[no3]==1 and reslist[no1]==1:
            reslist[no2]=0
            
    for i in range(len(reslist)):
        print(reslist[i])

Submission status: Accepted

Solution code:
import sys

class QualityChecking:
    def __init__(self, a, b, c, N):
        self.__N = N
        self.__a = a
        self.__b = b
        self.__c = c
        self.__test = []
    
    def read(self, i, j, k, r):
        self.__test.append([i, j, k, r])
    
    def solve(self):
        result = [2] * (self.__a + self.__b + self.__c + 1)
        ok = set()
        ng = set()
        unknown = set()
        for t in self.__test:
            if t[3] == 1:
                ok.add(t[0])
                ok.add(t[1])
                ok.add(t[2])
        test1 = []
        for t in self.__test:
            test1.append(["ok" if x in ok else x for x in t])
        for t in test1:
            if t[3] == 0:
                if t[0] == "ok" and t[1] == "ok":
                    ng.add(t[2])
                elif t[0] == "ok" and t[2] == "ok":
                    ng.add(t[1])
                elif t[1] == "ok" and t[2] == "ok":
                    ng.add(t[0])
        for i in range(1, self.__a + self.__b + self.__c + 1):
            if i in ok:
                print("1")
            elif i in ng:
                print("0")
            else:
                print("2")
               

while True:
    line = input()
    a, b, c = map(int, list(line.split()))
    if a == 0 and b == 0 and c == 0:
        break
    line = input()
    n = int(line)
    qc = QualityChecking(a, b, c, n)
    for _ in range(0, n):
        i, j, k, r = map(int, list(input().split()))
        qc.read(i, j, k, r)
    qc.solve()
    del qc

Submission status: Accepted

Solution code:
while 1:
    try:
        a,b,c=map(int,input().split())
        parts=[2]*(a+b+c+1)
        N=int(input())
        met_keep=[]
        for nn in range(N):
            i,j,k,r=map(int,input().split())
            if r==1:
                parts[i],parts[j],parts[k]=1,1,1
            if r==0:
                met_keep.append([i,j,k])
        for i,j,k in met_keep:
            if parts[i]==1 and parts[j]==1 and parts[k]==2:
                parts[k]=0
            elif parts[i]==1 and parts[j]==2 and parts[k]==1:
                parts[j]=0
            elif parts[i]==2 and parts[j]==1 and parts[k]==1:
                parts[i]=0
        for i in parts[1:]:
            print(i)
    except:break

Submission status: Accepted

Solution code:
while 1:
    a, b, c = map(int, input().split())
    if a == 0:
        break

    data = []
    n = int(input())
    for _ in range(n):
        data.append(list(map(int, input().split())))

    data = sorted(data, key=lambda x: -x[3])
    parts = [2] * (a+b+c)

    for d in data:
        a = d[0]
        b = d[1]
        c = d[2]
        if d[3] == 1:
            parts[a-1] = 1
            parts[b-1] = 1
            parts[c-1] = 1
        else:
            if parts[a-1] == 1 and parts[b-1] == 1:
                parts[c-1] = 0
            elif parts[c-1] == 1 and parts[b-1] == 1:
                parts[a-1] = 0
            elif parts[a-1] == 1 and parts[c-1] == 1:
                parts[b-1] = 0

    for part in parts:
        print(part)

Submission status: Accepted

Solution code:
for _ in range(5):
    a, b, c = [int(s) for s in input().strip().split(' ')]
    if  a | b | c == 0:
        break
    n = int(input().strip())
    rlist = [[int(s) for s in input().strip().split(' ')] for _ in range(n)]
    clist = [2,]*sum((a,b,c))
    for success in filter(lambda r: r[3] == 1, rlist):
        for i in success[:3]:
            clist[i-1] = 1

            for fail in filter(lambda r: r[3] == 0, rlist):
                plist = [clist[i-1] for i in fail[:3]]
                if sorted(plist) == [1,1,2]:
                    for i in fail[:3]:
                        if clist[i-1] == 2:
                            clist[i-1] = 0
                            break

    [print(r) for r in clist]

Submission status: Runtime Error

Solution code:
while True:
    l = map(int, raw_input().split())
    if l[0]==0 and l[1]==0 and l[2]==0: break
    n = l[0]+l[1]+l[2]
    s = [] #????¨????
    a = set(range(n)) #??????
    b = set() #?£????
    c = set() #??£???
    for i in range(input()):
        x, y, z, r = map(int, raw_input().split())
        x-=1;y-=1;z-=1;
        if r==0: s.append(set([x,y,z]))
        else:
            c.add(x);c.add(y);c.add(z);
    while True:
        f = True
        for lst in s:
            if len(lst & c) >=2:
                s.remove(lst)
                lst = lst - c
                b.add(lst.pop())
                f = False
        if f : break
    a = a - b; a = a - c;
    for i in range(n):
        if i in a: print 2
        elif i in b: print 0
        else: print 1

Submission status: Runtime Error

Solution code:
while True:
    l = map(int, raw_input().split())
    if l[0]==0 and l[1]==0 and l[2]==0: break
    n = l[0]+l[1]+l[2]
    s = [] #????¨????
    a = set(range(n)) #??????
    b = set() #?£????
    c = set() #??£???
    for i in range(input()):
        x, y, z, r = map(int, raw_input().split())
        x-=1;y-=1;z-=1;
        if r==0: s.append(set([x,y,z]))
        else:
            c.add(x);c.add(y);c.add(z);
    while True:
        f = True
        for lst in s:
            if len(lst & c) >=2:
                s.remove(lst)
                lst = lst - c
                if len(lst)>=1: b.add(lst.pop())
                f = False
        if f : break
    a = a - b; a = a - c;
    for i in range(n):
        if i in a: print 2
        elif i in b: print 0
        else: print 1

Submission status: Runtime Error

Solution code:
while True:
    m = sum(map(int, input().split()))
    n = int(input())
    fixed = [2] * m
    failures = []
    for _ in range(n):
        i, j, k, r = map(int, input().split())
        i, j, k = (x - 1 for x in (i, j, k))
        if r:
            fixed[i] = fixed[j] = fixed[k] = 1
        else:
            failures.append((i, j, k))

    for i, j, k in failures:
        fi, fj, fk = (fixed[x] for x in (i, j, k))
        if fi == 1:
            if fj == 1:
                fixed[k] = 0
            elif fk == 1:
                fixed[j] = 0
        elif fj == 1 and fk == 1:
            fi = 0

    print('\n'.join(str(x) for x in fixed) + '\n')