首页 > 代码库 > Codeforces300B--LOL的匹配系统

Codeforces300B--LOL的匹配系统

#include <stdio.h>#include <stdlib.h>#include <math.h>#include <time.h>#include <string.h>#include <iostream>#include <vector>#include <list>#include <stack>#include <queue>#include <map>#include <set>#include <algorithm>typedef short int int16;///32767typedef int int32;///2147483647typedef long long int64;///9223372036854775807const double PI=acos(-1.0);///3.141593const long long MOD=(long long)1E9+7LL;///1000000007int isdigit(int c);int islower(int c);int isupper(int c);int tolower(int c);int toupper(int c);template <class T> T Susake_pow(T a,T b)///pow{T res;if(b==0) return 1;else while((b&1)==0){b>>=1;a*=a;}res=a;b>>=1;while(b!=0){a*=a;if((b&1)!=0)res*=a;b>>=1;}return res;}template<class T> inline T gcd(T a,T b)///gcd{if(a<0)return gcd(-a,b);if(b<0)return gcd(a,-b);return (b==0)?a:gcd(b,a%b);}template<class T> inline T lcm(T a,T b)///lcm{if(a<0)return lcm(-a,b);if(b<0)return lcm(a,-b);return a*(b/gcd(a,b));}template<class T> inline char *Susake_nsystem(T n)///itoa(26){T t=0,i;char *s,*p;s=(char *)malloc(sizeof(char)*1000);p=(char *)malloc(sizeof(char)*1000);while(n){s[t]=n%26+64;if(s[t]==64){s[t]+=26;n-=26;}t++;n/=26;}s[t]=\0;for(i = 0; i < t; i++)p[i]=s[t-1-i];p[i]=\0;free(s);return p;}int Susake_system(char *s)///atoi(26){int len=strlen(s),i,sum=0;char p[1000];for(i=0;i<len;i++)p[i]=s[len-1-i]-64;for(i=0;i<len;i++)sum+=p[i]*Susake_pow(26,i);return sum;}int fa[49], ans[49];template <class T> T union_find(T x)///union_find{return fa[x] == x ? x : fa[x] = union_find(fa[x]);}int teams[49] = { 0 };int team[49] = { 0 };int groups[49][49] = { 0 };int dfs(int s, int n, int t, std::vector<int> &g){    g.push_back(s);    int cnt = 0;    for (int i = 1; i <= n; ++i)        if (groups[s][i] && !team[i])        {            team[i] = team[s];            cnt += dfs(i,n,t,g);        }    return cnt + 1;}int main (){    int n,m;    scanf("%d %d",&n,&m);    for (int i = 0; i < m; ++i)    {        int a,b;        scanf("%d %d",&a,&b);        groups[a][b] = 1;        groups[b][a] = 1;    }    int ct=0;    std::vector<std::vector<int> > g1;    std::vector<std::vector<int> > g2;    std::vector<std::vector<int> > g3;    for (int i=1; i <= n; ++i)    {        if (!team[i])        {            ++ct;            team[i] = ct;            std::vector<int> g;            int cnt = dfs(i,n,ct,g);            if (cnt > 3)            {                printf("-1\n");                return 0;            }            teams[i] = cnt;            if (cnt == 1)                g1.push_back(g);            else if (cnt == 2)                g2.push_back(g);            else                g3.push_back(g);        }    }    if (g2.size() > g1.size())    {        printf("-1\n");        return 0;    }    // Merge teams with size 2 with size one    for (int i = 0; i < g2.size(); ++i)    {        g2[i].push_back(g1[i][0]);        g3.push_back(g2[i]);    }    // Merge the remaining of size one together    for (int i = g2.size(); i < g1.size(); i += 3)    {        g1[i].push_back(g1[i+1][0]);        g1[i].push_back(g1[i+2][0]);        g3.push_back(g1[i]);    }    for (int i = 0; i < g3.size(); ++i) printf("%d %d %d\n",g3[i][0],g3[i][1],g3[i][2]);    return 0;}

 

Codeforces300B--LOL的匹配系统