Codeforces Round #181 (Div. 2)

Source: Internet
Author: User

B. Coach

Test instructions: Known to have n students, numbering from 1 to N, where N is a multiple of 3, the teacher needs to divide the N students into groups of 3 people. If the class I student is to be in a group with the J student, then the J student must also want to be in a group with the first student (of course, I or J students may also be a group of other K students). Teachers need to meet the students ' requirements when they are grouped. If the group can be implemented according to the requirements, the number of students will be output; otherwise output-1;

Analysis: First of all need to find out the required students to carry out depth-first traversal, if from a student began to traverse the length of more than 3, according to the requirements of students in a group of more than 3 students, then output-1; If exactly 3, then output the number of the 3 students, if less than 3, the need to fill up the students can Then the output number. Because the student's "requirements" are transitive, an adjacency matrix is used to indicate that a student wants to be in a group with another student. Depth-first traversal, creating an array to indicate whether a student has been visited.

1#include <iostream>2#include <vector>3 using namespacestd;4 BOOLmatrix[ -][ -];5 BOOLvisited[ -];6 intN;7vector<vector<int> >teams;8vector<int>team;9 voidDfsintCurrent )Ten { One Team.push_back (current); AVisited[current] =true; -      for(intj =0; j<n; J + +) -     if(Matrix[current][j] &&!Visited[j]) the Dfs (j); - } - intMain () - { +     intm; -CIN >> N >>m; +memset (Matrix,false,sizeof(matrix)); AMemset (visited,false,sizeof(visited)); at      for(intj =0; j<m; J + +) -     { -         intA, B; -Cin >> a >>b; -Matrix[a-1][b-1] = matrix[b-1][a-1] =true; -     } invector<int>Free ; -      for(intj =0; j<n; J + +) to     { +         BOOLIsfree =true; -          for(intK =0; k<n; k++) the         { *             if(Matrix[j][k]) $Isfree =false;Panax Notoginseng         } -         if(Isfree) the         { +VISITED[J] =true; A Free.push_back (j); the         } +     } -      for(intj =0; j<n; J + +) $     if(!Visited[j]) $     { - team.clear (); - Dfs (j); the         if(Team.size () >3) -         {Wuyicout <<-1; the             return 0; -         } Wu          while(Team.size () <3) -         { About             if(Free.empty ()) $             { -cout <<-1; -                 return 0; -             } A Team.push_back (Free.back ()); + Free.pop_back (); the         } - Teams.push_back (team); $     } the     if(Teams.size () <n/3) the      while(!free.empty ()) the     { the team.clear (); -          for(intj =0; j<3; J + +) in         { the Team.push_back (Free.back ()); the Free.pop_back (); About         } the Teams.push_back (team); the     } the      for(intj =0; J<teams.size (); J + +) +     { -          for(intK =0; k<3; k++) thecout << Teams[j][k] +1<<" ";Bayicout <<Endl; the     } the     return 0; -}
View Code

Codeforces Round #181 (Div. 2)

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.