题目地址

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, bioccurs 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.

Example
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个人和m组关系(意思就是这两个人必须在一组),现在问你可不可以在满足这m组关系的情况下将他们分成n/3组(题目保证n可以被3整除)

解法:一共有两种,本人是第二种,第一种是网上大佬的。
解法一:并查集(真是一个好东西)
用到并查集。关于并查集的博客,我看了几个,觉得最通俗易懂的还是这个博客:并查集-简单易懂。并查集确定每组人的个数和代表。然后遍历,如果出现个数超过三个的组,就直接输出-1,都小于等于3的话,接下来考虑2个的组个数是否大于一个的,因为2个人的组肯定要和一个人的组搭档,超出的话就肯定不满足条件了,剩下的情况就都是满足条件的。

AC代码现场奉上:

#include<stdio.h>
//rang[i] 以i为代表的组人的个数 flag[i]表示第i个是否被输出过 pre[i]第i个的代表
int pre[100], rank[100],flag[100];
void unite(int x, int y);
int find(int x);
int main()
{int n, m,i,a,b,j,s=0;scanf("%d%d", &n, &m);for (i = 1; i <= n; ++i){pre[i] = i;rank[i] = 1;flag[i] = 1;}for (i = 0; i < m; ++i){scanf("%d%d", &a, &b);unite(a, b);}int x = 0, y = 0;for (i = 1; i <= n; ++i){if (find(i) == i){if (rank[i] > 3) {printf("-1\n");return 0;}else if (rank[i] == 2)x++;else if (rank[i] == 1)y++;}}if (x > y) {printf("-1\n");return 0;}for (i = 1; i <= n; i++){if (rank[i] == 3) {printf("%d ", i);s++;flag[i] = 0;for (j = 1; j <= n; j++){if (find(j) == i&&flag[j]){s++;if(s!=3)printf("%d ", j);else printf("%d\n", j);flag[j] = 0;}}}}for (i = 1; i <= n; i++){if (rank[i] == 2){printf("%d ", i);flag[i] = 0;for (j = 1; j <= n; j++){if (find(j) == i&&flag[j]){printf("%d ", j);flag[j] = 0;}}for(j=1;i<=n;j++)if (find(j) == j&&rank[j]==1&&flag[j]){printf("%d\n", j);flag[j] = 0;break;}}}j = 0;for (i = 1; i <= n; ++i){if (find(i) == i&&rank[i] == 1 && flag[i]){if (j != 2){printf("%d ", i);j++;}else{j = 0;printf("%d\n", i);}}}return 0;
}
//合并函数  两组合并 人数少的合并到多的 成为人数多的子树
void unite(int x, int y)
{int fx = find(x);int fy = find(y);if (fx != fy){if (rank[fx] >=rank[fy]){pre[fy] = fx;rank[fx] += rank[fy];}else{pre[fx] = fy;rank[fy] += rank[fx];}}
}
//查找函数
int find(int x)
{if (pre[x] != x)pre[x] = find(pre[x]);  //压缩路径 一个组内的直接指向代表return pre[x];
}

解法二:(我也不知道是什么算法,也许乱搞出了奇迹?! )
我的思路是

1:先选出可以3个人相互组队的(就是三个人相互喜欢的),存在结构体out中;

2:然后选出只有两个人相互喜欢的,然后把那些没有提及想和谁一队的人看成是和谁组队都行的人。再对他们进行组队。

3:然后剩下和谁组队都行的人(就是2中我们标注的),对他们进行组队。

依次进行玩以上的步骤后,发现AC不了,其实这就是这道题的坑点,如果存在一个人想和两个以上的人组队,那么直接输出-1。

注释:这个不是正解,纯粹乱搞,可能是数据比较水才AC

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<set>
using namespace std;
int vis[50];
int g[50][50];
int used[50];
set<int> pp;
struct node{int x;int y;int z;
};
node out[50];
int cishu[50];
int main()
{int n,m;while(scanf("%d%d",&n,&m) != EOF){memset(vis, 0,sizeof(vis));memset(g   , 0,sizeof(g));memset(used, 0,sizeof(used));memset(cishu, 0,sizeof(cishu));int a,b;for(int i=1; i<=m; i++){scanf("%d %d",&a,&b);vis[a] = vis[b] = 1;cishu[a]++;cishu[b]++;g[a][b] = g[b][a]=1;}int flag = 1;for(int i=1; i<=n; i++){if(cishu[i] > 2){flag=0;break;}}if(flag==0){printf("-1\n");continue;}int w = 0;for(int i=1; i<=n; i++){if(!(used[i]==0&&vis[i]==1))continue;for(int j=i+1; j<=n; j++){if(g[i][j]==0) continue;if(!(used[j]==0&&vis[j]==1))continue;for(int k=j+1; k<=n; k++){if(!(used[k]==0&&vis[k]==1))continue;if(g[i][k]==1&&g[j][k]==1){used[i]=used[j]=used[k]=1;++w;out[w].x=i;out[w].y=j;out[w].z=k;break;}}if(used[j]==1)break;}}if(w<n/3)for(int i=1; i<=n; i++){if(!(vis[i]==1&&used[i]==0))continue;for(int j=1; j<=n; j++){if(i==j)continue;if(!(vis[j]==1&&used[j]==0))continue;if(g[i][j]==0)continue;for(int k=1; k<=n; k++){if(i==j || i==k)continue;if(vis[k]==0&&used[k]==0){used[i]=used[j]=used[k]=1;++w;out[w].x=i;out[w].y=j;out[w].z=k;break;}}}}int ko = 0;if(w<n/3){pp.clear();for(int i=1; i<=n; i++){if(vis[i]==0&&used[i]==0){pp.insert(i);}}ko = pp.size()/3;}if(w+ko<n/3)printf("-1\n");else{for(int i=1; i<=w; i++){printf("%d %d %d\n",out[i].x, out[i].y, out[i].z);}for(set<int>::iterator it=pp.begin(); it!=pp.end(); ++it){cout << *it << " "; ++it;cout << *it << " "; ++it;cout << *it << endl;}}}return 0;
}

ok,就酱。

codeforces 300B切题记录相关推荐

  1. codeforces 1090B切题记录

    写在前面:说实话我做这道题目的时候异常的蒙逼,那个傻逼 大佬出的,TMD是pdf格式就算了,你大爷连提交地方或者测试数据都没有,怎么判断程序是否正确啊!!!所以如果有大佬拥有测试数据或者提交方法请在下 ...

  2. 【Python爬虫实战】codeforces刷题记录小助手

    先看效果图. 输入codeforces的用户名,可以查询用户的rating信息.以及参加比赛的信息(大星参数的不计算在内).还有总的AC数. 一.需求分析 找到显示用户参加contest信息的url. ...

  3. Codeforces补题记录(1)

    文章目录 Codeforces补题记录(1) 1.Codeforces Round #632 (Div. 2)(2020.4.11) A.Little Artem B.Kind Anton *C.Eu ...

  4. Codeforces 刷题记录(已停更)

    Codeforces 每日刷题记录 (已停更) 打'+'是一些有启发意义的题目,部分附上一句话题解,每日更新3题,大部分题目较水. Day ID Problem Tutorial Note 1 1 + ...

  5. 【做题记录】Codeforces做题记录

    最近决定写一些CF Div.1的题,练习一下速度和代码能力. 暂定从中考后的Codeforces Round #572开始. 大部分比较简单的题直接把题解写在这里,不单独开文章了. Codeforce ...

  6. 信息学切题记录:永远的A+B Problem(洛谷P1001)

    作为一个CF灰名(丢脸啊,打比赛从绿名掉到了这个层次)的蒟蒻,我决定发篇文章记录一下我在洛谷上的第一道题目(A+B Problem),这道题,正常人一眼解决,但是为了凸显自己的大佬 蒟蒻身份,我要装逼 ...

  7. Codeforces 刷水记录

    Codeforces-566F 题目大意:给出一个有序数列a,这个数列中每两个数,如果满足一个数能整除另一个数,则这两个数中间是有一条边的,现在有这样的图,求最大联通子图. 题解:并不需要把图搞出来, ...

  8. Codeforces 补题记录

    首先总结一下前段时间遇到过的一些有意思的题. Round #474 (Div. 1 + Div. 2, combined)   Problem G 其实关键就是n这个数在排列中的位置. 这样对于一个排 ...

  9. codeforces做题 记录

    1033 G 题意是 给 n堆 石子 Alice 和 Bob 游戏 Alice 每次可以在一堆中取出a枚石子,Bob可以在一堆中取出b枚石子,求对于a 属于 [1,m] b 属于[1,m] 有多少对 ...

最新文章

  1. 让你的 conda “回滚”到以前版本的环境
  2. linux下查看已经安装的jdk 并卸载jdk
  3. 拷贝构造函数的调用以及浅拷贝与深拷贝的理解
  4. NB-IOT的优势体现在哪些方面
  5. 只有想不到,「99」种扩展Jupyter功能的好方法
  6. c语言指针用法有哪些
  7. sql怎么撤回update_零基础快速自学SQL,2天足矣!
  8. 【转】转贴 poj分类
  9. Linux命令——lsb_release
  10. 基金前端代码和后端代码的区别 基金后端代码和基金前端代码区别
  11. CentOS 7.6 vi编辑器常用命令详解
  12. c语言中内存分配方式
  13. Hadoop集群搭建(超级详细)
  14. vue第四天笔记02——axios请求
  15. [教程]教你如何制作彩色的3D打印Groot
  16. 教资科目二重点简答题总结
  17. 标准柯西分布_柯西分布的随机数
  18. mvn上传pom/jar至Nexus私服
  19. python散点图图例显示标记点类型_python – matplotlib散点图中的标记点
  20. VMware Workstation pro无法在Windows上运行,检查可在Windows上运行的此应用的更新版本(无需卸载原先版本或原先版本卸载的按钮变成灰色)

热门文章

  1. 锂电池参数含义与选择
  2. 计算机组成.机器需要控制.控制器CU
  3. DS1302基本的读写
  4. 201771010101 白玛次仁
  5. 低版本浏览器(chrome小于40 firefox小于50 ie小于9)会提示升级信息
  6. 中国医科大学2021年12月《五官科护理学》作业考核试题
  7. MySQL02--高级(BTreeB+Tree、聚簇索引非聚簇索引、性能分析(Explain)、索引、sql优化)
  8. dos命令批量修改图片名称
  9. 哈工大人工智能暑期课实践项目——手写体识别四则运算
  10. 大数据分析步骤及分析方法详解