(Hdu step 6.3.2) Girls and Boys (for maximum independent set)

Source: Internet
Author: User

Topic:

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-part graph, the maximum independent set, simple problem. Where the maximum independent set = number of nodes-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 two-part diagram: (originally wanted to write, but found that others are finishing more than they are about to write in detail.) So here's a spin on http://blog.csdn.net/pi9nc/article/details/11848327 's content)


two-part diagram: In simple terms, if the midpoint of the diagram can be divided into two groups, and all edges cross the boundary of the group, then this is a two-part graph. To be exact: dividing the vertices of a graph into two disjoint setsUAndV, so that each edge is connected separatelyU、VVertices in the. If such a division exists, 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 any two edges have no public vertices. For example, the red edge in Figure 3, Figure 4, is the match of Figure 2.

We define matching points , matching edges , unmatched points , mismatched edges , and they are very obvious. Example 3, 1, 4, 5, 7 is the matching point, the other vertices are unmatched 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 all matches of a graph, called the maximum match for this graph. Figure 4 is a maximum match that contains 4 matching edges.

Perfect Match : if one of the graphs has a match, 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 (any point of the perfect match has already 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 shown, if there is a connecting edge between a pair of boys and girls, it means 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. If it's a different story: how many boys/girls do you like to pair with each other? This is the maximum matching problem.


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];int link[maxn];int useif[maxn];int n;/* * * To determine if the T node can find a matching node */bool can (int t) {int i;for (i = 0; i < n; ++i) {//Traverse all nodes//If I node has not matched && T node is willing and I node match if (use If[i] = = False && Map[t][i] = = True) {Useif[i] = true;//Then, mark the I node as already matched//if the I node currently does not have a matching node | | I node matching nodes can find other matching nodes if (link[i] = =-1 | | can (LINK[I])) {Link[i] = t;//Then the nodes of the river I node are updated to the T node return true;//return True, Indicates that the T junction can find a matching node}}}return false;//returns False if all nodes above the traversal failed to find a matching node. Indicates that a matching junction 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 (Useif,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);NT C;int j;for (j = 0; J < b; ++j) {scanf ("%d", &c), map[a][c] = true;//means A is willing to match C}}printf ("%d\n", N-max_match ()/2);//Request Maximum number of independent sets}return 0;}









(Hdu step 6.3.2) Girls and Boys (for maximum independent set)

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.