1627 - Team them up!

来源:互联网 发布:mac os x输入法切换 编辑:程序博客网 时间:2024/06/07 07:45

Your task is to divide a number of persons into two teams, in such a way, that:

everyone belongs to one of the teams;
every team has at least one member;
every person in the team knows every other person in his team;
teams are as close in their sizes as possible.
This task may have many solutions. You are to find and output any solution, or to report that the solution does not exist.

Input

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.
For simplicity, all persons are assigned a unique integer identifier from 1 to N.

The first line in the input file contains a single integer number N (2 ≤ N ≤ 100) - the total number of persons to divide into teams, followed by N lines - one line per person in ascending order of their identifiers. Each line contains the list of distinct numbers Aij (1 ≤ Aij ≤ N, Aij ≠ i) separated by spaces. The list represents identifiers of persons that ith person knows. The list is terminated by 0.

Output

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.
If the solution to the problem does not exist, then write a single message “No solution” (without quotes) to the output file. Otherwise write a solution on two lines. On the first line of the output file write the number of persons in the first team, followed by the identifiers of persons in the first team, placing one space before each identifier. On the second line describe the second team in the same way. You may write teams and identifiers of persons in a team in any order.

Sample Input

2

5
3 4 5 0
1 3 5 0
2 1 4 5 0
2 3 5 0
1 2 3 4 0

5
2 3 5 0
1 4 5 3 0
1 2 5 0
1 2 3 0
4 3 2 1 0
Sample Output

No solution

3 1 3 5
2 2 4

我有话说:
这道题我们可以换一种思想,不是去找认识的那一组关系,而是区分不认识的人。因为不认识的是不可能分到同一组的,也是影响两组之间人数差距的根本因素。
所以我们可以根据不认识关系确定所有人之间的关系图谱。这时就会有一堆联通分量。所以就可以根据他们来划分界限。然后这根据cc个联通分量的中心节点划分team1,team2。使两队之差最小。当然也不用担心后来加入的人和之前已加入的人会引起冲突,如果有一定是不能解决的。因为我们是以不认识关系为图dfs,会处理掉所有相关联的节点。后来还没有被处理的人一定是和已经处理过的人是认识的。

#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <vector>#include <cmath>using namespace std;const int maxn=100+5;int n,G[maxn][maxn],color[maxn],diff[maxn],cc;vector<int>team[maxn][2];//team[cc][c]存储第cc个color c的联通分量,联通分量的点雨点之间的关系是不认识bool dfs(int u,int c){    color[u]=c;    team[cc][c-1].push_back(u);    for(int v=0;v<n;v++)    {        if(u!=v&&!(G[u][v]&&G[v][u]))//两个人不认识应该被分到不同的队伍里去,因此,如果出现在同队的现象则为错        {            if(color[v]>0&&color[u]==color[v])return false;            if(color[v]==0&&!dfs(v,3-c))return false;        }    }    return true;}bool build_graph(){    memset(color,0,sizeof(color));    cc=0;    for(int i=0;i<n;i++)    {        if(!color[i])//还有未标记的点,以之为中心的联通分量判别队伍        {            team[cc][0].clear();            team[cc][1].clear();            if(!dfs(i,1))return false;            diff[cc]=team[cc][0].size()-team[cc][1].size();            cc++;        }    }    return true;}int d[maxn][maxn*2],teamno[maxn];//d[i][j+n]表示计算i个联通分量后team 1比team 2多j个人。j为负数,即少void print(int ans){    vector<int>team1,team2;    for(int i=cc-1;i>=0;i--)    {        int t;        if(d[i][ans-diff[i]+n]){t=0;ans-=diff[i];}//中心节点分入了team1;        else {t=1;ans+=diff[i];}        for(int j=0;j<team[i][t].size();j++)            team1.push_back(team[i][t][j]);        for(int j=0;j<team[i][t^1].size();j++)            team2.push_back(team[i][t^1][j]);    }    printf("%d",team1.size());    for(int j=0;j<team1.size();j++)        printf(" %d",team1[j]+1);    printf("\n");    printf("%d",team2.size());    for(int j=0;j<team2.size();j++)        printf(" %d",team2[j]+1);     printf("\n");}void dp(){    memset(d,0,sizeof(d));    d[0][0+n]=1;    for(int i=0;i<cc;i++)    {        for(int j=-n;j<=n;j++)        {            if(d[i][j+n])            {                d[i+1][j+diff[i]+n]=1;//1表示该种情况可成立,同时加入后两队人数差发生了变化                d[i+1][j-diff[i]+n]=1;//中心节点既可以加入team1,也可以加入team2,因此有两种情况            }        }    }    for(int ans=0;ans<=n;ans++)    {        if(d[cc][ans+n]){print(ans);return;}        if(d[cc][-ans+n]){print(-ans);return;}    }}int main(){    int T;    cin>>T;    while(T--)    {        cin>>n;        memset(G,0,sizeof(G));        for(int u=0;u<n;u++)        {            int v;            while(cin>>v&&v)G[u][v-1]=1;//读图        }        if(n==1||!build_graph())cout<<"No solution\n";        else dp();        if(T)cout<<"\n";    }    return 0;}
0 0
原创粉丝点击