# Student

**Topics:**Disjoint-set data structure, Natural number, Set

**Pages:**5 (1224 words)

**Published:**September 14, 2014

BÀI 11 (NC)

UNION-FIND DISJOINT SETS

THEORY

In computing, a disjoint-set data structure, also called a union–find data structure or merge–find set, is a data structure that keeps track of a set of elements partitioned into a number of disjoint (nonoverlapping) subsets. It supports two useful operations: o Find: Determine which subset a particular element is in. Find typically returns an item from this set that serves as its "representative"; by comparing the result of two Find operations, one can determine whether two elements are in the same subset.

o Union: Join two subsets into a single subset.

The other important operation, MakeSet, which makes a set containing only a given element (a singleton), is generally trivial. With these three operations, many practical partitioning problems can be solved (see the Applications section).

In order to define these operations more precisely, some way of representing the sets is needed. One common approach is to select a fixed element of each set, called its representative, to represent the set as a whole. Then, Find(x) returns the representative of the set that x belongs to, and Union takes two set representatives as its arguments.

Figure 1 shows a union-find disjoint sets:

Minh họa: http://www.comp.nus.edu.sg/~stevenha/visualization/ufds.html Tài liệu tham khảo: Sách Steven Haslim Phần Union-Find Disjoint Sets Vấn đề: Sinh viên hãy tham khảo tài liệu của Robert Segwick và Steven Haslim để cài đặt cấu trúc dữ liệu Union-Find Disjoint Sets

Figure 2 shows a Disjoint-sets forest with "union by rank" and "pass compression" heuristics:

GVHD: ĐỖ NHƯ TÀI

Trang 1 / 5

OLYMPIC TIN HỌC HUFLIT 2014

BÀI 11 (NC)

http://just-developer.livejournal.com/97353.html

GVHD: ĐỖ NHƯ TÀI

Trang 2 / 5

OLYMPIC TIN HỌC HUFLIT 2014

BÀI 11 (NC)

PHẦN 2. BÀI TẬP ỨNG DỤNG

EXERCISE 1. UVa 00793 - NETWORK CONNECTIONS

Bob, who is a network administrator, supervises a network of computers. He is keeping a log connections between the computers in the network. Each connection is bi–directional. Two computers are interconnected if they are directly connected or if they are interconnected with the same computer. Occasionally, Bob has to decide, quickly, whether two given computers are connected, directly or indirectly, according to the log information.

Write a program which based on information input from a text file counts the number of successful and the number of unsuccessful answers to the questions of the kind: is computeri interconnected with computerj?

Input:

The program reads data from a text file, as follows:

The number of computers in the network (a strictly positive integer);

A list of pairs of the form:

o c computeri computerj, where computeri and computerj are integers from 1 to no_of_computers. A pair of this form shows that computeri and computerj get interconnected.

o q computeri computerj, where computeri and computerj are integers from 1 to no_of_computers. A pair of this form stands for the question: is computeri interconnected with computerj?

Each pair is on a separate line. Pairs can appear in any order, regardless of their type. The log is updated after each pair of type (a) and each pair of type (b) is processed according to the current network configuration.

Output

For example, the input file illustrated in the sample below corresponds to a network of 10 computers and 7 pairs. There are N1 successfully answered questions and N2 unsuccessfully answered questions. The program prints these two numbers to the standard output on the same line, in the order: ‘successful answers, unsuccessful answers’, as shown in the sample output. Sample Input

10

c15

c27

q71

c39

q96

c25

q75

GVHD: ĐỖ NHƯ TÀI

Trang 3 / 5

OLYMPIC TIN HỌC HUFLIT 2014

BÀI 11 (NC)

Sample Input

1,2

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem &problem=734

EXERCISE 2. UVa 10507 –...

Please join StudyMode to read the full document