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 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 ;
}
Coach(并查集)的更多相关文章
- Codeforces Round #181 (Div. 2) B. Coach 带权并查集
B. Coach 题目连接: http://www.codeforces.com/contest/300/problem/A Description A programming coach has n ...
- BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]
4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...
- 关押罪犯 and 食物链(并查集)
题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用"怨气值"( ...
- 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用
图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...
- bzoj1854--并查集
这题有一种神奇的并查集做法. 将每种属性作为一个点,每种装备作为一条边,则可以得到如下结论: 1.如果一个有n个点的连通块有n-1条边,则我们可以满足这个连通块的n-1个点. 2.如果一个有n个点的连 ...
- [bzoj3673][可持久化并查集 by zky] (rope(可持久化数组)+并查集=可持久化并查集)
Description n个集合 m个操作 操作: 1 a b 合并a,b所在集合 2 k 回到第k次操作之后的状态(查询算作操作) 3 a b 询问a,b是否属于同一集合,是则输出1否则输出0 0& ...
- [bzoj3123][sdoi2013森林] (树上主席树+lca+并查集启发式合并+暴力重构森林)
Description Input 第一行包含一个正整数testcase,表示当前测试数据的测试点编号.保证1≤testcase≤20. 第二行包含三个整数N,M,T,分别表示节点数.初始边数.操作数 ...
- 【BZOJ-3673&3674】可持久化并查集 可持久化线段树 + 并查集
3673: 可持久化并查集 by zky Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 1878 Solved: 846[Submit][Status ...
- Codeforces 731C Socks 并查集
题目:http://codeforces.com/contest/731/problem/C 思路:并查集处理出哪几堆袜子是同一颜色的,对于每堆袜子求出出现最多颜色的次数,用这堆袜子的数目减去该值即为 ...
随机推荐
- codeforces 580D Kefa and Dishes(状压dp)
题意:给定n个菜,每个菜都有一个价值,给定k个规则,每个规则描述吃菜的顺序:i j w,按照先吃i接着吃j,可以多增加w的价值.问如果吃m个菜,最大价值是多大.其中n<=18 思路:一看n这么小 ...
- Unity3D中读取CSV文件
直接上代码 Part1: using UnityEngine; using System.IO; using System.Collections.Generic; public class CSV ...
- 20、CSS
CSS 层叠样式表(Cascading Style Sheets). 用于定义显示HTML样式. DIV和SPAN div是块级元素. span是行级元素. 将一些页面中的内容包裹起来统一设置样式. ...
- java: Eclipse jsp tomcat 环境搭建(完整)
] 欢迎您! 要学习一门语言,首先要做的就是搭建环境,然后能写一个小的Demo(类似Helloworld),不仅可以建立信心,而且还可以为之后的学习搭建一个验证平台,事半功倍. net领域的vs,号称 ...
- Java获取项目路径
参考博客.自己就不写了.我觉得他写得很详细 http://blog.csdn.net/hpf911/article/details/5852127
- hibernate使用sql语句查询实体时,要写上addEntity
abDAO.getSession().createSQLQuery(hql).addEntity(对象.class).list(); 参考http://blog.csdn.net/vacblog/ar ...
- 深入理解shared pool共享池之library cache的library cache pin系列三
关于library cache相关的LATCH非常多,名称差不多,我相信一些人对这些概念还是有些晕,我之前也有些晕,希望此文可以对这些概念有个更为清晰的理解,本文主要学习library cache p ...
- Android简单例子——IpHone样式AlertDialog
此例子源于网络,下载下来之后,自己加了写注释,作为总结,发到博客中,谢谢原作者 通过这个例子学到的东西 1.自定义对话框的使用 2.程序中使用颜色如何进行存放,增加复用性 3.加深线性布局.常用控件的 ...
- UITextField,常见属性的罗列和用法
UITextField是UIControl的子类 ,属于控件类(因为它有能力响应一些高级事件),在故事版中可以直接拖拽过来使用. 首先定义 UITextField *name; name = [[UI ...
- 使用methodSignatureForSelector与forwardInvocation实现消息转发 (转)
转自:http://blog.sina.com.cn/s/blog_8c87ba3b0102v006.html 在给程序添加消息转发功能以前,必须覆盖两个方法,即methodSignatureForS ...