We are given multiple datasets. For each dataset, we have n group definitions. Each group definition is of the form "groupi:member1,member2,...,membermi." (with a period at the end). Members can be either employee names (strings consisting of lowercase letters) or names of other groups (which are among the defined groups). There is no circular definitions. We are asked: output the number of employees included in the first group (group1). That is, we need to compute the set of employee members of group1 after expanding all nested group inclusions, removing duplicates. The group names themselves are not counted as employees; only those strings that are not among the defined group names are employees.
Constraints: n ≤ 100, each group has up to 10 members. Names are up to 15 lowercase letters. Input ends with a line containing 0.
We need to output the count of distinct employee names that are reachable from group1 via group memberships. Since there is no cycles, it's a DAG (directed acyclic graph). We can do DFS from group1, gathering employee names. Since employee names are strings, we can use a set. However, we must differentiate between employee names and group names. The groups are listed by name, and we have a mapping from group name to its direct member list (which includes both employees and subgroups). The input may contain groups that are never used, but group1 is the first defined group? Actually, the first group in the dataset is group1. The order of definitions is given: the first line after n is group1:... . So we know group1 is the first definition. So we need to compute the union of employees for group1.
Because there are no cycles, we can compute recursively with memoization to avoid recomputation for subgroups. For each group, we compute the set of employees that are members (direct or indirect). Then group1's count is the size of its set.
Potential issue: Since employees are also strings, it's possible that an employee name coincides with a group name? The problem says: "A member is another group name if it is one of group1, group2,..., or groupn. Otherwise it is an employee name." So if a string appears as a group name, then when it appears as a member, it is considered a group reference, not an employee. That means we should treat the set of group names as reserved; any string that is a group name is not an employee, even if the same string could also appear as an employee? But the problem states that members are either group names (if they are among the defined groups) or employee names. So if a string matches a group name, it's considered a group reference. But can there be an employee with the same name as a group? Possibly? The problem doesn't explicitly forbid that. But if an employee name coincides with a group name, then it would be ambiguous; however, the specification says: "A member is another group name if it is one of group1, group2,..., or groupn. Otherwise it is an employee name." So that means if a string that is in the member list exactly matches a defined group name, then it is interpreted as that group, not as an employee. So if there is an employee string that matches a group name, it would be impossible because then it would be treated as a group. But could there be a case where a group name also appears as an employee in another group? The problem states: "There are no circular (or recursive) definitions of group(s)." It doesn't explicitly forbid employee names equal to group names. However, typical problems avoid such ambiguity. To be safe, we should adhere to the rule: when parsing a member, if the name is among the known group names (from the definitions), we treat it as a group; otherwise, treat as employee. So even if an employee name coincides with a group name, it will be interpreted as a group reference, which might lead to expansion and possibly including employees from that group, but then the original "employee" would not be counted as an employee. However, I think it's safe to assume that employee names are distinct from group names, as the problem says "A member is another group name if it is one of group1, group2,..., or groupn. Otherwise it is an employee name." This implies that group names are a set; any member that matches a group name is a group, otherwise it's an employee. So if there were an employee name equal to a group name, it would be considered a group, so the problem likely ensures they are distinct. I'll proceed with that.
Thus algorithm:
For each dataset:
- Read n.
- If n == 0: break.
- Initialize a dictionary (map) groups: from group name to list of direct members (strings). Also store group names in a set.
- For i in 1..n: read line (strip). Remove the trailing period? The line ends with a period. We'll split appropriately.
- Find the colon index. group_name = line[:colon_index].
- After colon, there is a comma-separated list terminated by period. So we can take substring from colon_index+1 to -1 (excluding the period) and split by ','. That gives a list of member names. Possibly there could be empty? But mi >=1 so list non-empty.
- Store groups[group_name] = member_list.
- Now compute for group1: its transitive employee set.
We'll define a recursive function compute(group) that returns a set of employee names (strings). Use memoization (cache). Since no cycles, recursion safe but we can also use iterative topological order.
Procedure compute(group):
- If group in cache: return cache[group].
- Initialize an empty set result.
- For each member in groups[group]:
- If member is a group name (i.e., member in groups map) then:
subset = compute(member)
result = result union subset
- Else:
add member to result (since it's an employee)
- Store cache[group] = result and return.
Now answer = len(compute(group1)). Output that number.
Edge Cases: Group might be defined but not needed? No issue.
Also note: The specification says: "You may assume that mi member names of a group are mutually different." So direct members list has no duplicates, but indirect duplicates can occur (like alice appears twice after expansion). We handle by using set union.
Complexities: n ≤ 100, each group expansion could involve up to all employee names. Employee names might be many? Each group has at most 10 members, and depth could be up to n (100). Max total employee names possibly up to n * 10 = 1000, but duplicates. So manageable.
Now we need to implement reading input. The input lines may have spaces? Problem statement shows no spaces. So we can read lines as is, maybe strip whitespace like newline.
One nuance: The first group is group1, but note that group names may be defined in any order? Actually, the input defines n lines each with a group. The first line is group1, second line group2, etc. But the members can refer to groups defined later. That's fine because we build the map first, then compute.
Now test with given examples.
Example 1:
2
development:alice,bob,design,eve.
design:carol,alice.
Compute development: direct: alice (employee), bob (employee), design (group), eve (employee). design expands to {carol, alice}. Union: {alice, bob, eve, carol} = 4.
Output 4.
Example 2:
3
one:another.
another:yetanother.
yetanother:dave.
one -> another -> yetanother -> {dave}. So 1.
Example 3:
3
friends:alice,bob,bestfriends,carol,fran,badcompany.
bestfriends:eve,alice.
badcompany:dave,carol.
friends direct: alice (e), bob (e), bestfriends (g), carol (e), fran (e), badcompany (g).
bestfriends: {eve, alice}
badcompany: {dave, carol}
Union: alice, bob, carol, fran, eve, dave -> 6? Let's count: alice, bob, carol, fran, eve, dave = 6. Yes.
Example 4:
5 groups a,b,c,d,e forming a chain? a:b,c,d,e. b:c,d,e,f. c:d,e,f,g. d:e,f,g,h. e:f,g,h,i.
We need to compute a's employees. Since all members are groups except maybe some? Let's parse: a's direct: b,c,d,e (all groups). So expand each.
b: c,d,e,f -> f is an employee? Actually f is not defined as a group? Check groups: a,b,c,d,e are defined, so f is not a group => employee. Similarly c: d,e,f,g -> g employee; d: e,f,g,h -> h employee; e: f,g,h,i -> i employee. Also perhaps more. So a's employees are all employees reachable. That includes f,g,h,i? Also maybe something else. Let's compute properly: Starting from a. a includes b,c,d,e. b includes c,d,e,f. c includes d,e,f,g. d includes e,f,g,h. e includes f,g,h,i. So union: f,g,h,i plus maybe? Are there any employees defined in other groups? a's direct list no employees. So all employees are f,g,h,i. But note that f appears multiple times. So set size = 4. Output 4.
Example 5:
4
aa:bb.
cc:dd,ee.
ff:gg.
bb:cc.
Group1: aa. aa includes bb (group). bb includes cc (group). cc includes dd,ee (employees? dd and ee are not defined as groups? There is no group named dd, ee; so they are employees). So aa's employees are dd and ee = 2. (Note also ff and gg are defined but not used). Output 2.
Matches given outputs.
Thus the solution is straightforward.
Now we need to implement the input reading. Since lines end with period, we can read using sys.stdin.
Implementation details:
- Use sys.stdin to read lines. We'll read line by line, ignoring blank lines? Possibly not needed.
- First line integer n. If n == 0: break.
- Then for i in range(n): read line; strip trailing newline and also possibly trailing spaces. But line might have trailing spaces? Unlikely. We'll use line = sys.stdin.readline().rstrip('\n'). Then we can process.
- However, note that the line might contain spaces? The problem shows no spaces. So we can safely split at colon and comma.
Processing each line:
line = line.strip()? We can use strip to remove leading/trailing whitespace but be careful about period: line ends with '.' so we need to keep that. Using line.strip() will remove spaces at ends, but it's fine.
col_index = line.find(':')
group = line[:col_index].strip()
members_str = line[col_index+1:] # includes comma-separated list ending with period.
Remove the trailing period:
if members_str.endswith('.'):
members_str = members_str[:-1]
Then split by ',' to get list:
members = members_str.split(',')
However, there may be empty members? mi >= 1, so there will be at least one.
members list may contain strings that could be empty if there is trailing comma? But spec says comma-separated list, no trailing comma before period. So safe.
Store groups[group] = members.
Now after reading all groups, we have groups dict.
Now we need to compute transitive closure for group1.
We'll have a cache dict mapping group name to set of employees.
Define a recursive function:
def expand(group):
if group in cache:
return cache[group]
res = set()
for member in groups[group]:
if member in groups: # is a group
res.update(expand(member))
else:
res.add(member)
cache[group] = res
return res
Now answer = len(expand(group1))
Print answer.
One nuance: In the dataset, group1 is the first group defined, not necessarily the name "group1". So we need to store its name. We can either store first group name in a variable.
During reading, we can store first_group_name = None. When i==0, set first_group_name = group.
Then after reading, compute for that name.
Alternatively, since group1 appears first, we can simply use the key of the first definition. But we have groups dict which is unordered (in Python, dict preserves insertion order from 3.7+). However, to be safe, we can store separately.
I'll store first_group = None and set it when reading first line.
Now test with sample.
Implementation in a main function.
Potential recursion depth: The graph depth could be up to n=100, which is safe for Python recursion limit default (1000). So fine.
Alternatively, we could use iterative topological sort. But recursion is easier.
Now let's consider any edge cases:
-
Group definitions might have employee names that coincide with group names? But as per rules, if a member matches a group name, it is considered a group. So we must treat it as such. Our code does: if member in groups: then treat as group. That matches specification. But what if an employee name is the same as a group that is defined later? Actually, groups map is built before processing, so member in groups returns True if it's any group name. So that's consistent. However, if an employee name equals a group name, it will be wrongly treated as group. But problem likely ensures distinctness? There's no explicit statement that group names and employee names are disjoint. However, given the rule "A member is another group name if it is one of group1, group2,..., or groupn. Otherwise it is an employee name." this means that the classification is based on whether the string is among the defined group names. So even if a string also could be an employee, it's always interpreted as a group if it matches a group name. That implies that an employee could not have the same name as any group, because it would then be considered a group. The problem might allow such a scenario, but it would cause the employee to never be counted as an employee; however, the specification clearly says "otherwise it is an employee name", meaning if it's not a group name, it's an employee. So it's a mutually exclusive classification: each member is either a group name or an employee name, not both. So the input should guarantee that there is no overlap. Good.
-
There might be employees that are also group names but not defined? Not possible because group names are only those defined. So if an employee name equals a group name, then it's defined as a group, so it's a group. So the input would have that string as both a group and an employee member? But they said group names are mutually different, but not that employee names are distinct from group names. But if an employee name equals a group name, then that member in some group would be considered a group according to the rule. The input might still include that string as an employee in some other group, but then it would be ambiguous. I'd still follow the rule; if the member is in groups, treat as group. That's correct.
Now we need to consider possibility of groups that have no employees (only subgroups)? That's fine. Their set will be empty.
Now we need to ensure that we handle datasets where group1 is defined but might not have any employees? Then answer = 0.
But is that allowed? mi >= 1 so each group has at least one member. But it could be only subgroups. So after expansion, it's possible that all members are groups that eventually lead to no employees? But subgroups also must have at least one member. However, if there is a cycle? But cycles are prohibited. Could there be a leaf group that consists only of employees? Yes. So if there is a chain that eventually ends at leaf groups with employees, then group1 might have employees. But could there be a leaf group that has only subgroups? Not possible because each group must have at least one member. If it has only subgroups, then those subgroups must have members, etc. Eventually there must be employees? Not necessarily: could all groups ultimately refer to each other? But cycles are prohibited, so eventually there must be a group that contains at least one employee, otherwise you'd have infinite descent or a cycle. Since no cycles and each group has at least one member, the graph is a DAG. In a finite DAG with no source (employee leaf) of employee nodes? Actually, groups are nodes, and employee nodes are terminal (no outgoing edges). The DAG must have at least one employee because otherwise it would be impossible to have finite nodes without cycles? Let's think: Suppose we have n groups, each group's members are all group names (so all are references to other groups). Since there are n groups and no cycles, there must be a group that eventually references something that is not a group? But if all members are group names, then the graph edges are from groups to groups. No cycles, so it's a DAG. In a finite DAG, there must be sinks (nodes with out-degree zero). But if a group is a sink, it must have no outgoing edges. But it must have at least one member, so if its members are all group names, those would be outgoing edges, so it wouldn't be a sink. Therefore, a sink group cannot have all members as group names because then it would have outgoing edges. So a sink group must have at least one employee member. Therefore, eventually there will be employees. So group1 will have at least some employees? Possibly if group1 is not connected to any sink? Actually, from group1 you can traverse edges; since the graph is a DAG and all paths end at sinks, which have employees. So group1 will include those employees. So answer should be at least 1? Not necessarily, because group1 might be defined but if its members are groups that eventually have employees, yes. But could there be a group that itself is a sink? That would have employees. So group1 is either a sink (has employees) or has transitive employees. So answer >= 1. In the example, we got 1 for the second dataset. So answer can be 1. Could answer be 0? Only if group1 has no members? But mi>=1, so not. So answer>=1. But it's fine.
Now we need to output number of employees. So integer.
Implementation wise: Use sys.stdin to read lines.
I'll write the solution.
Potential pitfalls:
-
Input lines may have trailing spaces. We'll strip them before processing? It's safer to strip the entire line and then process. However, if we strip, we remove spaces that could be part of the name? Names consist of lowercase letters, no spaces. So it's safe to strip whitespace. We'll do line = line.strip().
-
Splitting: after colon, we have something like "alice,bob,design,eve." So members_str includes the period. We'll remove period by checking if last character is '.' and slice.
-
However, there could be a situation where the line ends with '.' but there might be spaces after the period? If we stripped, the period will be last char.
-
Also there could be lines like "development:alice,bob,design,eve." with no spaces.
Now the code:
import sys
def solve():
import sys
lines = sys.stdin.readlines()
# But we need to process sequentially; simpler: read line by line.
# Actually we can use iterator.
it = iter(sys.stdin.read().splitlines())
# but we need to handle zero sentinel.
# We'll use while True:
out_lines = []
while True:
line = sys.stdin.readline()
if not line:
break
line = line.strip()
if line == '':
continue # skip empty lines? but input may not have empty lines.
n = int(line)
if n == 0:
break
groups = {}
first_group = None
for i in range(n):
line = sys.stdin.readline().strip()
# Possibly there are blank lines? Usually not. But just in case:
while line == '':
line = sys.stdin.readline().strip()
# Find colon
colon_idx = line.find(':')
group_name = line[:colon_idx]
if first_group is None:
first_group = group_name
members_str = line[colon_idx+1:]
# Remove trailing period
if members_str.endswith('.'):
members_str = members_str[:-1]
members = members_str.split(',')
groups[group_name] = members
# Now compute
cache = {}
def expand(grp):
if grp in cache:
return cache[grp]
res = set()
for m in groups[grp]:
if m in groups:
res.update(expand(m))
else:
res.add(m)
cache[grp] = res
return res
employees = expand(first_group)
out_lines.append(str(len(employees)))
sys.stdout.write("\n".join(out_lines))
if name == 'main':
solve()
But careful: In reading, we used sys.stdin.readline() inside while loop. This might cause issues if we mix reading from sys.stdin after having read some lines. However, our approach is fine: we read n, then for each dataset we read n lines.
But what if there are trailing spaces after the period? Using strip() removed them. Then members_str might not end with '.' because we stripped the whole line. For example, line = "design:carol,alice." after strip, it's "design:carol,alice." so members_str = "carol,alice." Then we check if members_str.endswith('.'): it does, so we remove it, good.
What if there is a line like "design:carol,alice. " with a space before newline? Strip removes trailing spaces, so it becomes "design:carol,alice.", same.
Now about reading until zero: The sentinel is a line containing a single "0". Might have spaces? Probably just "0". We'll parse as int after strip, so okay.
Now test with provided example. Let's manually run.
I'll run mental simulation.
But there is a nuance: In the dataset, n lines for groups. There is no guarantee that groups are listed in order and that they are not separated by blank lines. Usually not. However, we should be robust to possible blank lines; but the problem likely doesn't include blank lines. But to be safe, we can read lines and skip empty ones when reading group definitions. However, if we skip empty lines, we might read fewer than n group lines. So we should read exactly n non-empty lines, but the input spec doesn't mention blank lines, so I'll assume none.
Alternatively, we could read the entire input and process it tokenized. But simpler to use readline.
Edge Cases: When we call expand recursively, the recursion uses groups dict for membership check. It's fine.
Potential bug: The expand function uses groups[grp] to get members. But what if we call expand on a group that is not defined? All groups are defined, so no issue.
Now about the cache: We store sets; they could be mutated later? No.
Now confirm with the 5th dataset: groups: aa:bb., cc:dd,ee., ff:gg., bb:cc. . So first_group = aa. expand(aa): members [bb] -> bb in groups, so expand(bb): members [cc] -> cc in groups, so expand(cc): members ['dd','ee'] -> neither in groups? groups has cc, dd is not a group, ee not a group. So add dd, ee. Return set {dd,ee} for cc. So bb returns same {dd,ee}. aa returns {dd,ee}. Count 2. Good.
Now consider if there is a group that includes an employee name that is also a group name? As discussed, we treat it as group, which might cause missing the employee. But spec says otherwise. However, is it possible that a group name appears as an employee in a different context? Suppose there is a group name "alice", and in some group's member list "alice" appears. According to rule, it's a group reference. But there might also be an employee named "alice" that appears elsewhere? Then "alice" would be a group name and cannot be an employee. So if the input includes a group named "alice", then any occurrence of "alice" as member is considered a group, not employee. So the employee "alice" would never appear; it would be inconsistent. So we can safely follow the rule.
Now one more nuance: The specification says: "The name preceding the colon is the group name and the names following it specify its members." and "A smaller task group may be included in a larger one. So, a group name can appear as a member of another group, for instance, as follows." So it's clear that group names are used as members. It also says "A member is another group name if it is one of group1, group2,..., or groupn. Otherwise it is an employee name." So the rule is unambiguous. So we implement accordingly.
Now one more nuance: Could there be duplicate employee names across groups? We use set, so distinct.
Now we need to output each answer on its own line.
Time complexity: For each group, we might union sets. In worst-case, each group's set could contain all employees (max maybe number of employees <= total members? Actually, employees are distinct strings that appear as leaves. The total number of employee names could be up to sum of mi? But each group has up to 10 members, n <=100, so total member entries ≤ 1000. However, some members are groups. The number of distinct employee names is at most 1000 (if all entries are employees). So sets are small. Union operations are fine.
Alternative approach: We can topologically sort groups and compute sets in reverse order. But recursion with memo is simpler.
Now let's test with a dataset where group1 is defined but not expanded due to recursion depth? Should be fine.
Now we need to watch out for Python recursion depth default 1000. Depth up to n=100, safe.
Now what about large input? Not large.
Now confirm that we correctly parse lines with possible trailing spaces after period. We remove period, then split by comma. That yields the list of members. It could be that there is an empty string if there's a trailing comma? Not according to spec. So okay.
Now about reading lines: The line may contain multiple spaces? The problem says names consist of lowercase letters and colon, commas, period. So no spaces. So it's safe.
Now final code. I'll add some comments.
Potential improvement: We might want to use sys.setrecursionlimit, but not needed.
Now one more test: Suppose there is a group that includes an employee name that is the same as a group name but that group name is defined later? For example, group1: a,b. group2: x,y. Here a is an employee, but later there is a group named a? That would be ambiguous. But if group name a is defined, then a appears as member in group1, but group1 defined before group a? Does order matter? According to rule, if a is defined as a group anywhere, then it's a group name. So at the time of parsing group1, we don't know yet that a will be defined as group? But after reading all definitions, we have the set of all group names. So when processing membership, we treat member as group if its name is in the set of all group names. So it's fine, regardless of definition order.
Thus our approach is correct.
Now output: Should be just numbers, each on new line. Our out_lines accumulates and then prints with newline.
We can also print as we go.
Now final code.
But I realize we used both sys.stdin.readline and sys.stdout.write. It's fine.
Double-check reading loop: while True: line = sys.stdin.readline().strip() if not line: break? Actually if line is empty due to EOF, readline returns '', strip yields ''. But there might be blank lines in between datasets? Input specification doesn't mention blank lines. However, to be robust, we could read lines until we get a non-empty line for n. But if we read a blank line and treat as n, it would cause ValueError when converting to int. So we should skip blank lines before reading n.
Better to do:
import sys
def main():
out = []
while True:
line = sys.stdin.readline()
if not line:
break
line = line.strip()
if line == '':
continue
if line == '0':
break
n = int(line)
groups = {}
first = None
for _ in range(n):
line = sys.stdin.readline()
# skip possible blank lines
while line is not None and line.strip() == '':
line = sys.stdin.readline()
line = line.strip()
colon = line.find(':')
group = line[:colon]
if first is None:
first = group
members_str = line[colon+1:]
if members_str.endswith('.'):
members_str = members_str[:-1]
members = members_str.split(',')
groups[group] = members
cache = {}
def expand(g):
if g in cache:
return cache[g]
res = set()
for m in groups[g]:
if m in groups:
res.update(expand(m))
else:
res.add(m)
cache[g] = res
return res
out.append(str(len(expand(first))))
sys.stdout.write('\n'.join(out))
if name == 'main':
main()
This should work.
Potential bug: In the reading of group lines, we skip blank lines by while line.strip() == '' but we need to be careful not to get infinite loop at EOF. But because the input is well-formed, we don't expect blank lines. However, we can simply assume no blank lines, and not include that while loop. Simpler: just read and strip each line; if it's empty, that would be an error. So I'll not include the while loop.
But there might be stray newline? Possibly but not.
Thus final solution.
Now I'll write the final answer in the required format.
But the problem statement says "Output: For each dataset, output the number of employees included in the first group of the dataset, that is group1, in a line." So just the number.
Alright.
Now, it's a simple solution.
We should also consider memory: Not large.
Now, I'll produce final code with comments.
One more nuance: In the input, there might be a group name that appears as a member but is not defined? But spec says: "A member is another group name if it is one of group1, group2,..., or groupn." That implies that if a member is a group name, it must be one of the defined groups. So no undefined group references. So safe.
Now final answer.
I'll now write the final code in a clean way.
Note: For the expansion, we should ensure we treat groups as defined only if they appear in the groups dictionary. That's correct.
Potential alternative: Use iterative topological order: compute indegrees? But unnecessary.
Now we can output.
Thus final solution.
I'll include an explanation in the final answer? The user asked: "Write a program". So I should output just the code? The typical response on this platform is to provide the solution code in the answer. The instructions: "You are ChatGPT... provide final answer". So I'll provide the code.
But before that, I should consider whether there is any trick: The input may have groups that are not used, and group1 may include them or not. That's fine.
Another possible nuance: The group names and employee names are case-sensitive? All lowercase letters. So consistent.
Now think about depth-first recursion: Since there are no cycles, we won't have infinite recursion. However, if there is a very deep chain (e.g., n=100), recursion depth is 100, safe.
But what about Python default recursion limit? It's 1000, so fine.
Now final code.
Edge case: Could there be groups with multiple members that are groups, and we need to avoid duplicate expansions? Using set union ensures distinct employees.
Now let's test with some custom cases:
Case: n=1, group1: alice. -> employees {alice} -> output 1.
Case: n=3, a:b,c. b:d. c:d. d:e. (but note d defined? Actually group d is defined? If d is not defined, then b's member d is employee? But d is not defined as group, so it's employee. Then a expands to {d} from b and {d} from c, set = {d} -> 1. Works.
Case: n=2, a:b. b:a. This is a cycle, but problem says no circular definitions, so won't appear.
Now about output: No extra characters.
Now finalize.
I'll produce the code with function solve().
Now I'll write the answer. ```python
import sys
def solve() -> None:
out = []
while True:
line = sys.stdin.readline()
if not line:
break
line = line.strip()
if line == '':
continue
if line == '0':
break
n = int(line)
groups = {} # group name -> list of direct members
first_group = None # name of group1
for i in range(n):
line = sys.stdin.readline().strip()
# Locate the colon
colon = line.find(':')
group_name = line[:colon]
if first_group is None:
first_group = group_name
members_str = line[colon+1:] # includes trailing period
if members_str.endswith('.'):
members_str = members_str[:-1] # remove the period
members = members_str.split(',')
groups[group_name] = members
# cache for already expanded groups
cache = {}
def expand(g: str) -> set:
"""Return the set of all employees reachable from group g."""
if g in cache:
return cache[g]
result = set()
for member in groups[g]:
if member in groups: # it's a subgroup
result.update(expand(member))
else: # it's an employee
result.add(member)
cache[g] = result
return result
employees = expand(first_group)
out.append(str(len(employees)))
sys.stdout.write('\n'.join(out))
if name == 'main':
solve()