Problem:


パトロール

文久２（1862）年、会津の殿様が京都守護職を命ぜられました。京都守護職とは治安の悪化した幕末の京都を守る大切な役目です。幕府や他の藩と分担して市中をパトロールしなければなりません。ところがいざ分担ルートを決める段になって、家臣の中でも有名な頑固者の古老から以下のような注文がつきました。

大変なことになりました。殿様といえどもこの家臣の言い分を無視するわけにはいきません。分担ルートの選択によっては、「武士の面目が立たない」ということになってしまいます。

ということで、スタート地点、ゴール地点、交差点の情報を入力として、上の三つの条件を満たすかどうかを判定するプログラムを作って、殿様に献上してください。

スタート地点を 1、ゴール地点を2、その他の交差点を 3 以上の整数で表します。1つの道路は、その道が結ぶ1組の交差点番号で表します。なお、交差点の数は 100 以下とし、全ての交差点からスタート地点およびゴール地点への経路がそれぞれ一つ以上あるものとします。

入力

複数のデータセットが与えられます。各データセットは以下の形式で与えられます。

a1 b1
a2 b2
：
：
0 0

各行の２つの整数は、交差点 ai と交差点 bi とをつなぐ道路が存在することを示します。
ai と bi がともに 0 のとき交差点情報の入力の終わりを示します。

データセットの数は50を超えません。

出力

各データセットに対して、武士の面目が立つ場合（三つの条件を満たす場合）OK、それ以外の場合（三つの条件を満たさない場合）NG と１行に出力してください。

Sample Input

1 3
3 4
3 5
3 6
4 6
4 7
4 7
5 6
6 7
5 8
5 8
6 8
6 9
7 9
8 9
9 2
0 0
1 3
3 4
3 4
4 2
0 0

Output for the Sample Input

OK
NG



Sample input:
1 3
3 4
3 5
3 6
4 6
4 7
4 7
5 6
6 7
5 8
5 8
6 8
6 9
7 9
8 9
9 2
0 0
1 3
3 4
3 4
4 2
0 0


Sample output:
OK
NG

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import (division, absolute_import, print_function,
                        unicode_literals)
from sys import stdin
from collections import Counter

cnt = Counter()
for line in stdin:
    start, end = (int(s) for s in line.split())
    if start or end:
        cnt[start] += 1
        cnt[end] += 1
    else:
        answer = 'OK'
        num_odd = 0
        for i in cnt.values():
            num_odd += i % 2
            if 2 < num_odd:
                answer = 'NG'
                break
        print(answer)
        cnt.clear()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import (division, absolute_import, print_function,
                        unicode_literals)
from sys import stdin
from collections import Counter

cnt = Counter()
for line in stdin:
    start, end = (int(s) for s in line.split())
    if start or end:
        cnt[start] += 1
        cnt[end] += 1
    else:
        eulerian_graph = sum(i % 2 for i in cnt.values()) in (0, 2)
        print('OK' if eulerian_graph else 'NG')
        cnt.clear()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import (division, absolute_import, print_function,
                        unicode_literals)
from sys import stdin
from collections import Counter

cnt = Counter()
for line in stdin:
    start, end = (int(s) for s in line.split())
    if start or end:
        cnt[start] += 1
        cnt[end] += 1
    else:
        eulerian_graph = sum(i % 2 for i in cnt.values()) == 2
        print('OK' if eulerian_graph else 'NG')
        cnt.clear()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import (division, absolute_import, print_function,
                        unicode_literals)
from sys import stdin
from collections import Counter

cnt = Counter()
for line in stdin:
    start, end = (int(s) for s in line.split())
    if start or end:
        cnt[start] += 1
        cnt[end] += 1
    else:
        semi_eulerian = sum(i % 2 for i in cnt.values()) == 2
        if semi_eulerian and cnt[1] % 2 and cnt[2] % 2:
            print('OK')
        else:
            print('NO')
        cnt.clear()

Submission status: Wrong Answer

Solution code:
path = [0 for i in range(10)]
while True:
    a,b = map(int, raw_input().split())
    if a == 0: break
    path[a-1] += 1; path[b-1] += 1
for i in range(3,10):
    if path[i]%2 != 0:
        print "NG"
        break
else:
    print "OK"

Submission status: Wrong Answer

Solution code:
while True:
	try:
		path = [0]*10
		while True:
			a,b = map(int, raw_input().split())
			if a == 0: break
			path[a-1] += 1; path[b-1] += 1
		for i in range(3,10):
	   		if path[i]%2 != 0:
				print "NG"
	    		break
	   	else:
	   		print "OK"
   	except:
   		break

Submission status: Wrong Answer

Solution code:
while True:
	try:
		path = [0]*10
		while True:
			a,b = map(int, raw_input().split())
			if a == 0: break
			path[a-1] += 1; path[b-1] += 1
		for i in range(3,10):
			if path[i]%2 != 0:
				print "NG"
				break
		else:
			print "OK"
   	except:
   		break

Submission status: Wrong Answer

Solution code:
while True:
	try:
		path = [0]*10
		while True:
			a,b = map(int, raw_input().split())
			if a == 0: break
			path[a-1] += 1; path[b-1] += 1
		if path[0]>2 or path[1]>2:
			print "NG"
		else:
			for i in range(3,10):
				if path[i]%2 != 0:
					print "NG"
					break
			else:
				print "OK"
   	except:
   		break

Submission status: Wrong Answer

Solution code:
while True:
	try:
		path = [0]*10
		while True:
			a,b = map(int, raw_input().split())
			if a == 0: break
			path[a-1] += 1; path[b-1] += 1
		for i in range(3,10):
			if path[i]%2 != 0:
				flag = 0
				break
		else:
			flag = 1
		if path[0]%2 == path[1]%2 == 1 and  flag:
			print "OK"
		else:
			print "NG"
   	except:
   		break

Submission status: Wrong Answer

Solution code:
while True:
	try:
		path = [0]*101
		while True:
			a,b = map(int, raw_input().split())
			if a == 0: break
			path[a-1] += 1; path[b-1] += 1
		for i in range(3,10):
			if path[i]%2 != 0:
				flag = 0
				break
		else:
			flag = 1
		if path[0]%2 == path[1]%2 == 1 and  flag:
			print "OK"
		else:
			print "NG"
   	except:
   		break

Submission status: Wrong Answer

Solution code:
def f():
  c=[0]*101
  while 1:
    a,b=map(int,raw_input().split())
    if a==b==0:break
    c[a]+=1
    c[b]+=1
  if c[1]==c[2]==1:
    f=0
    for e in c[3:]:
      if e%2!=0:break
    else:f=1
    print ["NG","OK"][f]  
  return
while 1:
  try:f()
  except:break

Submission status: Wrong Answer

Solution code:
def f():
  c=[0]*101
  while 1:
    a,b=map(int,raw_input().split())
    if a==b==0:break
    c[a]+=1
    c[b]+=1
  f=0
  if c[1]==c[2]==1:
    for e in c[3:]:
      if e%2!=0:break
    else:f=1
  print ["NG","OK"][f]
  return
while 1:
  try:f()
  except:break

Submission status: Wrong Answer

Solution code:
while 1:
    v = set()
    edges = 0
    try:
        a, b = map(int, raw_input().split())
        v.add(a)
        v.add(b)
        edges += 1
    except EOFError:
        break
    while 1:
        a, b = map(int, raw_input().split())
        if a == b == 0:
            break
        v.add(a)
        v.add(b)
        edges += 1
    ret = edges - (len(v) - 1)
    if ret % 2:
        print "NG"
    else:
        print "OK"

Submission status: Wrong Answer

Solution code:
#coding:utf-8

#初期化
key=[]
for i in range(101):
    key.append(i)
flag = 0
#オイラー路：すべての頂点のうち、次数が奇数であるものがちょうど２つ(入口と出口)
while 1:
    input = map(int, raw_input().split())
    if(input[0] == 0 and input[1] == 0):
        break
    key[input[0]]+=1
    key[input[1]]+=1

for v in key:
    if v%2 == 1:
        flag+=1
if(flag == 2):
    print "OK"
else:
    print "NG"

Submission status: Wrong Answer

Solution code:
#coding:utf-8
while 1:
    try:
        #初期化
        key=[]
        for i in range(101):
            key.append(i)
        flag = 0
        #オイラー路：すべての頂点のうち、次数が奇数であるものがちょうど２つ(入口と出口)
        while 1:
            input = map(int, raw_input().split())
            if(input[0] == 0 and input[1] == 0):
                break
            key[input[0]]+=1
            key[input[1]]+=1
                    
        for v in key:
            if v%2 == 1:
                flag+=1
        if(flag == 2):
            print "OK"
        else:
            print "NG"
    except:
        break

Submission status: Wrong Answer

Solution code:
#coding:utf-8
while 1:
    try:
        #初期化
        key=[]
        for i in range(101):
            key.append(0)
        flag = 0
        #オイラー路：すべての頂点のうち、次数が奇数であるものがちょうど２つ(入口と出口)
        while 1:
            input = map(int, raw_input().split())
            if(input[0] == 0 and input[1] == 0):
                break
            key[input[0]]+=1
            key[input[1]]+=1
                    
        for v in key:
            if v%2 == 1:
                flag+=1
        if(flag == 2):
            print "OK"
        else:
            print "NG"
    except:
        break

Submission status: Wrong Answer

Solution code:
#coding:utf-8
while 1:
    try:
        #初期化
        key=[]
        for i in range(101):
            key.append(0)
        flag = 0
        #オイラー路：すべての頂点のうち、次数が奇数であるものがちょうど２つ(入口と出口)
        while 1:
            input = map(int, raw_input().split())
            if(input[0] == 0 and input[1] == 0):
                break
            key[input[0]]+=1
            key[input[1]]+=1

        for v in key:
            if v%2 == 1:
                flag+=1
        if(flag == 2 or flag == 0):
            print "OK"
        else:
            print "NG"
    except:
        break

Submission status: Wrong Answer

Solution code:
#coding:utf-8
while 1:
    try:
        key=[]
        for i in range(101):
            key.append(0)
        flag = 0
      
        while 1:
            input = map(int, raw_input().split())
            if(input[0] == 0 and input[1] == 0):
                break
            key[input[0]]+=1
            key[input[1]]+=1

        for v in key:
            if v%2 == 1:
                flag+=1
        if(flag == 2 or flag == 0):
            print "OK"
        else:
            print "NG"
    except:
        break

Submission status: Wrong Answer

Solution code:
while 1:
    try:
        
        key=[]
        for i in range(101):
            key.append(0)
        flag = 0
       
        while 1:
            input = map(int, raw_input().split())
            if(input[0] == 0 and input[1] == 0):
                break
            key[input[0]]+=1
            key[input[1]]+=1

        for v in key:
            if v%2 == 1:
                flag+=1
        if(flag == 2 or flag == 0):
            print "OK"
        else:
            print "NG"
    except:
        break

Submission status: Wrong Answer

Solution code:
while True:
    try:
        path = [0]*100
        while True:
            a,b = map(int, raw_input().split())
            if a == 0: break
            path[a-1] += 1; path[b-1] += 1
        if (path[0]%2 == path[1]%2 == 1):
            print "OK"
        else:
            print "NG"
    except:
        break

Submission status: Wrong Answer

Solution code:
#coding:utf-8
while 1:
    try:
        #初期化
        key=[]
        for i in range(101):
            key.append(0)
        flag = 0;
        #オイラー路：すべての頂点のうち、次数が奇数であるものがちょうど２つ(入口と出口)
        while 1:
            input = map(int, raw_input().split())
            if(input[0] == 0 and input[1] == 0):
                break
            key[input[0]]+=1
            key[input[1]]+=1

        for v in key[3:]:
            if v%2 == 1:
                flag+=1
        if(flag == 0):
            print "OK"
        else:
            print "NG"
    except:
        break

Submission status: Wrong Answer

Solution code:
#coding:utf-8
while 1:
    try:
        #初期化
        key=[0]*101
        flag = 0;
        #オイラー路：すべての頂点のうち、次数が奇数であるものがちょうど２つ(入口と出口)
        while 1:
            input = map(int, raw_input().split())
            if(input[0] == 0 and input[1] == 0):
                break
            key[input[0]]+=1
            key[input[1]]+=1

        for v in key:
            if v%2 == 1:
                flag+=1
        if(flag == 2):
            print "OK"
        else:
            print "NG"
    except:
        break

Submission status: Wrong Answer

Solution code:
#coding:utf-8
while 1:
    try:
        #初期化
        key=[0]*101
        flag = 0
        #オイラー路：すべての頂点のうち、次数が奇数であるものがちょうど２つ(入口と出口)
        while 1:
            input = map(int, raw_input().split())
            if(input[0] == 0 and input[1] == 0):
                break
            key[input[0]]+=1
            key[input[1]]+=1
        if(key[1]%2==1):
            flag += 1
        if(key[2]%2==1):
            flag += 1
        for v in key[3:]:
            if(v%2==1):
                flag += 1
            
        if(flag == 2):
            print "OK"
        else:
            print "NG"
    except:
        break

Submission status: Wrong Answer

Solution code:
#coding:utf-8
while 1:
    try:
        #初期化
        key=[0]*101
        flag = 0
        #オイラー路：すべての頂点のうち、次数が奇数であるものがちょうど２つ(入口と出口)
        while 1:
            input = map(int, raw_input().split())
            if(input[0] == 0 and input[1] == 0):
                break
            key[input[0]]+=1
            key[input[1]]+=1
        if(key[1]%2==1 and key[2]%2==1):
            for v in key[3:]:
                if(v%2==0):
                    flag = 1
                else:
                    flag = 0
                    break
            if(flag == 1):
                print "OK"
        else:
            print "NG"

    except:
        break

Submission status: Time Limit Exceeded

Solution code:
import sys

def random_walk(s, lis):
    for i, v in enumerate(lis):
        if s in v:
            v_c = v[:]
            v_c.remove(s)
            if v_c[0] == 2:
                if len(lis) == 1:
                    print 'OK'
                    return True
                else:
                    break
            else:
                if random_walk(v_c[0], lis[:i]+lis[i+1:]):
                    return True
        
lis = []
for line in sys.stdin:
    t = map(int, line.rstrip().split())
    if t[0] != 0 and t[1] != 0:
        lis.append(t)
    else:
        if not random_walk(1, lis):
            print 'NO'
        lis = []

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict


def dfs(v):
    stack.append(v)
    global ret
    if ret:
        return
    if v == 2 and len(used) == cnt:
        ret = True
        return
    for idx, u in enumerate(edges[v]):
        tmp = tuple(sorted((u, v)) + [idx])
        if tmp in used:
            continue
        used.add(tmp)
        dfs(u)
        used.discard(tmp)
    stack.pop()

while 1:
    edges = defaultdict(list)
    cnt = 0
    try:
        a, b = map(int, raw_input().split())
        edges[a].append(b)
        edges[b].append(a)
        cnt += 1
    except EOFError:
        break
    while 1:
        a, b = map(int, raw_input().split())
        if a == b == 0:
            break
        edges[a].append(b)
        edges[b].append(a)
        cnt += 1
    stack = []
    ret = False
    used = set()
    dfs(1)
    if ret:
        print "OK"
    else:
        print "NG"

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict


def dfs(v):
    global ret
    if ret:
        return
    if v == 2:
        if len(used) == cnt:
            ret = True
        return
    for idx, u in enumerate(edges[v]):
        tmp = tuple(sorted((u, v)) + [idx])
        if tmp in used:
            continue
        used.add(tmp)
        dfs(u)
        used.discard(tmp)

while 1:
    edges = defaultdict(list)
    cnt = 0
    try:
        a, b = map(int, raw_input().split())
        edges[a].append(b)
        edges[b].append(a)
        cnt += 1
    except EOFError:
        break
    while 1:
        a, b = map(int, raw_input().split())
        if a == b == 0:
            break
        edges[a].append(b)
        edges[b].append(a)
        cnt += 1
    stack = []
    ret = False
    used = set()
    dfs(1)
    if ret:
        print "OK"
    else:
        print "NG"

Submission status: Accepted

Solution code:
import sys

def solve(lis):
    for e in lis[1:3]:
        if not e%2:
            return "NG"
    for e in lis[3:10]:
        if e%2:
            return "NG"
    return "OK"

li = [0]*102
for s in sys.stdin:
    a,b = map(int,s.split())
    if a == b == 0:
        print solve(li)
        li = [0]*102
        continue
    li[a] += 1
    li[b] += 1

Submission status: Accepted

Solution code:
import sys
f = sys.stdin

def is_odd(num):
    return num % 2 == 1

intersection = [0 for _ in range(101)]
for line in f:
    a, b = map(int, line.split())
    if a == b == 0:
        if is_odd(intersection[1]) and is_odd(intersection[2]) and 0 == sum(1 for i in range(3, len(intersection)) if is_odd(intersection[i])):
            print('OK')
        else:
            print('NG')
        intersection = [0 for _ in range(101)]
    else:
        intersection[a] += 1
        intersection[b] += 1

Submission status: Accepted

Solution code:
while 1:
    try:
        p=[0]*100
        while 1:
            a,b=map(int,raw_input().split())
            if a==0:
                break
            p[a-1]+=1
            p[b-1]+=1
        if p[0]%2==p[1]%2==1 and sum(filter(lambda x:x%2==1,p[2:]))==0:
            print 'OK'
        else:
            print 'NG'
    except:
        break

Submission status: Accepted

Solution code:
import sys

ipt = []
for line in sys.stdin:
  ipt.append(line)
dic = {}
dic[1] = True
dic[2] = True

for i in xrange(len(ipt)):
  s = ipt[i]
  a,b = map(int, s.split())
  if a==b==0:
    if len(dic) == 0:
      print "OK"
    else:
      print "NG"
    dic = {}
    dic[1] = True
    dic[2] = True
    continue
  if a in dic:
    del dic[a]
  else:
    dic[a] = True
  if b in dic:
    del dic[b]
  else:
    dic[b] = True

Submission status: Accepted

Solution code:
c=[0]*100
while 1:
    try:a,b=map(int,input().split())
    except:break
    c[a-1]+=1;c[b-1]+=1
    if a==0:
        for i in range(2,100):
            if c[i]&1:print('NG');break
        else:print('OK' if c[0]&1 and c[1]&1 else 'NG')
        c=[0]*100

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys
import os
import math

# adjacent list
M = []
for i in range(105):
    M.append([])

def solve(M):
    odd_num = 0
    even_num = 0

    for m in M:
        if len(m) % 2 == 0:
            even_num += 1
        else:
            odd_num += 1

    # semi-Eulerlian graph
    if odd_num == 2 and len(M[0]) % 2 == 1 and len(M[1]) % 2 == 1:
        print('OK')
    else:
        print('NG')

for s in sys.stdin:
    a, b = map(int, s.split())
    if a == b == 0:
        solve(M)

        # reset
        M = []
        for i in range(105):
            M.append([])
    else:
        a -= 1
        b -= 1
        M[a].append(b)
        M[b].append(a)

Submission status: Accepted

Solution code:
# Aizu Problem 0086: Patrol
#
import sys, math, os

# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input2.txt", "rt")


a = [0] * 101
while True:
    try:
        x, y = [int(_) for _ in input().split()]
    except EOFError:
            break
    if x == y == 0:
        c = sum([a[i] % 2 for i in range(3, 101)])
        print("OK" if c == 0 and a[1] % 2 == 1 and a[2] % 2 == 1 else "NG")
        a = [0] * 101
    else:
        a[x] += 1
        a[y] += 1

Submission status: Accepted

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

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


def main(args):
    for line in sys.stdin:
        routes = []
        a, b = map(int, line.split())
        if a == 0 and b == 0:
            break
        while a != 0 or b!= 0:
            routes.append([a, b])
            a, b = map(int, input().split())

        degrees = [0] * 101
        for a, b in routes:
            degrees[a] += 1
            degrees[b] += 1
        odds = [x for x in degrees if x%2==1]
        # evens = [x for x in degrees if x%2==0]
        # print(degrees)

        if len(odds) == 2 and degrees[1]%2==1 and degrees[2]%2==1:
            print('OK')
        else:
            print('NG')


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

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict
data_list = []

for line in sys.stdin:
    data_list.append(list(map(int, line.split())))

def is_half_euler_graph(node_list):
    for node in node_list:
        isBool = True
        for dic_key, dic_value in node.items():
            if dic_key != 1 and dic_key != 2:
                if dic_value % 2 != 0:
                    isBool = False
                    break
            else :
                if dic_value % 2 == 0:
                    isBool = False
                    break
        if isBool :
            print("OK")
        else :
            print("NG")



if __name__ == '__main__' :
    node_data_lists = []
    tmp_list = []
    count_lists = []
    tmp_dic = {}
    tmp_dic = defaultdict(int)

    for i in range(0, len(data_list)):
        if data_list[i][0] == 0 and data_list[i][1] == 0:
            node_data_lists.append(tmp_list[:])
            tmp_list.clear()
        else :
            tmp_list.append(data_list[i])

    for node_data_list in node_data_lists :
        tmp_dic.clear()
        for i in range(0, len(node_data_list)):
            tmp_dic[node_data_list[i][0]] += 1
            tmp_dic[node_data_list[i][1]] += 1

        count_lists.append(tmp_dic.copy())

    is_half_euler_graph(count_lists)

Submission status: Accepted

Solution code:
import sys

def is_half_euler_graph(node_list):
    for node in node_list:
        isBool = True
        for dic_key, dic_value in node.items():
            if dic_key != 1 and dic_key != 2:
                if dic_value % 2 != 0:
                    isBool = False
                    break
            else :
                if dic_value % 2 == 0:
                    isBool = False
                    break
        if isBool :
            print("OK")
        else :
            print("NG")



if __name__ == '__main__' :
    from collections import defaultdict
    data_list = []

    for line in sys.stdin:
        data_list.append(list(map(int, line.split())))


    node_data_lists = []
    tmp_list = []
    count_lists = []
    tmp_dic = {}
    tmp_dic = defaultdict(int)

    for i in range(0, len(data_list)):
        if data_list[i][0] == 0 and data_list[i][1] == 0:
            node_data_lists.append(tmp_list[:])
            tmp_list.clear()
        else :
            tmp_list.append(data_list[i])

    for node_data_list in node_data_lists :
        tmp_dic.clear()
        for i in range(0, len(node_data_list)):
            tmp_dic[node_data_list[i][0]] += 1
            tmp_dic[node_data_list[i][1]] += 1

        count_lists.append(tmp_dic.copy())

    is_half_euler_graph(count_lists)

Submission status: Accepted

Solution code:
import fileinput

if __name__ == '__main__':
    # 標準入力からグラフ読み込み
    G = []
    g = []
    for line in fileinput.input():
        tokens = list(map(int, line.strip().split()))
        a, b = tokens[0], tokens[1]
        if a == 0 and b == 0:
            G.append(g)
            g = []
        else:
            g.append((tokens[0], tokens[1]))

    # グラフオブジェクトから経路判定
    for g in G:
        result = True
        counts = [0 for i in range(100)]        
        for a, b in g:
            counts[a-1] += 1
            counts[b-1] += 1
        for c in counts[0:2]:
            if c % 2 == 0:
                result = False
        for c in counts[2:100]:
            if c % 2 == 1:
                result = False
        print("OK" if result else "NG")

Submission status: Accepted

Solution code:
while True:
    try:
        d = {}
        while True:
            a, b = map(int, input().split())
            if a == 0:
                break
            if a in d:
                d[a] += 1
            else:
                d[a] = 1
            if b in d:
                d[b] += 1
            else:
                d[b] = 1
        if d[1]%2 == 1 and d[2]%2 == 1:
            del d[1], d[2]
            for node in d:
                if d[node]%2 == 1:
                    print("NG")
                    break
            else:
                print("OK")
        else:
            print("NG")
    except:
        break

Submission status: Accepted

Solution code:
import sys
ab = []
for line in sys.stdin:
    temp = list(map(int, line.split()))
    if temp == [0, 0]:
        v = [0 for i in range(max(ab)+1)]
        for i in ab:
            v[i] += 1
        odd = 0
        for i in v:
            if i % 2 == 1:
                odd += 1
        if odd == 2 and ((v[1] % 2) == (v[2] % 2) == 1):
            print('OK')
        else:
            print('NG')
        ab = []
    else:
        ab += temp

Submission status: Accepted

Solution code:
import sys
v=[0]*101
for e in sys.stdin:
 a,b=map(int,e.split())
 if a==0:
  print(['NG','OK'][1&v[1]and 1&v[2]and sum(x&1 for x in v[3:])==0])
  v=[0]*101
 v[a]+=1;v[b]+=1

Submission status: Accepted

Solution code:
import sys
v=[0]*101
for e in sys.stdin:
 a,b=map(int,e.split())
 v[a]+=1;v[b]+=1
 if a==0:
  print(['NG','OK'][1&v[1]and 1&v[2]and sum(x&1 for x in v)==2])
  v=[0]*101

Submission status: Accepted

Solution code:
import sys
v=[0]*101
for e in sys.stdin:
 a,b=map(int,e.split())
 v[a]+=1;v[b]+=1
 if a==0:
  print(['NG','OK'][(v[1]&1)*(v[2]&1)*sum(x&1 for x in v)==2])
  v=[0]*101

Submission status: Accepted

Solution code:
import sys
v=[0]*101
for e in sys.stdin:
 a,b=map(int,e.split())
 v[a]+=1;v[b]+=1
 if a==0:
  print(['NG','OK'][(v[1]&1)*(v[2]&1)*sum(x&1 for x in v)==2])
  v=[0]*100

Submission status: Accepted

Solution code:
import sys
v=[0]*100
for e in sys.stdin:
 a,b=map(int,e.split())
 v[a]+=1;v[b]+=1
 if a==0:
  print(['NG','OK'][(v[1]&1)*(v[2]&1)*sum(x&1 for x in v)==2])
  v=[0]*100

Submission status: Accepted

Solution code:
def solve():
  dic = {}
  while True:
    a, b = map(int, input().split())
    if not a:
      break
  
    if a in dic:
      dic[a] += 1
    else:
      dic[a] = 1
  
    if b in dic:
      dic[b] += 1
    else:
      dic[b] = 1
  
  #スタートとゴールの次数が偶数ならNG
  if not dic[1] % 2 or not dic[2] % 2:
    print("NG")

  else:
    #それ以外の頂点の次数が奇数ならNG
    for k in dic:
      if not k in (1,2) and dic[k] % 2:
        print("NG")
        break
    #それ以外はOK
    else:
      print("OK")

while True:
  try:
    solve()
  except EOFError:
    break

Submission status: Accepted

Solution code:
# AOJ 0086 Patrol
# Python3 2018.6.17 bal4u

while True:
	try: a, b = list(map(int, input().split()))
	except: break
	
	c = [0]*105
	n = 0
	while True:
		if a != b:
			c[a] += 1
			c[b] += 1
		if a > n: n = a
		if b > n: n = b
		a, b = list(map(int, input().split()))
		if a == 0: break

	f = False
	if (c[1]&1) and (c[2]&1):
		i = 3
		while i <= n:
			if (c[i]&1): break;
			i += 1
		if i > n: f = True
	print("OK" if f else "NG")

Submission status: Accepted

Solution code:
while True:
    try:
        cross=[0]*101
        while True:
            a,b=map(int,raw_input().split())
            if (a,b)==(0,0):
                break
            cross[a]+=1
            cross[b]+=1
        if cross[1]%2==cross[2]%2==1 and len(filter(lambda x:x%2==1,cross[3:]))==0:
            print "OK"
        else:
            print "NG"
    except:
        break

Submission status: Accepted

Solution code:
import sys

while True:
    L=[0]*101
    while True:
        try:
            a,b=map(int,raw_input().split())
        except EOFError:
            sys.exit()
        if a==b==0:break
        L[a]+=1
        L[b]+=1
    odd=0
    for i in L:
        if i%2==1:
            odd+=1
    print "NG" if odd!=2 else "OK"

Submission status: Accepted

Solution code:
import sys
nodes = [0] * 100
for line in sys.stdin:
	frm, to = map(int, line.split())
	if frm == 0 and to == 0:
		if nodes[1] & 1 and nodes[2] & 1 and all(not(i & 1) for i in nodes[3:]):
			print "OK"
		else:
			print "NG"
		nodes = [0] * 100
	else:
		nodes[frm] += 1
		nodes[to] += 1

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import (division, absolute_import, print_function,
                        unicode_literals)
from sys import stdin
from collections import Counter

cnt = Counter()
for line in stdin:
    start, end = (int(s) for s in line.split())
    if start or end:
        cnt[start] += 1
        cnt[end] += 1
    else:
        semi_eulerian = sum(i % 2 for i in cnt.values()) == 2
        if semi_eulerian and cnt[1] % 2 and cnt[2] % 2:
            print('OK')
        else:
            print('NG')
        cnt.clear()

Submission status: Accepted

Solution code:
while True:
	try:
		path = [0]*101
		while True:
			a,b = map(int, raw_input().split())
			if a == 0: break
			path[a-1] += 1; path[b-1] += 1
		for i in range(3,101):
			if path[i]%2 != 0:
				flag = 0
				break
		else:
			flag = 1
		if path[0]%2 == path[1]%2 == 1 and  flag:
			print "OK"
		else:
			print "NG"
   	except:
   		break

Submission status: Accepted

Solution code:
while True:
	try:
		path = [0]*100
		while True:
			a,b = map(int, raw_input().split())
			if a == 0: break
			path[a-1] += 1; path[b-1] += 1
		if path[0]%2 == path[1]%2 == 1 and sum(filter(lambda x:x%2==1,path[2:])) == 0:
			print "OK"
		else:
			print "NG"
   	except:
   		break

Submission status: Accepted

Solution code:
def f():
  c=[0]*101
  while 1:
    a,b=map(int,raw_input().split())
    if a==b==0:break
    c[a]+=1
    c[b]+=1
  f=0
  if c[1]%2==c[2]%2==1:
    for e in c[3:]:
      if e%2:break
    else:f=1
  print ["NG","OK"][f]
  return
while 1:
  try:f()
  except:break

Submission status: Accepted

Solution code:
while 1:
  try:
    c=[0]*101
    while 1:
      a,b=map(int,raw_input().split())
      if a==b==0:break
      c[a]+=1
      c[b]+=1
    c=map(lambda x:x%2,c)
    if c[1]==1==c[2] and sum(c[3:])==0:print"OK"
    else:print"NG"
  except:break

Submission status: Accepted

Solution code:
while 1:
  try:
    c=[0]*101
    while 1:
      a,b=map(int,raw_input().split())
      if a==b==0: break
      c[a]+=1
      c[b]+=1
    c=map(lambda x:x%2,c)
    if c[1]==1==c[2] and sum(c[3:])==0: print"OK"
    else: print"NG"
  except: break

Submission status: Accepted

Solution code:
while 1:
  try:
    c = [0]*101
    while 1:
      a, b = map(int, raw_input().split())
      if a==0==b:break
      c[a] += 1
      c[b] += 1
    c = map(lambda x: x%2, c)
    if c[1] and c[2] and sum(c[3:])==0:print"OK"
    else:print"NG"
  except:break

Submission status: Accepted

Solution code:
try:
  while 1:
    c=[0,1,1]+[0]*98
    while 1:
      a, b = map(int, raw_input().split())
      if a==0==b:break
      c[a] = -c[a]+1
      c[b] = -c[b]+1
    if c.count(1): print "NG"
    else: print "OK"
except:exit

Submission status: Accepted

Solution code:
while 1:
  c=[0,1,1]+[0]*98
  while 1:
    try: a, b = map(int, raw_input().split())
    except: exit()
    if a==0==b:break
    c[a] = -c[a]+1
    c[b] = -c[b]+1
  if c.count(1): print "NG"
  else: print "OK"

Submission status: Accepted

Solution code:
while 1:
  c=[0,1,1]+[0]*98
  while 1:
    try: a, b = map(int, raw_input().split())
    except: exit()
    if a==0==b:break
    c[a] = -c[a]+1
    c[b] = -c[b]+1
  print ["NG","OK"][sum(c)==0]

Submission status: Accepted

Solution code:
while 1:
  c=[0,1,1]+[0]*98
  while 1:
    try: a, b = map(int, raw_input().split())
    except: exit()
    if a==0==b:break
    c[a] = 1-c[a]
    c[b] = 1-c[b]
  print ["NG","OK"][sum(c)==0]

Submission status: Accepted

Solution code:
while 1:
  c=[0,1,1]+[0]*98
  while 1:
    try:a,b=map(int,raw_input().split())
    except:exit()
    if a==0==b:break
    c[a]=1-c[a]
    c[b]=1-c[b]
  print["NG","OK"][sum(c)==0]

Submission status: Accepted

Solution code:
while 1:
    edges = [0] * 101
    try:
        a, b = map(int, raw_input().split())
        edges[a] += 1
        edges[b] += 1
    except EOFError:
        break
    while 1:
        a, b = map(int, raw_input().split())
        if a == b == 0:
            break
        edges[a] += 1
        edges[b] += 1
    if edges[1] % 2 and edges[2] % 2 and all(map(lambda a: a % 2 == 0, edges[3:])):
        print "OK"
    else:
        print "NG"