关闭

B. Coach

1445人阅读 评论(0) 收藏 举报
分类:

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 ton, 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.

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 

解题说明:本题的意思是教练培训n个同学(n%3 = 0),要给他们分组,每3个人一组,要分n/3组,输入的m行中的x、y代表x想和y分一组,教练会根据他们的请求将他们分为一组,若最后分组不成功输出-1,否则一一输出各组同学的序号。

参考http://www.cnblogs.com/LK1994/p/3427099.html :先套并查集模板将可以分成一组的以邻接表的形式存起来,然后再判断,如果与i一组的人数大于3,说明分组不成功

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int n,m;
int gp[50][50];//gp[i][j]表示j与i可以分一组
int cnt[50];//cnt[i]表示与i一组的人数
int vis[50];
int set[50];
queue<int>que[4];
int find(int x)
{
    if(set[x] != x)
        set[x] = find(set[x]);
    return set[x];
}

int main()
{
    scanf("%d %d",&n,&m);
    int t,flag;
    for(int i = 1; i <= n; i++)
        set[i] = i;
    int u,v;
    for(int i = 1; i <= m; i++)
    {
        scanf("%d %d",&u,&v);
        int uu = find(u);
        int vv = find(v);
        if(uu != vv)
            set[uu] = vv;
    }
    for(int i = 1; i <= n; i++)
    {
        cnt[i] = 0;
        t = find(i);
        for(int j = 1; j <= n; j++)
        {
            if(t == find(j))
            {
                gp[i][cnt[i]++] = j;
            }
        }
    }//并查集,将所有可能分一组的存起来
    
    memset(vis,0,sizeof(vis));
    flag = 1;
    for(int i = 1; i <= n; i++)
    {
        if(cnt[i] > 3)//如果与i一组的人数大于3,不合法
        {
            flag = 0;
            continue;
        }
        if(vis[gp[i][0]]) continue;
        
        else if(cnt[i] == 1)
            que[1].push(i);
        else if(cnt[i] == 2)
            que[2].push(i);
        else if(cnt[i] == 3)
            que[3].push(i);

        vis[gp[i][0]] = 1;
    }
    if(que[1].size() < que[2].size() || (que[1].size()-que[2].size())%3 !=0)
        flag = 0;

    if(flag)
    {
        while(que[2].size() > 0)
        {
            int sec = que[2].front();
            que[2].pop();
            printf("%d %d ",gp[sec][0],gp[sec][1]);

            int fir = que[1].front();
            que[1].pop();
            printf("%d\n",gp[fir][0]);
        }

        while(que[1].size() > 0)
        {
            int fir = que[1].front();
            que[1].pop();
            printf("%d ",gp[fir][0]);

            fir = que[1].front();
            que[1].pop();
            printf("%d ",gp[fir][0]);

            fir = que[1].front();
            que[1].pop();
            printf("%d\n",gp[fir][0]);
        }

        while(que[3].size() > 0)
        {
            int thi= que[3].front();
            que[3].pop();
            printf("%d %d %d\n",gp[thi][0],gp[thi][1],gp[thi][2]);
        }
    }
    else printf("-1\n");

    return 0;
}


0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

B树、B-tree B+树、B*树

BST 即二叉搜索树: 1.所有非叶子结点至多拥有两个儿子(Left和Right); 2.所有结点存储一个关键字; 3.非叶子结点的左指针指向小于其关键字的子树,右指针指向大于其关键字的子树;...
  • yusiguyuan
  • yusiguyuan
  • 2014-11-07 21:59
  • 1144

B树、B-树、B+树、B*树

源地址丢失。不好意思。 B树        即二叉搜索树:        1.所有非叶子结点至多拥有两个儿子(Left和Right);  ...
  • huyanping
  • huyanping
  • 2013-02-01 13:31
  • 1322

B树、B-树、B+树、B*树

B树        即二叉搜索树:        1.所有非叶子结点至多拥有两个儿子(Left和Right);      ...
  • ILOVESMJ
  • ILOVESMJ
  • 2016-06-08 18:29
  • 3916

codeforces 181.div2 300B - Coach 并查集

第一次见这样的并查集~~ 做的都无语了~~ 好多细节要注意 题目链接 题目是这样的,n个人(能被3整除)然后,按照队员的要求分组,有要求和喜欢的人在一起的的一定要到一组,一组最多有3人...
  • yangshuolll
  • yangshuolll
  • 2013-04-26 02:38
  • 1109

2017年上海金马五校程序设计竞赛(网上资格赛) B Coach(并查集)

Coach From: DHUOJ, 2016060703 Submit  Time Limit: 1 s Description In the ACM training te...
  • xunalove
  • xunalove
  • 2017-05-29 14:21
  • 381

Coach CodeForces - 300B

A programming coach has n students to teach. We know that n is divisible by 3. Let's assume that all...
  • qq_36556340
  • qq_36556340
  • 2017-07-02 19:54
  • 137

详解Nervana最新开源深度强化学习库Coach,支持DQN、DDQN等十多种算法|附开源代码

原文来源:github 作者:Gal Leibovich 「雷克世界」编译:嗯~阿童木呀、多啦A亮   概述   Coach是一个python强化学习研究框架,包含许多最先进算...
  • Ksf3kg7dU95rn0XL
  • Ksf3kg7dU95rn0XL
  • 2017-10-23 00:00
  • 144

研发管理07:Agile Coach---如何构建敏捷项目管理团队---帮助团队成长

本篇文章是 如何构建敏捷项目管理团队 系列文章的第二篇。   介绍Scrum Coach 如何帮助团队成长! 帮助团队成长 概述 第一部分;  教练是导师 ...
  • bamboolsu
  • bamboolsu
  • 2015-05-12 14:33
  • 1392

英特尔Nervana发布强化学习库Coach:支持多种价值与策略优化算法

近日,英特尔 Nervana 发布了一个强化学习库 Coach,能够支持许多顶级的强化学习算法。   项目地址:http://coach.nervanasys.com  什么是 Coach 动...
  • Uwr44UOuQcNsUQb60zk2
  • Uwr44UOuQcNsUQb60zk2
  • 2017-10-24 20:20
  • 43

WHU 1124 Football Coach(最大流,巧妙构图)

Problem 1124 - Football Coach Time Limit: 2000MS   Memory Limit: 65536KB    Total Submit: 332  A...
  • acm_cxq
  • acm_cxq
  • 2016-06-03 16:25
  • 278
    个人资料
    • 访问:2072987次
    • 积分:36975
    • 等级:
    • 排名:第132名
    • 原创:1495篇
    • 转载:121篇
    • 译文:19篇
    • 评论:952条
    最新评论