【暑假】[深入动态规划]UVa 1627 Team them up!

UVa 1627 Team them up!

题目:

Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

 Status

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 

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

--------------------------------------------------------------------------------------------------------------------------------------------------------------------

思路:

  给出关系图,不相识(互相)的两人必须分在不同组,要求分成两组且分组后有两组人数相差最少。

  按照相反关系重新建图,如果两人不互相认识则连边,那么在一个联通块中,如何分组或是不能分组可知。如果不能构成二分图,那么问题无解因为不能满足必须分在不同组的要求。

  设d[i][j+n]表示已经考虑到第i个联通块且两组相差i的情况是否存在。因为 j 属于[-n,n]所以需要+n调节j的范围。

  有状态转移方程:

      if(d[i][j+n]) 

             d[i+1][j+n+diff[i]]=1;

             d[i+1][j+n-diff[i]]=1;

其中diff[i]代表第i个联通块可分成的两组人数之差。

ans的得到需要按绝对值从小到大依此枚举,根据d[][]判断是否存在即可。

代码:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<vector>
 4 #define FOR(a,b,c) for(int a=(b);a<(c);a++)
 5 using namespace std;
 6 
 7 const int maxn = 100 + 5;
 8 
 9 int colors_num,n,m;
10 int d[maxn][2*maxn],diff[maxn];
11 int G[maxn][maxn];
12 vector<int> team[maxn][2];
13 int colors[maxn];
14 
15 //如果不是二部图return false 
16 bool dfs(int u,int c) {
17     colors[u]=c;  //c==1 || 2
18     team[colors_num][c-1].push_back(u);
19     FOR(v,0,n) 
20     if(u!=v && !(G[u][v]&&G[v][u])){ //不互相认识 
21         if(colors[v]>0 && colors[u]==colors[v]) return false;
22         //u v不能在一组却出现在了一组 
23         if(!colors[v] && !dfs(v,3-c)) return false; 
24     }
25     return true;
26 }
27 
28 bool build_graph() {
29     colors_num=0; 
30     memset(colors,0,sizeof(colors));
31     
32     FOR(i,0,n) if(!colors[i]){
33         team[colors_num][0].clear();
34         team[colors_num][1].clear();
35         if(!dfs(i,1)) return false;
36         diff[colors_num]=team[colors_num][0].size()-team[colors_num][1].size();
37         colors_num++;
38     }
39     return true;
40 }
41 
42 void print(int ans) {
43   vector<int> team1, team2;
44   for(int i = colors_num-1; i >= 0; i--) {  //对 每个联通块 
45     int t;
46     if(d[i][ans-diff[i]+n]) { t = 0; ans -= diff[i]; }  //判断+- //组号为t 
47     else { t = 1; ans += diff[i]; }
48     for(int j = 0; j < team[i][t].size(); j++)  //加入team1 
49       team1.push_back(team[i][t][j]);
50     for(int j = 0; j < team[i][1^t].size(); j++) //加入team2 
51       team2.push_back(team[i][1^t][j]);
52   }
53   printf("%d", team1.size());
54   for(int i = 0; i < team1.size(); i++) printf(" %d", team1[i]+1);
55   printf("
");
56 
57   printf("%d", team2.size());
58   for(int i = 0; i < team2.size(); i++) printf(" %d", team2[i]+1);
59   printf("
");
60 }
61 
62 void dp() {
63     //d[i][j+n] 代表考虑到第i个联通块时两组相差j的情况是否存在 
64     memset(d,0,sizeof(d));
65     d[0][0+n]=1;
66     
67     //+n 调节范围 
68     FOR(i,0,colors_num) 
69       FOR(j,-n,n+1) if(d[i][j+n]) {
70           //刷表 存在 
71           d[i+1][j+n+diff[i]]=1;
72           d[i+1][j+n-diff[i]]=1;
73       }
74       
75     FOR(ans,0,n+1) {
76         if(d[colors_num][n+ans]) {print(ans); return; }
77         if(d[colors_num][n-ans]) {print(-ans); return; }
78     }
79 }
80 
81 int main() {
82   int T;  scanf("%d",&T);
83   while(T--) {
84       scanf("%d",&n);
85       FOR(u,0,n) {  //读入原图 
86           int v; 
87           while(scanf("%d",&v) && v) G[u][v-1]=1; //v-1调节序号 
88       }
89       if(n==1 || !build_graph()) printf("No solution
"); //n==1 -> no solution 
90       else  dp();
91       
92       if(T) printf("
");
93   }
94   return 0;
95 }
原文地址:https://www.cnblogs.com/lidaxin/p/4740396.html