Three types of Query
void associate(int a,int b) // make a and b connected directly
bool isAssociated(int a,int b) // whether a and b are directly connected
bool isConnected(int a,int b) // Whether a and b are directly/undirectly connected
Pretty straight forward : went with union find
Twist:
TC of find()? in worst case ? //O(n)
Can we optimize? Yes: path compression.
What about TC now? o(logn)
How?
Took some 10 mins here. had to derive and go through examples how log(n) is upper bound
Coded in remaining 10-15 mins. Almost correct,
Didn't get time to dry run.
I'm feeling its a reject (been a week).
Feeling bad after doing so many hards on union-find and stumbling upon deriving its TC
Take away: brush up the very basics as well.