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 ...
随机推荐
- Delphi 注册文件类型 设置文件图标
{------------------------------------------------------------------------------- @过程名: slpert -& ...
- 【转】linux_fdisk命令详解
转自:http://www.cnblogs.com/xiaofengkang/archive/2011/06/06/2073579.html fdisk -l 可以列出所有的分区,包括没有挂上的分区和 ...
- android命名规范
Android 开发规范 (陈杨) (一)注意事项 1. 编码方式统一用UTF-8. Android Studio默认已是UTF-8,只要不去改动它就可以了. 2. 缩进统一为4个空格,将Tab si ...
- ansible安装及问题解决
本文主要用来安装ansible,在进行安装的时候,也可以使用其他的版本进行安装,本文主要讲述安装ansible的步骤,还有常用问题的解决. 1.查看python版本 由此可以看到python的版本为2 ...
- 【LeetCode】88 - Merge Sorted Array
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...
- .NET在IE9中页面间URL传递中文变成乱码的解决办法
在.Net的项目中,鼠标点击查询按钮,转到查询页面,但URL中包含中文时,传到服务器端后,中文变成了乱码(只有IE9出现该问题). 尝试使用Server.UrlEncode()进行编码, ...
- leetcode@ [273] Integer to English Words (String & Math)
https://leetcode.com/problems/integer-to-english-words/ Convert a non-negative integer to its englis ...
- BOX2D测试
; ; Box2DTestLayer = cc.Layer.extend({ world:null, //GLESDebugDraw *m_debugDraw; ctor:function () { ...
- Problems running django-admin
“command not found: django-admin”¶ django-admin should be on your system path if you installed Djang ...
- C# Devexpress学习--LabelControl
A LabelControl can display an image (regular or animated GIF file). Different images can be provided ...