cheran-senthil / PyRival

⚡ Competitive Programming Library
https://pyrival.readthedocs.io/
Apache License 2.0
1.17k stars 312 forks source link

Make DisjoinSetUnion.py more ICPC friendly #88

Closed hairez closed 11 months ago

hairez commented 11 months ago

One of many changes we want to make to the code on PyRival to make it more ICPC friendly. More specifically, it would be nice with shorter code, so more code could fit in the team reference document. Additionally, it would be easier to copy of by hand during a competition.

hairez commented 11 months ago
def find(self, a):
        while a != self.par[a]:
            a,self.par[a] = self.par[a], self.par[self.par[a]]
        return a

@bjorn-martinsson mentioned that version 2 is actually faster, since all parents are mapped to the same parent which is the furthest away, which is faster in general.

def find(self, a):
        acopy = a
        while a != self.parent[a]:
            a = self.parent[a]
        while acopy != a:
            self.parent[acopy], acopy = a, self.parent[acopy]
        return a
cheran-senthil commented 11 months ago

I kinda disagree, this is never going to be used for ICPC because Python is too slow, this is for personal use where the ability to immediately have clarity is the most important thing.

cheran-senthil commented 11 months ago

It's three letters at each occurrence, at this point it is more practical to practice your typing.