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.

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;
}


你可能感兴趣的:(CodeForces 300B Coach(并查集 + 水题))