zoukankan      html  css  js  c++  java
  • Codeforces Round #181 (Div. 2) B. Coach 带权并查集

    B. Coach

    题目连接:

    http://www.codeforces.com/contest/300/problem/A

    Description

    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.

    Sample Input

    3 0

    Sample Output

    3 2 1

    Hint

    题意

    有n个点,然后有m个条边。

    你需要分成n/3个组,每个组必须3个人,连在一起的点,必须分在同一个组

    输出方案,没有输出-1

    题解:

    带权并查集

    相连的时候,维护一下这个集合里面点的个数就好了

    如果不够的话,就看看自由的点能不能插进去

    讨论一下就好了(雾

    代码

    #include<bits/stdc++.h>
    using namespace std;
    
    int fa[55];
    int num[55];
    int vis[55];
    vector<int> group[55];
    vector<int> temp;
    int fi(int x)
    {
        return x==fa[x]?x:fa[x]=fi(fa[x]);
    }
    void uni(int x,int y)
    {
        int p=fa[x],q=fa[y];
        if(p==q)return;
        fa[q]=p;
        num[p]+=num[q];
        num[q]=0;
        vis[x]=1,vis[y]=1;
        for(int i=0;i<group[q].size();i++)
            group[p].push_back(group[q][i]);
        group[q].clear();
    }
    int main()
    {
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
            fa[i]=i,num[i]=1,group[i].push_back(i);
        for(int i=1;i<=m;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            uni(x,y);
        }
        for(int i=1;i<=n;i++)
        {
            if(num[fi(i)]>3)
                return puts("-1");
        }
        int tot = 0;
        for(int i=1;i<=n;i++)
            if(!vis[i])temp.push_back(i),group[i].clear();
        for(int i=1;i<=n;i++)
        {
            if(group[i].size()==0)continue;
            if(group[i].size()==2)
            {
                if(tot==temp.size())return puts("-1");
                group[i].push_back(temp[tot++]);
            }
        }
        for(int i=1;i<=n;i++)
        {
            if(group[i].size()==3)
            {
                for(int j=0;j<3;j++)
                    printf("%d ",group[i][j]);
                printf("
    ");
            }
        }
        for(int i=tot;i<temp.size();i+=3)
            printf("%d %d %d
    ",temp[i],temp[i+1],temp[i+2]);
    }
  • 相关阅读:
    Eclipse 中使用 ctrl 无法追踪函数的问题
    AJAX跨域问题
    eclipse设置svn代理
    同步IO和异步IO
    阿里云配置redis
    Centos +django+nginx
    Centos 安装nginx
    django 给前端传递HTML内容
    django项目初探
    python邮件服务
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5118590.html
Copyright © 2011-2022 走看看