CodeForces 300B Coach(并查集)

Coach

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.

Example
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/3个小组,每组三个人。
有n个人和m对关系,每对关系里面两个人必须在一个队中,找出能满足条件分组情况之一即可。若不满足输出-1

思路: 用到并查集。关于并查集的博客,我看了几个,觉得最通俗易懂的还是这个博客:并查集-简单易懂。并查集确定每组人的个数和代表。然后遍历,如果出现个数超过三个的组,就直接输出-1,都小于等于3的话,接下来考虑2个的组个数是否大于一个的,因为2个人的组肯定要和一个人的组搭档,超出的话就肯定不满足条件了,剩下的情况就都是满足条件的。
小细节:输出的时候要给每个位置一个标记符,避免重复输出。先输出3个,接下来输出两个和一个配对的。最后输出一个的,3个3个一队。

#include<stdio.h>
//rang[i] 以i为代表的组人的个数 flag[i]表示第i个是否被输出过 pre[i]第i个的代表
int pre[100], rank[100],flag[100];
void unite(int x, int y);
int find(int x);
int main()
{
    int n, m,i,a,b,j,s=0;
    scanf("%d%d", &n, &m);
    for (i = 1; i <= n; ++i)
    {
        pre[i] = i;
        rank[i] = 1;
        flag[i] = 1;
    }
    for (i = 0; i < m; ++i)
    {
        scanf("%d%d", &a, &b);
        unite(a, b);
    }
    int x = 0, y = 0;
    for (i = 1; i <= n; ++i)
    {
        if (find(i) == i)
        {
            if (rank[i] > 3) {
                printf("-1\n");
                return 0;
            }
            else if (rank[i] == 2)x++;
            else if (rank[i] == 1)y++;
        }
    }
    if (x > y) {
        printf("-1\n");
        return 0;
    }
    for (i = 1; i <= n; i++)
    {
        if (rank[i] == 3) 
        {
            printf("%d ", i);
            s++;
            flag[i] = 0;
            for (j = 1; j <= n; j++)
            {
                if (find(j) == i&&flag[j])
                {
                    s++;
                    if(s!=3)
                    printf("%d ", j);
                    else printf("%d\n", j);
                    flag[j] = 0;
                }
            }
        }
    }
    for (i = 1; i <= n; i++)
    {
        if (rank[i] == 2)
        {
            printf("%d ", i);
            flag[i] = 0;
            for (j = 1; j <= n; j++)
            {
                if (find(j) == i&&flag[j])
                {
                    printf("%d ", j);
                    flag[j] = 0;
                }
            }
            for(j=1;i<=n;j++)
                if (find(j) == j&&rank[j]==1&&flag[j])
                {
                    printf("%d\n", j);
                    flag[j] = 0;
                    break;
                }
        }
    }
    j = 0;
    for (i = 1; i <= n; ++i)
    {
        if (find(i) == i&&rank[i] == 1 && flag[i])
        {
            if (j != 2)
            {
                printf("%d ", i);
                j++;
            }
            else
            {
                j = 0;
                printf("%d\n", i);
            }
        }
    }
    return 0;
}
//合并函数  两组合并 人数少的合并到多的 成为人数多的子树
void unite(int x, int y)
{
    int fx = find(x);
    int fy = find(y);
    if (fx != fy)
    {
        if (rank[fx] >=rank[fy])
        {
            pre[fy] = fx;
            rank[fx] += rank[fy];
        }
        else
        {
            pre[fx] = fy;
            rank[fy] += rank[fx];
        }
    }
}
//查找函数
int find(int x)
{
    if (pre[x] != x)
        pre[x] = find(pre[x]);  //压缩路径 一个组内的直接指向代表
    return pre[x];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值