Codeforces Round #181 (Div. 2) B. Coach 带权并查集
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 带权并查集的更多相关文章
- Codeforces Round #345 (Div. 1) C. Table Compression dp+并查集
题目链接: http://codeforces.com/problemset/problem/650/C C. Table Compression time limit per test4 secon ...
- 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 ...
- Codeforces Round #375 (Div. 2) D. Lakes in Berland 并查集
http://codeforces.com/contest/723/problem/D 这题是只能把小河填了,题目那里有写,其实如果读懂题这题是挺简单的,预处理出每一块的大小,排好序,从小到大填就行了 ...
- 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 ...
- 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 ...
- CodeForces - 688C:NP-Hard Problem (二分图&带权并查集)
Recently, Pari and Arya did some research about NP-Hard problems and they found the minimum vertex c ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 通过HttpClient来调用Web Api接口
回到目录 HttpClient是一个被封装好的类,主要用于Http的通讯,它在.net,java,oc中都有被实现,当然,我只会.net,所以,只讲.net中的HttpClient去调用Web Api ...
- sed命令使用记录
背景:文件A,文件B,文件格式一致,有两列,第一列为key,第二列为value. 目的:将文件A中的内容插入到文件B中,不能在最后,不能有重复key(我的key和value用tab键分割) 实现:我的 ...
- LR之错误处理
1.脚本的健壮性 2.VuGen的处理机制 3.lr_continue_on_error函数 4.示例代码
- 将垃圾送入无底洞,顺便整理dev知识
相信用过Linux的童鞋们都用过crontab来做定时任务,不需要额外的安装程序和配置,一条简单的语句搞定定时任务,但是小伙伴们发现了没,如果你的定时任务执行频率很高而且会产生大量的输出的话,你的老爷 ...
- linux 如何让程序在开机时启动,关机前关闭
可以将自己所写的script的文件名写入/etc/rc.d/rc.local(用户自定义开机启动程序) 中,在/etc/rc.d/init.d 中貌似也可以
- scala初学
起因:新公司的程序用scala,为了不落后,不落伍,跟上时代的浪潮,咱们测试也得学学新东西 适合读者:有java经验的IT人士 scala:所有变量都是对象,所有操作都是方法 1.定义变量:变量:类型 ...
- MySQL Connector_J_5.1.34_2014.10
5.1版本符合JDBC3.0和JDBC4.0规范 跟MySQL4.1-5.7兼容 5.1.21以后支持JDK7的JDBC4.1规范 在MySQL4.1之前,是不支持utf8的 com.mysql.jd ...
- struts2类库下载
struts2开发包下载 到http://struts.apache.org/download.cgi#struts2014下载struts-2.x.x-all.zip,目前最新版为2.1.6.下载完 ...
- 在fedora20下配置hadoop2.5.1的eclipse插件
(博客园-番茄酱原创) 在我的系统中,hadoop-2.5.1的安装路径是/opt/lib64/hadoop-2.5.1下面,然后hadoop-2.2.0的路径是/home/hadoop/下载/had ...
- xcode 6.4 安装Alcatraz失败解决方法
Alcatraz Xcode6.4安装不了解决方法http://www.cocoachina.com/bbs/read.php?tid=310380 版权声明:本文为博主原创文章,未经博主允许不得转载 ...