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 xi, yi, zi (1 ≤ xi, yi, zi ≤ n) — the i-th team.
If there are multiple answers, you are allowed to print any of them.
Sample Input
3 0
3 2 1
6 4
1 2
2 3
3 4
5 6
-1
3 3
1 2
2 3
1 3
3 2 1
题意:教练培训n个同学(n%3 = 0),要给他们分组,每3个人一组,要分n/3组,输入的m行中的x、y代表x想和y分一组,教练会根据他们的请求将他们分为一组,若最后分组不成功输出-1,否则一一输出各组同学的序号。 思路:先套并查集模板将可以分成一组的以邻接表的形式存起来,然后再判断,如果与i一组的人数大于3,说明分组不成功。具体看代码。
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int n,m;
int gp[][];//gp[i][j]表示j与i可以分一组
int cnt[];//cnt[i]表示与i一组的人数
int vis[];
int set[];
queue<int>que[];
int find(int x)
{
if(set[x] != x)
set[x] = find(set[x]);
return set[x];
} int main()
{
scanf("%d %d",&n,&m);
int t,flag;
for(int i = ; i <= n; i++)
set[i] = i;
int u,v;
for(int i = ; i <= m; i++)
{
scanf("%d %d",&u,&v);
int uu = find(u);
int vv = find(v);
if(uu != vv)
set[uu] = vv;
}
for(int i = ; i <= n; i++)
{
cnt[i] = ;
t = find(i);
for(int j = ; j <= n; j++)
{
if(t == find(j))
{
gp[i][cnt[i]++] = j;
}
}
}//并查集,将所有可能分一组的存起来 memset(vis,,sizeof(vis));
flag = ;
for(int i = ; i <= n; i++)
{
if(cnt[i] > )//如果与i一组的人数大于3,不合法
{
flag = ;
continue;
}
if(vis[gp[i][]]) continue; else if(cnt[i] == )
que[].push(i);
else if(cnt[i] == )
que[].push(i);
else if(cnt[i] == )
que[].push(i); vis[gp[i][]] = ;
}
if(que[].size() < que[].size() || (que[].size()-que[].size())% !=)
flag = ; if(flag)
{
while(que[].size() > )
{
int sec = que[].front();
que[].pop();
printf("%d %d ",gp[sec][],gp[sec][]); int fir = que[].front();
que[].pop();
printf("%d\n",gp[fir][]);
} while(que[].size() > )
{
int fir = que[].front();
que[].pop();
printf("%d ",gp[fir][]); fir = que[].front();
que[].pop();
printf("%d ",gp[fir][]); fir = que[].front();
que[].pop();
printf("%d\n",gp[fir][]);
} while(que[].size() > )
{
int thi= que[].front();
que[].pop();
printf("%d %d %d\n",gp[thi][],gp[thi][],gp[thi][]);
}
}
else printf("-1\n"); return ;
}