CodeForces 300B Coach(并查集 + 水题)
2015-08-13 01:02
761 查看
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,
![](http://codeforces.com/predownloaded/fc/f0/fcf044a729cbd2a5255bae95e738cdf97d3b561f.png)
.
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
![](http://codeforces.com/predownloaded/39/56/395604b46067ac410561de35c3c2f4a254bb633d.png)
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 test(s)
input
output
input
output
input
output
这道题,我做了很久接近一个半小时,无他,题目都看不懂意思,现在深刻体会到了英语的重要性,还望大家努力学习英语,否则一道简单的题目,你丫的都要看上一个多小时才明白,别人早做完了,加强英语是必要的
至于题目思路,只要你看懂题目了,这道题就是代码长了点,其他的你都懂
![](http://img.blog.csdn.net/20150813010228148?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
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,
![](http://codeforces.com/predownloaded/fc/f0/fcf044a729cbd2a5255bae95e738cdf97d3b561f.png)
.
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
![](http://codeforces.com/predownloaded/39/56/395604b46067ac410561de35c3c2f4a254bb633d.png)
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 test(s)
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
这道题,我做了很久接近一个半小时,无他,题目都看不懂意思,现在深刻体会到了英语的重要性,还望大家努力学习英语,否则一道简单的题目,你丫的都要看上一个多小时才明白,别人早做完了,加强英语是必要的
至于题目思路,只要你看懂题目了,这道题就是代码长了点,其他的你都懂
#include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <queue> using namespace std; const int MAXN = 50 * (50 - 1) / 2; int n, m,par[MAXN],sum[MAXN]; bool vis[MAXN]; void init() { for(int i = 1; i <= n; i ++) { par[i] = i; sum[i] = 1; } } int find(int x) { return par[x] == x ? x : par[x] = find(par[x]); } void unite(int x,int y) { x = find(x); y = find(y); if(x != y) { if(x > y) { par[y] = x; sum[x] += sum[y]; } else { par[x] = y; sum[y] += sum[x]; } } } int main() { int a,b; scanf("%d%d", &n, &m); init(); for(int i = 0; i < m; i ++) { scanf("%d%d",&a,&b); unite(a,b); } int x = 0,y = 0; for(int i = 1; i <= n; i ++) { if(find(i) == i) { if(sum[i] > 3) { printf("-1\n"); return 0; } else if(sum[i] == 2) x ++; else if(sum[i] == 1) y ++; } } if(x > y) { printf("-1\n"); return 0; } for(int i = 1; i <= n; i ++) { if(sum[i] == 3) { vis[i] = true; printf("%d",i); for(int j = 1; j <= n; j++) { if(find(j) == i && !vis[j]) { vis[j] = true; printf(" %d",j); } } printf("\n"); } } for(int i = 1; i <= n; i ++) { if(!vis[i] && sum[i] == 2) { vis[i] = true; printf("%d",i); for(int j = 1; j <= n; j ++) { if(find(j) == i && !vis[j]) { vis[j] = true; printf(" %d",j); break; } } for(int j = 1; j <= n; j ++) { if(find(j) == j && sum[j] == 1 && !vis[j]) { vis[j] = true; printf(" %d\n",j); break; } } } } int cnt = 0; for(int i = 1; i <= n; i ++) { if(find(i) == i && sum[i] == 1 && !vis[i]) { if(cnt != 0) printf(" "); printf("%d",i); cnt ++; if(cnt >=3) { cnt = 0; printf("\n"); } } } return 0; }
相关文章推荐
- 算法题:查验省份证
- Hdu 4272 LianLianKan(贪心+链表)
- 从工厂模式到依赖注入的逻辑演变
- android中自定义进度条样式(ProgressDialog)
- Unity3D之AssetBundle【7】共享资源打包/依赖资源打包
- Direct-X学习笔记--公告板
- CodeForces 300A Array(水题)
- POJ 3928 & hdu 2492 & Uva1428 PingPong 【树状数组】
- 数据库事务隔离级别与锁
- 函数(方法)
- Unity3D之AssetBundle【6】解包场景
- 什么是堆 什么是栈 它们在哪里
- 习惯了黑夜作伴
- 用JavaScript实现PHP的urlencode与urldecode函数
- 第一篇博文
- javascript fundamental concept
- Unity3D之AssetBundle【5】打包场景
- CSS3学习笔记<1>
- LeetCode——Lowest Common Ancestor of a Binary Search Tree
- STM32固件库详解