随笔-157  评论-1  文章-0 

Coach(并查集)

Description

A programming coach has n students to teach. We know that n is divisible by 3. Let's assume that all students are numbered from 1 to n, inclusive.

Before the university programming championship the coach wants to split all students into groups of three. For some pairs of students we know that they want to be on the same team. Besides, if the i-th student wants to be on the same team with the j-th one, then the j-th student wants to be on the same team with the i-th one. The coach wants the teams to show good results, so he wants the following condition to hold: if the i-th student wants to be on the same team with the j-th, then the i-th and the j-th students must be on the same team. Also, it is obvious that each student must be on exactly one team.

Help the coach and divide the teams the way he wants.

Input

The first line of the input contains integers n and m(3 ≤ n ≤ 48. Then follow m lines, each contains a pair of integers ai, bi(1 ≤ ai < bi ≤ n) — the pair ai, bi means that students with numbers ai and bi want to be on the same team.

It is guaranteed that n is divisible by 3. It is guaranteed that each pair ai, bi occurs in the input at most once.

Output

If the required division into teams doesn't exist, print number -1. Otherwise, print  lines. In each line print three integers xiyizi (1 ≤ xi, yi, zi ≤ n) — the i-th team.

If there are multiple answers, you are allowed to print any of them.

Sample Input

Input
3 0
Output
3 2 1 
Input
6 4
1 2
2 3
3 4
5 6
Output
-1
Input
3 3
1 2
2 3
1 3
Output
3 2 1 
题意:教练培训n个同学(n%3 = 0),要给他们分组,每3个人一组,要分n/3组,输入的m行中的x、y代表x想和y分一组,教练会根据他们的请求将他们分为一组,若最后分组不成功输出-1,否则一一输出各组同学的序号。

思路:先套并查集模板将可以分成一组的以邻接表的形式存起来,然后再判断,如果与i一组的人数大于3,说明分组不成功。具体看代码。
  1 #include<stdio.h>
  2 #include<string.h>
  3 #include<queue>
  4 using namespace std;
  5 int n,m;
  6 int gp[50][50];//gp[i][j]表示j与i可以分一组
  7 int cnt[50];//cnt[i]表示与i一组的人数
  8 int vis[50];
  9 int set[50];
 10 queue<int>que[4];
 11 int find(int x)
 12 {
 13     if(set[x] != x)
 14         set[x] = find(set[x]);
 15     return set[x];
 16 }
 17 
 18 int main()
 19 {
 20     scanf("%d %d",&n,&m);
 21     int t,flag;
 22     for(int i = 1; i <= n; i++)
 23         set[i] = i;
 24     int u,v;
 25     for(int i = 1; i <= m; i++)
 26     {
 27         scanf("%d %d",&u,&v);
 28         int uu = find(u);
 29         int vv = find(v);
 30         if(uu != vv)
 31             set[uu] = vv;
 32     }
 33     for(int i = 1; i <= n; i++)
 34     {
 35         cnt[i] = 0;
 36         t = find(i);
 37         for(int j = 1; j <= n; j++)
 38         {
 39             if(t == find(j))
 40             {
 41                 gp[i][cnt[i]++] = j;
 42             }
 43         }
 44     }//并查集,将所有可能分一组的存起来
 45     
 46     memset(vis,0,sizeof(vis));
 47     flag = 1;
 48     for(int i = 1; i <= n; i++)
 49     {
 50         if(cnt[i] > 3)//如果与i一组的人数大于3,不合法
 51         {
 52             flag = 0;
 53             continue;
 54         }
 55         if(vis[gp[i][0]]) continue;
 56         
 57         else if(cnt[i] == 1)
 58             que[1].push(i);
 59         else if(cnt[i] == 2)
 60             que[2].push(i);
 61         else if(cnt[i] == 3)
 62             que[3].push(i);
 63 
 64         vis[gp[i][0]] = 1;
 65     }
 66     if(que[1].size() < que[2].size() || (que[1].size()-que[2].size())%3 !=0)
 67         flag = 0;
 68 
 69     if(flag)
 70     {
 71         while(que[2].size() > 0)
 72         {
 73             int sec = que[2].front();
 74             que[2].pop();
 75             printf("%d %d ",gp[sec][0],gp[sec][1]);
 76 
 77             int fir = que[1].front();
 78             que[1].pop();
 79             printf("%d\n",gp[fir][0]);
 80         }
 81 
 82         while(que[1].size() > 0)
 83         {
 84             int fir = que[1].front();
 85             que[1].pop();
 86             printf("%d ",gp[fir][0]);
 87 
 88             fir = que[1].front();
 89             que[1].pop();
 90             printf("%d ",gp[fir][0]);
 91 
 92             fir = que[1].front();
 93             que[1].pop();
 94             printf("%d\n",gp[fir][0]);
 95         }
 96 
 97         while(que[3].size() > 0)
 98         {
 99             int thi= que[3].front();
100             que[3].pop();
101             printf("%d %d %d\n",gp[thi][0],gp[thi][1],gp[thi][2]);
102         }
103     }
104     else printf("-1\n");
105 
106     return 0;
107 }
View Code

 

posted on 2013-11-16 19:53 straw_berry 阅读(...) 评论(...) 编辑 收藏