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. VB6.0编程笔记——(1)篇外篇&目录

    从计算机专业毕业到进入IT行业,说来也有些年头了.相比较而言算是幸运,也有很多的同学进入了其他行业,也有一些朋友又想进入这个行业.现在回想自己的一路历程,总结一下,也是一份记忆. 基于以上的原因,希望 ...

  2. C.xml

    pre{ line-height:1; color:#1e1e1e; background-color:#f0f0f0; font-size:16px;}.sysFunc{color:#627cf6; ...

  3. 前端面试题(JS篇)

    原题地址:http://handyxuefeng.blog.163.com/blog/static/454521722013111714040259/ 好吧,最近打算换工作,所以关注比较多的是面试题, ...

  4. 33条C#、.Net经典面试题目及答案

    33条C#..Net经典面试题目及答案[zt] 本文集中了多条常见的C#..Net经典面试题目例如".NET中类和结构的区别"."ASP.NET页面之间传递值的几种方式? ...

  5. CentOS下安装R

    R的Windows版本有直接的安装包,直接下载安装很方便,但是对于CentOS6以上,不能直接通过yum 安装R,需要自己编译. 1. 在编译之前,用yum安装各种软件 (1)安装gcc > y ...

  6. JavaScript操作DOM的那些坑

    js在操作DOM中存在着许多跨浏览器方面的坑,本文花了我将近一周的时间整理,我将根据实例整理那些大大小小的“坑”. DOM的工作模式是:先加载文档的静态内容.再以动态方式对它们进行刷新,动态刷新不影响 ...

  7. 指向函数的指针数组(C++)

    我们能够创建一个指向函数的指针数组.为了选择一个函数,只需要使用数组的下标,然后间接引用这个指针.这种方式支持表格式驱动码的概念:可以根据状态变量去选择被执行函数,而不用条件语句或case语句.这种设 ...

  8. webservice注释

    @WebService 1.serviceName: 对外发布的服务名,指定 Web Service 的服务名称:wsdl:service.缺省值为 Java 类的简单名称 + Service.(字符 ...

  9. Android签名用keytool和jarsigner制作apk文件

    生成证书 keytool -genkey -alias aeo_android.keystore -keyalg RSA -validity -keystore aeo_android.keystor ...

  10. C++11显式虚函数重载

    [C++11显式虚函数重载] 在子类中给重载的虚函数加上override, 可以让编译器检察基类是否有这一虚函数.此功能适用于当基类原有的虚函数发生变化,即相当于编译期检察. 而基类,可以给函数加上f ...