Prove that $A\cup B$ is connected.

You are on the right track. Continuing from your argument: in a similar fashion we see that $B$ must be contained in the other open, so $B \subseteq X$. Now we can use that $\bar{A} \cap B \neq \emptyset$, because that means there is a point $b \in B$ that is also in the closure of $A$. So every open containing $b$ must intersect $A$. In particular $X$ must intersect $A$, and now we have our contradiction.

Note that this argument works for any topological space, not just $\mathbb{R}^n$.


I once gave an answer to this question that is pretty similar (modulo notation) but you can also use the fact that

$X$ is connected iff every continuous $f:X \to \{0,1\}$ (the latter in the discrete topology) is constant.

So take $f: A \cup B \to \{0,1\}$ and assume it's continuous. Then by connectedness of $A$, $f\restriction_A$ is constant (say with value $a \in \{0,1\}$) and likewise $f\restriction_B$ is constant with value $b \in \{0,1\}$. By continuity of $f$ we know:

$$f[\overline{A}] \subseteq \overline{f[A]} = \{a\}$$

while of course $f[B]=\{b\}$. But as $\overline{A} \cap B \neq \emptyset$ it follows that $a=b$ and $f$ is constant and thus $A \cup B$ is connected.