Show if all continuous functions $f$ from $X \to \{0,1\}$ with the discrete topology are constant, then $X$ is connected. This problem was similar to this, but I couldn't make it work with the direction I want to show:
I tried showing the contrapositive (the inverse of the linked problem), which is showing "if $X$ is disconnected, then all the continuous functions $f$ are surjective" but I'm very stuck. 
My attempt was: $X$ disconnected $\implies X = A \cup B$ where $A$ and $B$ are disjoint open sets in $X$. And since $f$ is continuous, we know  $f^{-1}(\{ 0 \}), f^{-1}(\{ 1 \})$, and $f^{-1}(\{ 0,\, 1 \})$ are all open in $X$. 
My gut tells me because $A$ and $B$ are open, disjoint and cover $X$, that I could conclude, without loss of generality, that $f(A) = \{ 0 \}$ and $f(B) = \{ 1 \}$, which shows surjectivity. But I can't figure out if this idea is correct and, if it is, how to formalize it. 
 A: You don't start with a continuous function. You have to use one particular continuous function that is not a constant to get a contradiction assuming that $X$is not connected. If $X=A\cup B$ where $A$ and $B$ are disjoint non-empty open sets then define $f:X \to \{0,1\}$ by $f(x)=0$ for all $x \in A$ and $f(x)=1$ for all $x \in B$. Then $f$ is continuous because, for any  set $U \subset \{0,1\}$ we have $f^{-1}(U)=\emptyset, A,B$ or $A\cup B$. But $f$ is not a constant. This contradiction proves that $X$ is connected. 
A: You are on the right track. We will show, that if $X$ is not connected then there exists a continuous function from $X$ to $\{0,1\}$ which is not constant. If $X$ is not connected we can find a subset $A$ which is not the empty set nor $X$ which is open and closed. Note that $A^c$ also shares this property. Hence define our required function $f$ to be s.t $f=0$ on $A$ and $f=1$ on $A^c$. Check that $f$ is continuous.
A: Your answer is absolutely correct.
Because $f$ is continuous and $\{0\},\{1\}$ are open, the sets $f^{-1}(0)$ and $f^{-1}(1)$ yield two disjoint open sets that cover $X$. Since $f$ is not constant, these two sets are both nontrivial, so $X$ is disconnected.
