ACM: 动态规划题 poj&nb…

Team Them Up!

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

 

题意: 现在要你将n个人分配到2个组里面, 已经给出每人的相识关系, 满足下列要求:

      (1). 每个人都属于一个组.

      (2). 每个组里面至少有一个人.

      (3). 组里面每个人都相互认识.

      (4). 2个组的人数尽可能相近.

      最后求出每个组的人数和成员.(全部人编号1~n)或则无解;

 

解题思路:

      1. 分组问题是: 二分染色问题并且判断是否有解情况.

         分析:

         (1). 每人相互之间关系, 题目已经给出. 建图, 求出补图, 补图中直接相连的节点

              表示不能分到同一个组.

         (2). 再进行染色, 在染色过程中, 发现节点有相邻的表示无解.

      2. 怎么输出每组人数和组员成为了难点. 纠结甚久. 在进行染色时候每个连通分量是一条

         路径. 在这条路径上分成了2个组. 并且每个连通分量都是如此.

         记录下每个连通分量的人数和具体哪些节点的分组情况.

         设: num[i][0/1]表示第i个连通分量分在 第0组 或则 第1组 的人数.

             p[i][0/1][k]表示第i个连通分量分在 第0组 或则 第1组 的第k个人得节点编号.

      3. 题目变成了O-1背包问题了. 设状态dp[i][j]表示前i个连通分量是否能装下j个人.

         状态方程: dp[i][j] = dp[i-1][ j-num[i][0/1] ] (j >= num[i][0/1]);

         记录路径: path[i][j] = 0/1;

 

代码:

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
#define MAX 105

int n, C;
int num[MAX][2], p[MAX][2][MAX], color[MAX], path[MAX][MAX];
bool dp[MAX][MAX], g[MAX][MAX];
int count; //连通分量的个数

void read_graph()
{
 memset(g, false, sizeof(g));
 memset(num, 0, sizeof(num));
 memset(p, 0, sizeof(p));
 memset(color, -1, sizeof(color));
 memset(dp, false, sizeof(dp));
 memset(path, -1, sizeof(path));
 count = 0;

 int i, j, v;
 for(i = 1; i <= n; ++i)
 {
  while(1)
  {
   scanf("%d", &v);
   if(v == 0) break;
   g[i][v] = true;
  }
 }

 for(i = 1; i <= n; ++i) //求补图
 {
  for(j = i+1; j <= n; ++j)
  {
   if(g[i][j] && g[j][i])
    g[i][j] = g[j][i] = false;
   else
    g[i][j] = g[j][i] = true;
  }
 }
}

bool dfs(int u) //二分染色
{
 int col = color[u];
 p[count][col][ num[count][col] ] = u;
 num[count][col]++;
 for(int i = 1; i <= n; ++i)
 {
  if(g[u][i])
  {
   if(color[i] == -1)
   {
    color[i] = col^1;
    if( !dfs(i) ) return false;
   }
   else if(color[i] == color[u]) return false;
  }
 }
 return true;
}

int DP()
{
 int c;
 dp[0][ num[0][0] ] = true;
 path[0][ num[0][0] ] = 0;
 dp[0][ num[0][1] ] = true;
 path[0][ num[0][1] ] = 1;

 for(int i = 1; i < count; ++i) //0-1背包求解人数分配,记录路径
 {
  for(int j = n/2; j >= 0; --j)
  {
   if( j >= num[i][0] && dp[i-1][ j-num[i][0] ] )
   {
    dp[i][j] = dp[i-1][ j-num[i][0] ];
    path[i][j] = 0;
    if(i == count-1)
    {
     c = j;
     break;
    }
    continue;
   }

   if( j >= num[i][1] && dp[i-1][ j-num[i][1] ])
   {
    dp[i][j] = dp[i-1][ j-num[i][1] ];
    path[i][j] = 1;
    if(i == count-1)
    {
     c = j;
     break;
    }
   }
  }
 }
 return c; //返回最大人数容量
}

void print_path()
{
 int i, j, k, result = 0;
 int temp = C, col;
 for(i = count-1; i >= 0; --i)
 {
  if(path[i][temp] == 0)
  {
   result += num[i][0];
   temp -= num[i][0];
  }
  else
  {
   result += num[i][1];
   temp -= num[i][1];
  }
 }
 printf("%d", result);
 
 temp = C;
 for(i = count-1; i >= 0; --i)
 {
  col = 1;
  if(path[i][temp] == 0)
  {
   col = 0;
   temp -= num[i][0];
  }
  else temp -= num[i][1];
  for(k = 0; k < num[i][col]; ++k)
   printf(" %d", p[i][col][k]);
 }
 printf("\n");
 printf("%d", n-result);
 
 temp = C;
 for(i = count-1; i >= 0; --i)
 {
  col = 0;
  if(path[i][temp] == 0)
  {
   col = 1;
   temp -= num[i][0];
  }
  else temp -= num[i][1];
  for(k = 0; k < num[i][col]; ++k)
   printf(" %d", p[i][col][k]);
 }
 printf("\n");
}

int main()
{
// freopen("input.txt", "r", stdin);
 while(scanf("%d", &n) != EOF)
 {
  read_graph();

  for(int i = 1; i <= n; ++i)
  {
   if(color[i] == -1)
   {
    color[i] = 0;
    if( !dfs(i) )
    {
     printf("No solution\n");
     goto end;
    }
    count++;
   }
  }
  C = DP();
  print_path();
end:;
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值