B. Coach

题目连接:

http://www.codeforces.com/contest/300/problem/A

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

Sample Output

3 2 1

Hint

题意

有n个点,然后有m个条边。

你需要分成n/3个组,每个组必须3个人,连在一起的点,必须分在同一个组

输出方案,没有输出-1

题解:

带权并查集

相连的时候,维护一下这个集合里面点的个数就好了

如果不够的话,就看看自由的点能不能插进去

讨论一下就好了(雾

代码

#include<bits/stdc++.h>
using namespace std; int fa[55];
int num[55];
int vis[55];
vector<int> group[55];
vector<int> temp;
int fi(int x)
{
return x==fa[x]?x:fa[x]=fi(fa[x]);
}
void uni(int x,int y)
{
int p=fa[x],q=fa[y];
if(p==q)return;
fa[q]=p;
num[p]+=num[q];
num[q]=0;
vis[x]=1,vis[y]=1;
for(int i=0;i<group[q].size();i++)
group[p].push_back(group[q][i]);
group[q].clear();
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
fa[i]=i,num[i]=1,group[i].push_back(i);
for(int i=1;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
uni(x,y);
}
for(int i=1;i<=n;i++)
{
if(num[fi(i)]>3)
return puts("-1");
}
int tot = 0;
for(int i=1;i<=n;i++)
if(!vis[i])temp.push_back(i),group[i].clear();
for(int i=1;i<=n;i++)
{
if(group[i].size()==0)continue;
if(group[i].size()==2)
{
if(tot==temp.size())return puts("-1");
group[i].push_back(temp[tot++]);
}
}
for(int i=1;i<=n;i++)
{
if(group[i].size()==3)
{
for(int j=0;j<3;j++)
printf("%d ",group[i][j]);
printf("\n");
}
}
for(int i=tot;i<temp.size();i+=3)
printf("%d %d %d\n",temp[i],temp[i+1],temp[i+2]);
}

Codeforces Round #181 (Div. 2) B. Coach 带权并查集的更多相关文章

  1. Codeforces Round #345 (Div. 1) C. Table Compression dp+并查集

    题目链接: http://codeforces.com/problemset/problem/650/C C. Table Compression time limit per test4 secon ...

  2. Codeforces Round #346 (Div. 2) F. Polycarp and Hay 并查集 bfs

    F. Polycarp and Hay 题目连接: http://www.codeforces.com/contest/659/problem/F Description The farmer Pol ...

  3. Codeforces Round #375 (Div. 2) D. Lakes in Berland 并查集

    http://codeforces.com/contest/723/problem/D 这题是只能把小河填了,题目那里有写,其实如果读懂题这题是挺简单的,预处理出每一块的大小,排好序,从小到大填就行了 ...

  4. Codeforces Round #363 (Div. 2) D. Fix a Tree —— 并查集

    题目链接:http://codeforces.com/contest/699/problem/D D. Fix a Tree time limit per test 2 seconds memory ...

  5. Codeforces Round #603 (Div. 2) D. Secret Passwords(并查集)

    链接: https://codeforces.com/contest/1263/problem/D 题意: One unknown hacker wants to get the admin's pa ...

  6. CodeForces - 688C:NP-Hard Problem (二分图&带权并查集)

    Recently, Pari and Arya did some research about NP-Hard problems and they found the minimum vertex c ...

  7. Codeforces Round #360 (Div. 1) D. Dividing Kingdom II 并查集求奇偶元环

    D. Dividing Kingdom II   Long time ago, there was a great kingdom and it was being ruled by The Grea ...

  8. Codeforces Round #346 (Div. 2) F. Polycarp and Hay 并查集

    题目链接: 题目 F. Polycarp and Hay time limit per test: 4 seconds memory limit per test: 512 megabytes inp ...

  9. codeforces Codeforces Round #345 (Div. 1) C. Table Compression 排序+并查集

    C. Table Compression Little Petya is now fond of data compression algorithms. He has already studied ...

随机推荐

  1. TCP经受时延的ACK

    下午看<卷1>的时候,感觉“TCP经受时延ACK”这段看的有些迷糊,最后还算理解了,所以这里记下来以免以后又忘了. 经受时延的ACK就是在接收到数据后不立马确认,而是等到内核的一个定时器到 ...

  2. 链接服务器"(null)"的 OLE DB 访问接口 "Microsoft.Jet.OLEDB.4.0" 返回了消息 "未指定的错误"。[手稿]

    消息 7302,级别 16,状态 1,第 1 行 无法创建链接服务器 "(null)" 的 OLE DB 访问接口 "Microsoft.JET.OLEDB.4.0&qu ...

  3. nagios-解决监控页面上的乱码

    1. 前景 在nagios的监控页面上发现返回来的信息为乱码,如下图所示: 查看相关日志,发现正常显示汉字,如下: 2. 解决方法(以下两个步骤缺一不可) 主要原因分析如下: 在nagios服务器上发 ...

  4. Office2013版的破解之路

    追着潮流,我还是更新了我的所有软件,2013版早就下载了,因为一直破解的问题没有装,这次终于找到必成功的办法. 1.准备工作: 下载office2013的官方版即可,官方版里不包含project和vi ...

  5. POJ 2395 Out of Hay(MST)

    [题目链接]http://poj.org/problem?id=2395 [解题思路]找最小生成树中权值最大的那条边输出,模板过的,出现了几个问题,开的数据不够大导致运行错误,第一次用模板,理解得不够 ...

  6. openstack neutron

  7. C++11之使用或禁用对象的默认函数

    [C++11之使用或禁用对象的默认函数] C++11 允许显式地表明采用或拒用编译器提供的内置函数.例如要求类型带有默认构造函数,可以用以下的语法: 另一方面,也可以禁止编译器自动产生某些函数.如下面 ...

  8. ubuntu 14.04 64位系统编译RT288x_SDK

    sudo apt-get install gcc g++ binutils patch bzip2 flex bison make autoconf gettext texinfo unzip sha ...

  9. STM32F407存储器和总线架构

    http://www.cnblogs.com/ransn/p/5654068.html

  10. USB开发库文件分析

    stm32f10x_it.c: 该文件中包含 USB 中断服务程序,由于 USB 中断有很多情况,这里的中断服务程序只是调用 usb_Istr.c 文件中的 USB_Istr 函数,由 USB_Ist ...