Rezultati

Up. imeNalogaJezikRezultatČas oddaje
kjejenovictk-2019 Barvanje drevesa Python 3 0/100Napačen odgovor (WA) 24. apr '19 @ 19:58

Test Točke Porabljen spomin Porabljen čas Status
#1 [več] 10/10 8,520 MiB 0,000 s OK
#2 [več] 10/10 8,668 MiB 0,000 s OK
#3 [več] 10/10 8,523 MiB 0,000 s OK
#4 [več] 10/10 8,609 MiB 0,000 s OK
#5 [več] 10/10 8,676 MiB 0,000 s OK
#6 [več] 0/10 9,211 MiB 0,000 s Napačen odgovor
#7 [več] 0/10 9,191 MiB 0,000 s Napačen odgovor
#8 [več] 10/10 9,168 MiB 0,000 s OK
#9 [več] 10/10 9,750 MiB 0,385 s OK
#10 [več] 0/10 9,215 MiB 0,000 s Napačen odgovor

Ocenjevani program (barvanje.py):
import sys
data = sys.stdin.readlines()[1:]
#data = open("barvanjetest","r").readlines()[1:]



class Graf:
    myDirection = None
    parent = None
    leftChild = None
    rightChild = None
    leftNeighbour = None
    rightNeighbour = None
    barva = None
    def __init__(self,i):
        self.i = i

    def getLeft(self):
        if not self.leftNeighbour:
            if self.myDirection == "R":
                if not self.parent.leftChild:
                    parentLeft = self.parent.getLeft()
                    if parentLeft:
                        self.leftNeighbour = parentLeft.rightChild
                else:
                    return self.parent.leftChild
            if self.myDirection == "L":
                parentLeft = self.parent.getLeft()
                if parentLeft:
                    if parentLeft.rightChild:
                        self.leftNeighbour = parentLeft.rightChild
                    else:
                        self.leftNeighbour = parentLeft.leftChild
        return self.leftNeighbour

    def getRight(self):
        if not self.rightNeighbour:
            if self.myDirection == "L":
                if not self.parent.rightChild:
                    parentRight = self.parent.getRight()
                    if parentRight:
                        self.rightNeighbour = parentRight.leftChild
                else:
                    return self.parent.rightChild
            if self.myDirection == "R":
                parentRight = self.parent.getRight()
                if parentRight:
                    if parentRight.leftChild:
                        self.rightNeighbour = parentRight.leftChild
                    else:
                        self.rightNeighbour = parentRight.rightChild
        return self.rightNeighbour


slovarObjektov = {}
parentless = set()
i = 1
for vrstica in data:
    levi, desni = list(map(int,vrstica.strip().split()))
    if i not in slovarObjektov:
        parentless.add(i)
    vozlisce = slovarObjektov.get(i, Graf(i))
    slovarObjektov[i] = vozlisce
    if levi != 0:
        vozlisce.leftChild = slovarObjektov.get(levi, Graf(levi))
        slovarObjektov[levi] = vozlisce.leftChild
        vozlisce.leftChild.parent = vozlisce
        vozlisce.leftChild.myDirection = "L"
        parentless.discard(levi)
    if desni !=0:
        vozlisce.rightChild = slovarObjektov.get(desni, Graf(desni))
        vozlisce.rightChild.parent = vozlisce
        slovarObjektov[desni] = vozlisce.rightChild
        vozlisce.rightChild.myDirection = "R"
        parentless.discard(desni)
    i+=1

for indeks in range(1,len(slovarObjektov)+1):
    barvesosedov = []
    vozlisce = slovarObjektov[indeks]
    for i in [vozlisce.parent, vozlisce.leftChild, vozlisce.rightChild, vozlisce.getLeft(), vozlisce.getRight()]:
        if i:
            if i.barva:
                barvesosedov.append(i.barva)
    for j in range(1,6):
        if j not in barvesosedov:
            vozlisce.barva=j
            break

barve = []
for indeks in range(1,len(slovarObjektov)+1):
    barve.append(slovarObjektov[indeks].barva)

print(*barve)



#print(slovarObjektov[7].getLeft().i)