Codeforces 300B Coach (并查集)

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 xiyizi(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 

题目链接: http://codeforces.com/problemset/problem/300/B

题意:给n个人(n一定能被3整除),m行ai,bi,表示ai和bi必须要在一个团体里,问是否能够把这n个人分成n/3队,输出这n/3个集合。

思路:并查集归并每个集合,并且记录每个集合中元素的个数,有大于3的直接输出-1即可,vector记录当前集合中存在哪些元素;对于集合元素小于3的集合,去找其他集合合并,直到集合中元素的个数为3.

代码如下:

#include <bits/stdc++.h>
using namespace std;

int n,m,par[110],cnt[110],vis[110];
vector<int>vct[110];

int FindSet(int x){
    if(x == par[x]) return x;
    return par[x] = FindSet(par[x]);
}

void UnoinSet(int x, int y){
    int fx = FindSet(x);
    int fy = FindSet(y);

    if(fx != fy) par[fx] = fy;

}

int main(){
    scanf("%d%d",&n,&m);
    int x,y;
    for(int i = 1; i <= n; i ++) par[i] = i;
    for(int i = 0; i < m; i ++){
        scanf("%d%d",&x,&y);
        UnoinSet(x,y);
    }
    for(int i = 1; i <= n; i ++){
        x = FindSet(i);
        cnt[x] ++;
        vct[x].push_back(i);
    }
    int MaxValue = 0;
    for(int i = 1; i <= n; i ++) MaxValue = max(MaxValue,cnt[i]);
    if(MaxValue > 3) printf("-1\n");
    else{
        int flag = 1;
        for(int i = n; i >= 1; i --){
            if(!vis[i] && cnt[i] < 3 && cnt[i] > 0){
                for(int j = i-1; j >= 1; j --){
                    if(!vis[j] && cnt[i] + cnt[j] <= 3){
                        cnt[i] += cnt[j]; vis[j] = 1;
                        for(int k = 0; k < vct[j].size(); k ++){
                            vct[i].push_back(vct[j][k]);
                        }
                    }
                }
                if(cnt[i] != 3){
                    flag = 0; break;
                }
            }
        }
        
        if(!flag) printf("-1\n");
        else{
            for(int i = 1; i <= n ;i ++){
                if(cnt[i] == 3){
                    for(int j = 0; j < vct[i].size(); j ++){
                        if(j) printf(" ");
                        printf("%d",vct[i][j]);
                    }
                    printf("\n");
                }
            }
        }
    }
    return 0;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值