Lemma: If $X$ is any topological space, $Z\subseteq X$ is both open and closed, $V\subseteq X$ is a connected set then either $V\subseteq Z$ or $V\subseteq X\setminus Z$.
I'll leave the proof of the lemma to you because it is almost trivial. Now let's use it for your exercise. Let $Z\subseteq\cup_{i\in E} A_i$ be a set which is both open and closed. For each $i\in E$ the set $A_i\subseteq \cup_{i\in E}A_i:=Y$ is connected and hence by the lemma $A_i\subseteq Z$ or $A_i\subseteq Y\setminus Z$. But now suppose that there are $i,j\in E$ such that $A_i\subseteq Z$ and $A_j\subseteq Y\setminus Z$. That means there is no element which is both in $A_i$ and $A_j$ which is a contradiction. So we conclude that either $A_i\subseteq Z$ for all $i\in E$ or $A_i\subseteq Y\setminus Z$ for all $i\in E$. If $A_i\subseteq Z$ for all $i\in E$ then the union is also a subset of $Z$ and hence $\cup_{i\in E}A_i=Z$. And if $A_i\subseteq Y\setminus Z$ for all $i\in E$ then $Z$ is empty.