How can I prove the following statement?
Given a function $U: A \subset \mathbb{R}^n \to \mathbb{R}$ with $A$ connected set, $$\nabla U=\bar{0} \,\,\,\,\,\,\, \forall \bar{x} \in A\implies U=\mathrm{constant} \,\,\,\,\,\,\, \forall \bar{x} \in A$$
In particular I do not understand how the condition of $A$ as a connected set is necessary to prove the statement.
If $A$ is connected and open and $x,y\in A$ are arbitrary with $[x,y]:=\{ ta+(1-t)b \mid t\in [0,1]\}\subset A$, there exists a number $t_0 \in (0,1)$ with $$U(y)-U(x)= \nabla U (x+t_0(y-x))\cdot(y-x) .$$ Pick $a,b \in A$ and link them with a polygon with vertex points $x_1, \dotsc, x_k$ ($k\in \mathbf N$) where $x_1 =a$ and $x_k=b$. Applying the intermediate value theorem on $[x_{j-1},x_{j}]$ ($j=2, \dotsc, k$) gives $$U(a)-U(x_2)=U(x_1)-U(x_2)=0 \qquad \dotsc \qquad U(x_{k-1})-U(x_k)=U(x_{k-1})-U(b)=0$$ since $\nabla U=0$ and hence $$U(a)=U(x_1)= \dotsc = U(x_{k-1})=U(x_k)=U(b).$$ Since $a$ and $b$ were arbitrary we showed that $U$ is constant.