Treat every email as a node and connect (union) all emails that appear in the same account. Two accounts belong to the same person exactly when their email sets sit in the same connected component, regardless of the name. The name is never a merge signal — only a shared email is.
Problem
You are given a list of accounts where each element accounts[i] is a list of strings. The first element accounts[i][0] is a name, and the rest of the elements are emails representing emails of the account.
Two accounts definitely belong to the same person if there is some common email to both accounts. Note that even if two accounts have the same name, they may belong to different people, as people could have the same name. A person can have any number of accounts initially, but all of their accounts definitely have the same name.
After merging the accounts, return the accounts in the following format: the first element of each account is the name, and the rest of the elements are emails in sorted order. The accounts themselves can be returned in any order.