01611: The Suspects
http://cs101.openjudge.cn/practice/01611/
Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. To minimize transmission to others, the best strategy is to separate the suspects from others. In the Not-Spreading-Your-Sickness University (NSYSU), there are many student groups. Students in the same group intercommunicate with each other frequently, and a student may join several groups. To prevent the possible transmissions of SARS, the NSYSU collects the member lists of all student groups, and makes the following rule in their standard operation procedure (SOP). Once a member in a group is a suspect, all members in the group are suspects. However, they find that it is not easy to identify all the suspects when a student is recognized as a suspect. Your job is to write a program which finds all the suspects.
输入
The input file contains several cases. Each test case begins with two integers n and m in a line, where n is the number of students, and m is the number of groups. You may assume that 0 < n <= 30000 and 0 <= m <= 500. Every student is numbered by a unique integer between 0 and n−1, and initially student 0 is recognized as a suspect in all the cases. This line is followed by m member lists of the groups, one line per group. Each line begins with an integer k by itself representing the number of members in the group. Following the number of members, there are k integers representing the students in this group. All the integers in a line are separated by at least one space. A case with n = 0 and m = 0 indicates the end of the input, and need not be processed.
输出
For each case, output the number of suspects in one line.
样例输入
100 4
2 1 2
5 10 13 11 12 14
2 0 1
2 99 2
200 2
1 5
5 1 2 3 4 5
1 0
0 0样例输出
4
1
1来源
Asia Kaohsiung 2003
"""
use a technique called Disjoint-set Union (DSU) or Union-Find, which is a data structure that
provides efficient methods for grouping elements into disjoint (non-overlapping) sets and
for determining whether two elements are in the same set.
"""
class UnionFind:
def __init__(self, n):
self.parent = list(range(n)) # Each student initially in their own set
self.rank = [0] * n # Rank of each node for path compression
def find(self, x):
# Find the representative (root) of the set that x is in
if self.parent[x] != x:
self.parent[x] = self.find(self.parent[x]) # Path compression
return self.parent[x]
def union(self, x, y):
# Union the sets that x and y are in
root_x = self.find(x)
root_y = self.find(y)
if root_x != root_y:
if self.rank[root_x] < self.rank[root_y]:
self.parent[root_x] = root_y
elif self.rank[root_y] < self.rank[root_x]:
self.parent[root_y] = root_x
else:
self.parent[root_y] = root_x
self.rank[root_x] += 1
def find_suspects(n, groups):
uf = UnionFind(n)
for group in groups:
for student in group[1:]:
uf.union(group[0], student) # Union the first student in the group with all others
suspect_set = set()
for i in range(n):
if uf.find(0) == uf.find(i): # If student is in the same set as the initial suspect
suspect_set.add(i)
return len(suspect_set)
def main():
while True:
n, m = map(int, input().split())
if n == 0 and m == 0:
break
groups = [list(map(int, input().split()))[1:] for _ in range(m)]
print(find_suspects(n, groups))
if __name__ == "__main__":
main()#2200015507 王一粟
def find(x):
if parent[x] == x:
return parent[x]
else:
parent[x] = find(parent[x])
return parent[x]
def disjoint(x,y):
rep_x,rep_y = find(x),find(y)
if rep_x != rep_y:
if rank[rep_x] < rank[rep_y]:
parent[rep_x] = rep_y
elif rank[rep_x] > rank[rep_y]:
parent[rep_y] = rep_x
else:
parent[rep_y] = rep_x
rank[rep_x] += 1
def joint(mylist):
node = mylist[0]
for element in mylist[1:]:
disjoint(node,element)
while True:
n,m = [int(i) for i in input().split()]
if n == 0 and m == 0:
break
parent = [i for i in range(n)]
rank = [0 for i in range(n)]
for i in range(m):
s = [int(i) for i in input().split()]
joint(s[1:])
rep_0 = find(0)
print(len([i for i in parent if find(i) == rep_0]))