Prove that $G$ is an open interval for two-valued continuous function $f$ 
Suppose $G\subset \mathbb R$ is a non-empty open set such that the function $f:G \rightarrow \{0,1\}$ is a two-valued function and is continuous. Show that any two-valued function on $G$ is a constant if and only if the set $G$ is an open interval.

I am concerning about the construction of open set for set $G$ and have no idea to start off with. 
 A: Theorem: A metric space $X$ is connected if and only if any continuous function $f:X\to \{0,1\}$ is constant. 
Proof:Suppose $X$ is connected and $f:X\to \{0,1\}$ is continuous. If $f$ is not a constant function, then $f$ is onto. Let $A=f^{-1}(0)$ and $B=f^{-1}(1)$. Then $A\cup B=X$,and $A,B\neq \emptyset$. Also note that both are proper subsets of $X$ and are open and  closed in $X$, a contradiction. 
Suppose $X$ is not connected. Let $A$ and $B$ be the disconnection. Then define $f:X\to \{0,1\}$ such that  $$f(x)=\begin{cases}
0, &\text{if $x\in A$}\\
1,&\text{if $x\in B$}
\end{cases}$$
$f$ is a non-constant continuous function(verify).
Another useful theorem is

A subset  $I$ of $\Bbb{R}$ is connected if and only if $I$ is an
  interval.

You can find a proof here.
I hope now you can complete your answer on your own.
A: If $G$ is an open interval then IVP tells you that if $f$ if takes both the values $0$ and $1$ then it must take all values in between. This proves one way.
For the other way use the fact any open set is a disjoint union of open intervals. If $G$ is a union of two or mote intervals define $f$ to be $1$ on one of them and $0$ on all the others. You will get a continuous function taking both the values $0$ and $1$ (and no other value).
