poj1112 team them up

http://www.elijahqi.win/archives/1138
Description
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
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
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
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
3 1 3 5
2 2 4
Source
Northeastern Europe 2001

昨晚一点多抄完

发现错了,干脆不调试了,今天早晨起来认真理清下思路继续

首先我们需要建出补集来 如何建补集就是如果两个人有一方不认识对方,那么我们就建边

然后二分图染色 看能否构成二分图,如果不能构成二分图那么 直接输出no solution

如果可以的话相当于背包dp

同一组里的人只是与组内颜色不同的人互不认识 所以直接dp就很简单了

打印路径的时候 应该选择最接近中间的地方开始打印


#include<cstdio>
#include<cstring>
#include<vector>
#define N 110
using namespace std;
inline int read(){
    int x=0;char ch=getchar();
    while (ch<'0'||ch>'9') ch=getchar();
    while (ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();}
    return x;
}
struct node{
    int y,next;
}data[2*N*N];
int num,h[N],cnt[N][2],tot,n,color[N];
bool map[N][N],f[N][N],flag[N];
vector<int> a[N][2];
inline void insert1(int x,int y){
    data[++num].y=y;data[num].next=h[x];h[x]=num;
    data[++num].y=x;data[num].next=h[y];h[y]=num;
}
bool dfs(int x,int colour){
    color[x]=colour;a[tot][colour].push_back(x);
    for (int i=h[x];i;i=data[i].next){
        int y=data[i].y;
        if (color[y]==color[x]) return false;
        if (color[y]!=color[x]&&color[y]!=-1) continue;
        if (!dfs(y,colour^1)) return false;
    }
    return true;
}
int main(){
    freopen("poj1112.in","r",stdin);
    n=read();
    for (int i=1;i<=n;++i){
        int j;j=read();while (j){
            map[i][j]=true;j=read();
        }
    }
    for (int i=1;i<=n;++i)
        for (int j=i+1;j<=n;++j) if(!map[i][j]||!map[j][i]) insert1(i,j);
    memset(color,-1,sizeof(color));
    for (int i=1;i<=n;++i){
        if (color[i]==-1){++tot;
            if (!dfs(i,0)){printf("No solution");return 0;}
            cnt[tot][0]=a[tot][0].size();cnt[tot][1]=a[tot][1].size();
        }   
    }//f[i][j]表示前1~i组分成一组有j人是否可行
//  for (int i=1;i<=tot;++i) printf("%d %d\n",cnt[i][0],cnt[i][1]);
    f[0][0]=true;
    for (int i=1;i<=tot;++i){
        for (int j=0;j<=100;++j){
            if(j>=cnt[i][0]){
                if(f[i-1][j-cnt[i][0]]) f[i][j]=true;
            } 
            if (j>=cnt[i][1]){
                if (f[i-1][j-cnt[i][1]]) f[i][j]=true;
            }
        }
    } 
    int mid=0,ans=0;
    for (mid=n/2;mid;mid--) if (f[tot][mid]) break;
    //printf("%d\n",mid); 
    for (int i=tot;i>=1;--i){
        if (f[i-1][mid-cnt[i][0]]){
            mid-=cnt[i][0];ans+=cnt[i][0];
            for (int j=0;j<a[i][0].size();++j) flag[a[i][0][j]]=true;
        }else{
            mid-=cnt[i][1];ans+=cnt[i][1];
            for (int j=0;j<a[i][1].size();++j) flag[a[i][1][j]]=true;
        }
    }
    printf("%d ",ans);
    for (int i=1;i<=n;++i) if (flag[i]) printf("%d ",i);printf("\n");
    printf("%d ",n-ans);
    for (int i=1;i<=n;++i) if (!flag[i]) printf("%d ",i);
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值