For the first time, see this and check the set ~~ I am speechless ~~ Pay attention to many details
Question Link
The question is like this. n people (can be divided into three groups) are then grouped according to the requirements of the team members, and those who have requirements and those who like them must be in the same group, A group can contain a maximum of three people. If you do not have any requirement, You can group the group. Each group can contain a maximum of three people.
And check the set to determine the number of each group, and then I am entangled in the need to merge two and one group, then, combine the three persons in one group ~~~
I should take a look at the problem-solving report. I wrote too many notes, and I wrote it after an hour and a half. I also read the answer to the example ~~ Speechless ~~
# Include <stdio. h> # include <string. h >#include <iostream >#include <vector> using namespace std; struct node {int father, sum;} peo [50]; int num [50]; // determine the number of headers grouped by int n, m; vector <int> first, second, third; void init () {// initialize first. clear (); second. clear (); third. clear (); for (int I = 1; I <= n; I ++) {peo [I]. father = I; peo [I]. sum = 1; num [I] = 1 ;}} int find_father (int I) {// query the function of the parent node if (I = peo [I]. father) Return I; peo [I]. father = find_father (peo [I]. father); return peo [I]. father;} void Union (int a, int B) {// merging operation int fa = find_father (a); int fb = find_father (B); if (fa! = Fb) {peo [fa]. father = fb; peo [fb]. sum + = peo [fa]. sum; num [fa] = 0; num [fb] = peo [fb]. sum ;}} int main () {while (scanf ("% d", & n, & m )! = EOF) {init (); int stu_one, stu_two; for (int I = 1; I <= m; I ++) {scanf ("% d ", & stu_one, & stu_two); Union (stu_one, stu_two);} int flag = 1; for (int I = 1; I <= n; I ++) {if (num [I]> 3) {flag = 0; break;} if (num [I] = 1) first. push_back (I); // record the parent node of a group of 1 people if (num [I] = 2) second. push_back (I); // record the parent node of a group of 2 people if (num [I] = 3) third. push_back (I); // record the parent nodes of a group of three people} if (flag = 0) {printf ("-1 \ n"); continue ;} // if the number of people in each group is greater than 3, if (second. size ()> first. size () {printf ("-1 \ n"); continue ;} // if a group of 2 members and a group of more than 1 Members are not true, *** this error occurs once for (int I = 0; I <third. size (); I ++) {// output the case where a group of three members is present, int ans = third [I], sum = 0; for (int j = 1; j <= n; j ++) {if (ans = find_father (j) {sum ++; if (sum = 1) printf ("% d ", j); else printf ("% d", j); if (sum = 3) printf ("\ n") ;}}for (int I = 0; I <second. size (); I ++) {// combines int sum = I; int ji = 0; int ans for a group of two people in a group; while (sum <1 + I) {ji ++; ans = second [sum]; for (int j = 1; j <= n; j ++) {if (ans = find_father (j) {if (ji = 1) {printf ("% d", j); ji ++ ;} else printf ("% d", j) ;}} sum ++;} ans = first [0]; // ****** the second error, each time you delete the first node for (int j = 1; j <= n; j ++) {if (ans = find_father (j )) {printf ("% d \ n", j) ;}} first. erase (first. begin () ;}for (int I = 0; I <first. size ()/3; I ++) {// int sum = I * 3; int ji = 0 for a group of three people; while (sum <(3 + I * 3) {ji ++; int ans = first [sum]; for (int j = 1; j <= n; j ++) {if (ans = find_father (j) {if (ji = 1) printf ("% d", j); if (ji = 2) printf ("% d", j); if (ji = 3) printf ("% d \ n", j) ;}} sum ++ ;}}} return 0 ;}