(Hdu step 6.3.2) Girls and Boys (match left after several people do not match, with adjacency matrix)

Source: Internet
Author: User

Title:

Girls and Boys
Time limit:20000/10000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 189 Accepted Submission (s): 127
Problem DescriptionThe Second year of the University somebody started a study on the romantic relations between the Studen Ts. The relation "romantically involved" is defined between one girl and one boy. For the study reasons it was necessary to find out the the maximum set satisfying the condition:there was no and the students in T He set who has been "romantically involved". The result of the program was the number of students in such a set.

The input contains several data sets in text format. Each data set represents one set of subjects of the study, with the following description:

The number of students
The description of each student, in the following format
Student_identifier: (number_of_romantic_relations) student_identifier1 student_identifier2 student_identifier3 ...
Or
Student_identifier: (0)

The Student_identifier is a integer number between 0 and n-1, for n subjects.
For each given data set, the program should write to standard output a line containing the result.
Output
Sample Input
70: (3) 4 5 61: (2) 4 62: (0) 3: (0) 4: (2) 0 15: (1) 06: (2) 0 130: (2) 1 21: (1) 02: (1) 0
Sample Output
52
Sourcesoutheastern Europe 2000
Recommendjgshining


Topic Analysis:

Two-dimensional graph, to find the maximum independent set. Simple question. The maximum independent set = number of nodes-the maximum number of matches/2. The so-called maximum independent set, in fact, is

There is no match to the collection of points (unmatched points).

Here are some of the basic concepts of the binary diagram: (originally wanted to write their own, but found that others collated than they are about to write to be specific.)

So here's a spin on http://blog.csdn.net/pi9nc/article/details/11848327 's content)


two-part diagram: In simple terms. Suppose that the midpoint of the graph can be divided into two groups, and that all edges cross the boundary of the group, then this is a binary graph. To be exact: dividing the vertices of a graph into two disjoint setsUAndV。 So that each edge is connected separatelyU、VVertices in the. Suppose there is such a division. Then the graph is a two-part graph. An equivalent definition of a binary graph is: A graph that does not contain "rings with odd numbers of edges". Figure 1 is a two-part diagram. In order to be clear, we will later draw it into the form of Figure 2.

match : In graph theory, a "match" (matching) is a set of edges in which there are no public vertices at random on two edges. Like what. Figure 3, the red edge in Figure 4 is the match of Figure 2.

We define matching points , matching edges , unmatched points , mismatched edges , and they are clearly meant to be. Compared to 3 in 1, 4, 5, 7 is the matching point, the other vertices are mismatched points, 1-5, 4-7 is the matching edge. The other edges are non-matching edges.

Maximum match : A match with the largest number of matched edges in a graph in all matches. Called the maximum match for this graph. Figure 4 is a maximum match, which includes 4 matching edges.

Perfect Match : Suppose that a match in a graph, all vertices are matching points, then it is a perfect match. Figure 4 is a perfect match.

Obviously, the perfect match must be the maximum match (the perfect match no matter what point has been matched, adding a new matching edge will certainly conflict with the existing matching edge). But not every diagram has a perfect match.

For example, as you can see, suppose that there is a connecting edge between a pair of boys and girls, it means that they like each other. Is it possible for all boys and girls to be paired 22 so that each pair likes each other? In graph theory. This is the perfect match problem. Suppose, in other words, how many boys/girls can pair with each other like each other? This is the maximum matching problem.


Topic Analysis:

To find the maximum independent set. Maximum Independent set = total number of nodes-maximum number of matches/2

The code is as follows:

/* * b.cpp * * Created on:2015 March 13 * author:administrator * * #include <iostream> #include <cstdio> #inc Lude <cstring>using namespace Std;const int maxn = 1001;int Map[maxn][maxn];//map[i][j]=1, indicating that the first girl is willing to match the J-Boy int Link[maxn];//link[i] = T. Indicates that the first male match is a girl tint useif[maxn];//indicates whether the first boy has matched int n;/** * Inferred T node can find a matching node */bool can (int t) {int I;for (i = 0; i < n; ++i) {//Traverse all nodes//Suppose I node has not matched && T node is willing and I node matches if (Useif[i] = False && map[t][i] = = tr. UE) {Useif[i] = true;//Then, mark the I node as already matched//assuming I node has no matching nodes right now | | I node matching nodes can find other matching nodes if (link[i] = = 1 | | can (LINK[I])) {Link[i ] = t;//The node of the river I node is updated to the T node return true;//returns True, indicating that the T node is able to find a matching node}}}return false;//assume that all nodes above the traversal cannot find a matching node. Returns FALSE. Indicates that a matching node could not be found for t}/** * For maximum number of matches */int Max_match () {int num = 0;int i;for (i = 0; i < n; ++i) {//Traverse all nodes to find the maximum number of matches memset (U Seif,false,sizeof (Useif)); if (can (i) = = true) {num++;}} The maximum number of matches that the return num;//will seek}int main () {while (scanf ("%d", &n)!=eof) {memset (map,false,sizeof (map)); Memset (Link,-1, sizeof (link));int I;int a,b;for (i = 0; i < n; ++i) {scanf ("%d: (%d)", &a,&b), int c;int j;for (j = 0; J < b; ++j) {scanf ("% D ", &c); Map[a][c] = true;//indicates that A is willing and C to match}}printf ("%d\n ", N-max_match ()/2);//seek maximum number of independent sets}return 0;}









Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.

(Hdu step 6.3.2) Girls and Boys (match left after several people do not match, with adjacency matrix)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.