Hint:
Prove (or use if you have already proved this) that $\pi_0$ is functorial. That is, prove that maps $f:X\longrightarrow Y$ induce maps map $f_*:\pi_0(X) \longrightarrow \pi_0(Y)$ in such a way that if we have $f:X\longrightarrow Y$ and $g:Y\longrightarrow Z$ then $(g\circ f)_* =g_*\circ f_*$, and $1_X:X\longrightarrow X$ induces $1_{\pi_0(X)}:\pi_0(X) \longrightarrow \pi_0(X)$.
Also prove that if you have $f\simeq g$ then $f_*=g_*$.
Using these facts the result follows easily.
Added later:
To elaborate: Let $S^0=\lbrace -1,1\rbrace $. Let $X$ be a topological space. Fix $x_0\in X$ (the basepoint). Identify $\pi_0(X)$ as the quotient of the set of all maps $f:S^0\longrightarrow X$ sending $1$ to $x_0$, divided by the relation of based homotopy equivalence. Using this definition the functoriality of $\pi_0$ is much easier. The equivalence is not necessarily pointed, so this approach to prove the hint is not adequate, see below.
Added even later:
All the maps in the original hint should be basepoint preserving for construction in the first addendum above to work, sorry (meaning that this "hint" doesn't work as it is). Meaning that the first addendum doesn't work.
(Hopefully) Final Addendum:
I was going to justify that the hint does work (it really does, see the answer linked), but it turns out that this question has been asked (and answered) before: https://math.stackexchange.com/a/26188/92139