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

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個人中創建n/3個ACM隊,問你是否能建成。

思路:看了幾個博客都說是並查集,不過我的方法並不是並查集,可能是數據量太少才AC了。

  我的思路是

 1:先選出可以3個人相互組隊的(就是三個人相互喜歡的),存在結構體out中;

 2:然後選出只有兩個人相互喜歡的,然後把那些沒有提及想和誰一隊的人看成是和誰組隊都行的人。再對他們進行組隊。

 3:然後剩下和誰組隊都行的人(就是2中我們標註的),對他們進行組隊。

 依次進行玩以上的步驟後,發現AC不了,其實這就是這道題的坑點,如果存在一個人想和兩個以上的人組隊,那麼直接輸出-1。

上代碼:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<set>
using namespace std;
int vis[50];
int g[50][50];
int used[50];
set<int> pp;
struct node{
  int x;
  int y;
  int z;
};
node out[50];
int cishu[50];
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m) != EOF)
    {
        memset(vis, 0,sizeof(vis));
        memset(g   , 0,sizeof(g));
        memset(used, 0,sizeof(used));
        memset(cishu, 0,sizeof(cishu));
        int a,b;
        for(int i=1; i<=m; i++)
        {
            scanf("%d %d",&a,&b);
            vis[a] = vis[b] = 1;
            cishu[a]++;
            cishu[b]++;
            g[a][b] = g[b][a]=1;
        }
        int flag = 1;
        for(int i=1; i<=n; i++)
        {
          if(cishu[i] > 2){flag=0;break;}
        }
        if(flag==0){printf("-1\n");continue;}
        int w = 0;
        for(int i=1; i<=n; i++)
        {
           if(!(used[i]==0&&vis[i]==1))continue;
           for(int j=i+1; j<=n; j++)
           {
              if(g[i][j]==0) continue;
              if(!(used[j]==0&&vis[j]==1))continue;
              for(int k=j+1; k<=n; k++)
              {
                  if(!(used[k]==0&&vis[k]==1))continue;
                  if(g[i][k]==1&&g[j][k]==1){used[i]=used[j]=used[k]=1;++w;out[w].x=i;out[w].y=j;out[w].z=k;break;}
              }
              if(used[j]==1)break;
            }
        }
        if(w<n/3)
        for(int i=1; i<=n; i++)
        {
            if(!(vis[i]==1&&used[i]==0))continue;
            for(int j=1; j<=n; j++)
            {
              if(i==j)continue;
              if(!(vis[j]==1&&used[j]==0))continue;
              if(g[i][j]==0)continue;
              for(int k=1; k<=n; k++)
              {
                  if(i==j || i==k)continue;
                  if(vis[k]==0&&used[k]==0)
                  {used[i]=used[j]=used[k]=1;++w;out[w].x=i;out[w].y=j;out[w].z=k;break;}
              }
            }
        }
        int ko = 0;
        if(w<n/3)
        {
            pp.clear();
            for(int i=1; i<=n; i++)
            {
                if(vis[i]==0&&used[i]==0){pp.insert(i);}
            }
            ko = pp.size()/3;
        }
        if(w+ko<n/3)printf("-1\n");
        else
        {
            for(int i=1; i<=w; i++)
            {
                printf("%d %d %d\n",out[i].x, out[i].y, out[i].z);
            }
           for(set<int>::iterator it=pp.begin(); it!=pp.end(); ++it)
           {
               cout << *it << " "; ++it;
               cout << *it << " "; ++it;
               cout << *it << endl;
           }
        }
     }
   return 0;
}
水波。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章