We represent the given information by a graph the vertices of which correspond to the 117 spies. Two vertices are connected iff the corresponding spies share a password which allows them to communicate directly. We need to ensure that at least one of the sub-graphs formed by spies sharing the same mission is connected.
To find the number of edges needed to ensure that a graph with a≥2 vertices is connected, no matter how the edges are placed, we first observe that the complete graph with a−1 vertices has 2(a−1)(a−2) edges. We next prove that it is sufficient to have
2(a−1)(a−2)+1=2a2−3a+4
edges. To see this, assume that the graph is not connected. Then there exist two sub-graphs, not connected to each other, one with x≥1 and the other with a−x≥1 vertices. The number of edges in such a graph is at most
2(a−x)(a−x−1)+2x(x−1)=22x2−2ax+a2−a==2a2−3a+2−(a−x−1)(x−1)≤2a2−3a+2=2(a−1)(a−2)
If the sizes of the three missions are a, b and c, the above shows that issuing
n=2a2−3a+2+2b2−3b+2+2c2−3c+2+1
passwords, ensures that at least one mission is networked. Finally, we need to minimise 2n=a2+b2+c2−3(a+b+c)+8 subject to the condition a+b+c=117. Because
3(a2+b2+c2)−(a+b+c)2=2(a2+b2+c2−ab−bc−ca)==(a−b)2+(b−c)2+(c−a)2≥0,
we obtain
2n≥3(a+b+c)2−3(a+b+c)+8=31172−3⋅117+8=4220,
with equality iff a=b=c=39. This shows that the minimum number of passwords needed to ensure that at least one mission is networked is n=2110. This number is sufficient iff the three missions are of equal size.