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,说明分组不成功。具体看代码。
 #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 ;
}

Coach(并查集)的更多相关文章

  1. Codeforces Round #181 (Div. 2) B. Coach 带权并查集

    B. Coach 题目连接: http://www.codeforces.com/contest/300/problem/A Description A programming coach has n ...

  2. BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]

    4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...

  3. 关押罪犯 and 食物链(并查集)

    题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用"怨气值"( ...

  4. 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用

    图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...

  5. bzoj1854--并查集

    这题有一种神奇的并查集做法. 将每种属性作为一个点,每种装备作为一条边,则可以得到如下结论: 1.如果一个有n个点的连通块有n-1条边,则我们可以满足这个连通块的n-1个点. 2.如果一个有n个点的连 ...

  6. [bzoj3673][可持久化并查集 by zky] (rope(可持久化数组)+并查集=可持久化并查集)

    Description n个集合 m个操作 操作: 1 a b 合并a,b所在集合 2 k 回到第k次操作之后的状态(查询算作操作) 3 a b 询问a,b是否属于同一集合,是则输出1否则输出0 0& ...

  7. [bzoj3123][sdoi2013森林] (树上主席树+lca+并查集启发式合并+暴力重构森林)

    Description Input 第一行包含一个正整数testcase,表示当前测试数据的测试点编号.保证1≤testcase≤20. 第二行包含三个整数N,M,T,分别表示节点数.初始边数.操作数 ...

  8. 【BZOJ-3673&3674】可持久化并查集 可持久化线段树 + 并查集

    3673: 可持久化并查集 by zky Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 1878  Solved: 846[Submit][Status ...

  9. Codeforces 731C Socks 并查集

    题目:http://codeforces.com/contest/731/problem/C 思路:并查集处理出哪几堆袜子是同一颜色的,对于每堆袜子求出出现最多颜色的次数,用这堆袜子的数目减去该值即为 ...

随机推荐

  1. HTML5 Canvas 2D绘图

    为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/ShiJiaqi. http://www.cnblogs.com/shijiaqi1066/p/4851774. ...

  2. Oracle存储过程中不支持DML语言的解决方法(针对遇见的DROP关键字)

    ---存储过程中的原语句: ---删除表 DROP TABLE A_NEWTDDATA; --报错 经查询:存储过程不支持DML语言: 解决方法: execute immediate 'DROP TA ...

  3. Linux只iptables

    1. 查看<strong>网络</strong>监听的端口: netstat -tunlp 2. 查看本机的路由规则: route stack@ubuntu:~$ route ...

  4. 如何在 Debian / Ubuntu 服务器上架设 L2TP / IPSec VPN

    本站的 Rio 最近在一台 Ubuntu 和一台 Debian 主机上配置了 L2TP / IPSec VPN,并在自己的博客上做了记录.原文以英文写就,我把它大致翻译了一下,结合我和 Rio 在设置 ...

  5. CSS jQuery HTML5 CSS3

    jquery css3图片文字介绍鼠标滚动页面动画单页 http://www.17sucai.com/preview/1/2013-12-30/%E5%8A%A8%E7%94%BB%E5%8D%95% ...

  6. 0-C相关01:NSlog函数介绍。

      NSlog()函数介绍: 首先:NSlog()函数是cocoa的框架中提供的一个方法: 下图中最上方是它在Xcode中的路径: : 同样都是输出函数.下边我们来看一下,在O-C中NSlog()和在 ...

  7. 【html】【5】html class属性css样式

    必看参考: http://www.divcss5.com/css3-style/ http://www.jb51.net/css/142448.html http://www.w3school.com ...

  8. Fedora 21 安装桌面环境

    Mate桌面环境:$ sudo yum install @mate-desktop KDE桌面环境:$ sudo yum install @kde-desktop XFCE桌面环境:$ sudo yu ...

  9. 百度地图Api 根据两个坐标点计算距离

    百度地图Android Sdk的Api里面,没有现成的直接获取两个坐标点之间距离的方法,但是,在jsapi里面,有直接计算距离的方法. class Point: pass def max(a,b): ...

  10. 在 Sublime Text 3 中运行 PHP

    参考http://segmentfault.com/blog/tony/1190000000395951 把php添加到环境变量 1.我的电脑->属性->高级系统设置->高级-> ...