E - Coach CodeForces - 300B

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 a i, b i (1 ≤ a i < b i ≤ n) — the pair a i, b i means that students with numbers a i and b i want to be on the same team.

It is guaranteed that n is divisible by 3. It is guaranteed that each pair a i, b i 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 x i, y i, z i (1 ≤ x i, y i, z i ≤ 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

题意:
将所有人分成一些组,每组三个人。有些人要求在一个组。求能否成功分组。

思路:
直接模拟吧

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#include <map>
#include <unordered_map>

using namespace std;
typedef unsigned long long ll;

const int maxn = 100;

int a[maxn][maxn],vis[maxn];
vector<int>vec[maxn];

int main() {
    int n,m;scanf("%d%d",&n,&m);
    int flag = 1,cnt = 0;
    for(int i = 1;i <= m;i++) {
        int x,y;scanf("%d%d",&x,&y);
        if(vis[x] && vis[y]) {
            if(vis[x] != vis[y]) flag = 0;
        } else if(vis[x]) {
            vis[y] = vis[x];
            vec[vis[x]].push_back(y);
        } else if(vis[y]) {
            vis[x] = vis[y];
            vec[vis[y]].push_back(x);
        } else {
            vis[x] = vis[y] = ++cnt;
            vec[cnt].push_back(x);
            vec[cnt].push_back(y);
        }
    }
    if(!flag) printf("-1\n");
    else {
        for(int i = 1;i <= n;i++) {
            if(!vis[i]) {
                for(int j = 1;j <= n / 3;j++) {
                    if(vec[j].size() < 3) {
                        vec[j].push_back(i);
                        break;
                    }
                }
            }
        }
        for(int i = 1;i <= n / 3;i++) {
            if(vec[i].size() != 3) {
                printf("-1\n");
                return 0;
            }
        }
        
        for(int i = 1;i <= n / 3;i++) {
            printf("%d %d %d\n",vec[i][0],vec[i][1],vec[i][2]);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值