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

UVa 1627 Team them up!

题目:

Team them up!

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

Submit 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("\n");
56
57   printf("%d", team2.size());
58   for(int i = 0; i < team2.size(); i++) printf(" %d", team2[i]+1);
59   printf("\n");
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"); //n==1 -> no solution
90       else  dp();
91
92       if(T) printf("\n");
93   }
94   return 0;
95 }
时间: 2024-10-12 10:25:41

【暑假】[深入动态规划]UVa 1627 Team them up!的相关文章

UVa 1627 - Team them up!——[0-1背包]

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

Uva 11609 - Team ( 组合数学 + 二项式性质 + 快速幂取模 )

Uva 11609 - Team ( 组合数学 + 二项式性质 + 快速幂取模 ) 题意: 有N个人,选一个或多个人参加比赛,其中一名当队长,有多少种方案? (如果参赛者完全相同但是队长不同,也算是一种情况) [ 1<=n <= 10^9 ] 分析: 这题要用到组合式公式的性质 转化之后快速幂取模轻松搞定之 代码: //Uva 11609 - Team /* 组合数公式 + 二项式系数性质 + 快速幂 手动自己推 -> F[n] = C(n,1)*1 + C(n,2)*2 + C(n,n

【暑假】[深入动态规划]UVa 12170 Easy Climb

UVa 12170 Easy Climb 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=24844 思路:  引别人一个题解琢磨一下: from:http://blog.csdn.net/glqac/article/details/45257659 代码: 1 #include<iostream> 2 #include<algorithm> 4 #define FOR(a,b,c) for(int a

【暑假】[深入动态规划]UVa 10618 Fixing the Great Wall

UVa 10618 Fixing the Great Wall 题目:  http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=36139 思路:   数轴上有n个点需要修复,每个点有信息c,x,d 表示位于x且在t时修缮的费用是c+d*t,找一个修缮序列使n个点能全部修缮且有费用最小. 可以发现:在任意时刻,修缮完的点都是连续的,因为修缮不需要时间,将一些点“顺手”修缮了肯定不差. d[i][j][k],表示已经将i-j个点修缮

【暑假】[深入动态规划]UVa 1628 Pizza Delivery

UVa 1628 Pizza Delivery 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=51189 思路:   本体与修缮长城一题有所相似.所以解法有相似之处. 不同之处就是本体可能会产生负情况,即送餐时间晚了客户会反过来找你要钱所以需要放弃,但修缮长城只有费用,顺手修了肯定是一个不错的选择. 依旧将区间两端与位置作为状态不过要添加一维cnt表示还需要送餐的人数.类似地定义:d[i][j][cnt][p]表示

【暑假】[深入动态规划]UVa 10618 The Bookcase

UVa 12099  The Bookcase 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=42067 思路:   将n本书分配到三层,使得形成的书架w*h最小 提前将书籍按照高度排序,因为无论第一本书(最高的书)无论放在那一层都会被考虑到,所以规定将它放在第一层,且第二层比第三层高. 因为从大到小排序的关系,只要jk==0那么新加入的书i就是该层的高度,否则高度不变. 设d[i][j][k]表示考虑过i本书第二

【暑假】[深入动态规划]UVa 1412 Fund Management

UVa 1412 Fund Management 题目: UVA - 1412 Fund Management Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Frank is a portfolio manager of a closed-end fund for Advanced Commercial Markets (ACM ). Fund

【暑假】[深入动态规划]UVa 10618 Tango Tango Insurrection

UVa 10618 Tango Tango Insurrection 题目: Problem A: Tango Tango Insurrection You are attempting to learn to play a simple arcade dancing game. The game has 4 arrows set into a pad: Up, Left, Down, Right. While a song plays, you watch arrows rise on a s

【暑假】[深入动态规划]UVa 1380 A Scheduling Problem

 UVa 1380 A Scheduling Problem 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=41557 思路:   (/▽\=) 代码: 1 // UVa1380 A Scheduling Problem 2 // Rujia Liu 3 #include<iostream> 4 #include<string> 5 #include<cstring> 6 #include