Codeforces Round #181 (Div. 2) B. Coach 带权并查集
来源:互联网 发布:5g网络手机 编辑:程序博客网 时间:2024/05/02 02:52
题目链接:见这里
B. Coach
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
Examples
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个条边。你需要分成n/3个组,每个组必须3个人,连在一起的点,必须分在同一个组。输出方案,没有输出-1。
解法:带权并查集。相连的时候,维护一下这个集合里面点的个数就好了。如果不够的话,就看看自由的点能不能插进去
//CF 300 B#include <bits/stdc++.h>using namespace std;const int maxn = 55;vector <int> temp;int n, m;namespace dsu{ int fa[maxn], num[maxn], vis[maxn]; vector <int> groub[maxn]; inline void init(){for(int i = 1; i <= n; i++) fa[i] = i, num[i] = 1, groub[i].push_back(i);} inline int find_set(int x){if(x == fa[x]) return x; return fa[x] = find_set(fa[x]);} inline void union_set(int x, int y){int fx = find_set(x), fy = find_set(y); if(fx == fy) return ; fa[fx] = fy; num[fy] += num[fx]; num[fx] = 0; vis[x] = vis[y] = 1; for(int i = 0; i < groub[fx].size(); i++) groub[fy].push_back(groub[fx][i]); groub[fx].clear();}} using namespace dsu;int main(){ scanf("%d%d", &n, &m); init(); for(int i = 1; i <= m; i++){ int x, y; scanf("%d%d", &x, &y); union_set(x, y); } for(int i = 1; i <= n; i++){ if(num[find_set(i)] > 3){ puts("-1"); return 0; } } int tot = 0; for(int i = 1; i <= n; i++){ if(!vis[i]){ temp.push_back(i); groub[i].clear(); } } for(int i = 1; i <= n; i++){ if(groub[i].size() == 0) continue; if(groub[i].size() == 2){ if(tot == temp.size()){ puts("-1"); return 0; } groub[i].push_back(temp[tot++]); } } for(int i = 1; i <= n; i++){ if(groub[i].size() == 3){ for(int j = 0; j < 3; j++){ printf("%d ", groub[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 #181 (Div. 2) B. Coach
- Codeforces 300B Coach 【并查集】
- Codeforces Round #396 (Div. 2)D. Mahmoud and a Dictionary(带权并查集)
- Codeforces Round #396 (Div. 2)D. Mahmoud and a Dictionary【带权并查集】
- Codeforces Round #396 (Div. 2)D. Mahmoud and a Dictionary(带权并查集)
- codeforces 181.div2 300B - Coach 并查集
- CodeForces 300B Coach(并查集 + 水题)
- Codeforces Round #250 (Div. 1) B 并查集
- Codeforces 396 div.2 带权并查集
- Codeforces Round #133 (Div. 2) B. Forming Teams(并查集)
- Codeforces Round #250 (Div. 2)(B,C,D并查集)
- Codeforces Round #254 (Div. 2) 445B - DZY Loves Chemistry (并查集)
- Codeforces Round #290 (Div. 2)B - Fox And Two Dots——并查集/搜索
- Codeforces Round #254 (Div. 2)(并查集,线段树)
- Codeforces Round #218 (Div. 2)---D. Vessels(并查集)
- Codeforces Round #346 (Div. 2) F bfs+并查集
- Codeforces Round #376 (Div. 2) C 并查集
- 关于android RTP驱动的问题
- Crunch字典生成器的使用
- C/C++中的char*,const char*,string之间转换
- 人工智能—状态空间法(state space)[二]
- error: linker command failed with exit code 1 (use -v to see invocation)
- Codeforces Round #181 (Div. 2) B. Coach 带权并查集
- oracle11G空表未导出
- 417. Pacific Atlantic Water Flow
- EF 配置Oracle数据库 EntityType“PersonInfoTest”未定义键。请为该 EntityType 定义键
- SVM中的正则化和损失是什么?
- js 调用qq聊天窗口
- 微信红包发放
- Faster RCNN代码理解(Python) ---训练过程
- 内核的早期页表