【暑假】[深入动态规划]UVa 1627 Team them up!
UVa 1627 Team them up!
题目:
| Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
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[][]判断是否存在即可。
代码:
#include<cstdio>
#include<cstring>
#include<vector>
#define FOR(a,b,c) for(int a=(b);a<(c);a++)
using namespace std; const int maxn = + ; int colors_num,n,m;
int d[maxn][*maxn],diff[maxn];
int G[maxn][maxn];
vector<int> team[maxn][];
int colors[maxn]; //如果不是二部图return false
bool dfs(int u,int c) {
colors[u]=c; //c==1 || 2
team[colors_num][c-].push_back(u);
FOR(v,,n)
if(u!=v && !(G[u][v]&&G[v][u])){ //不互相认识
if(colors[v]> && colors[u]==colors[v]) return false;
//u v不能在一组却出现在了一组
if(!colors[v] && !dfs(v,-c)) return false;
}
return true;
} bool build_graph() {
colors_num=;
memset(colors,,sizeof(colors)); FOR(i,,n) if(!colors[i]){
team[colors_num][].clear();
team[colors_num][].clear();
if(!dfs(i,)) return false;
diff[colors_num]=team[colors_num][].size()-team[colors_num][].size();
colors_num++;
}
return true;
} void print(int ans) {
vector<int> team1, team2;
for(int i = colors_num-; i >= ; i--) { //对 每个联通块
int t;
if(d[i][ans-diff[i]+n]) { t = ; ans -= diff[i]; } //判断+- //组号为t
else { t = ; ans += diff[i]; }
for(int j = ; j < team[i][t].size(); j++) //加入team1
team1.push_back(team[i][t][j]);
for(int j = ; j < team[i][^t].size(); j++) //加入team2
team2.push_back(team[i][^t][j]);
}
printf("%d", team1.size());
for(int i = ; i < team1.size(); i++) printf(" %d", team1[i]+);
printf("\n"); printf("%d", team2.size());
for(int i = ; i < team2.size(); i++) printf(" %d", team2[i]+);
printf("\n");
} void dp() {
//d[i][j+n] 代表考虑到第i个联通块时两组相差j的情况是否存在
memset(d,,sizeof(d));
d[][+n]=; //+n 调节范围
FOR(i,,colors_num)
FOR(j,-n,n+) if(d[i][j+n]) {
//刷表 存在
d[i+][j+n+diff[i]]=;
d[i+][j+n-diff[i]]=;
} FOR(ans,,n+) {
if(d[colors_num][n+ans]) {print(ans); return; }
if(d[colors_num][n-ans]) {print(-ans); return; }
}
} int main() {
int T; scanf("%d",&T);
while(T--) {
scanf("%d",&n);
FOR(u,,n) { //读入原图
int v;
while(scanf("%d",&v) && v) G[u][v-]=; //v-1调节序号
}
if(n== || !build_graph()) printf("No solution\n"); //n==1 -> no solution
else dp(); if(T) printf("\n");
}
return ;
}
【暑假】[深入动态规划]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 ...
- UVA 1627 Team them up!
https://cn.vjudge.net/problem/UVA-1627 题目 有n(n≤100)个人,把他们分成非空的两组,使得每个人都被分到一组,且同组中的人相互认识.要求两组的成员人数尽量接 ...
- UVa 1627 Team them up! (01背包+二分图)
题意:给n个分成两个组,保证每个组的人都相互认识,并且两组人数相差最少,给出一种方案. 析:首先我们可以知道如果某两个人不认识,那么他们肯定在不同的分组中,所以我们可以根据这个结论构造成一个图,如果两 ...
- UVA.540 Team Queue (队列)
UVA.540 Team Queue (队列) 题意分析 有t个团队正在排队,每次来一个新人的时候,他可以插入到他最后一个队友的身后,如果没有他的队友,那么他只能插入到队伍的最后.题目中包含以下操作: ...
- 【暑假】[深入动态规划]UVa 1628 Pizza Delivery
UVa 1628 Pizza Delivery 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=51189 思路: ...
- 【暑假】[深入动态规划]UVa 1380 A Scheduling Problem
UVa 1380 A Scheduling Problem 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=41557 ...
- 【暑假】[深入动态规划]UVa 12170 Easy Climb
UVa 12170 Easy Climb 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=24844 思路: 引别人一 ...
- 【暑假】[深入动态规划]UVa 10618 The Bookcase
UVa 12099 The Bookcase 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=42067 思路: ...
- 【暑假】[深入动态规划]UVa 10618 Fun Game
UVa 10618 Fun Game 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=36035 思路: 一圈人围坐 ...
随机推荐
- js获取节点
demo1: <!-- <div id="test" v="1">你好</div> --> // console.log(t ...
- 解决VS如何同时打开两个工程(xp和win7)
http://www.360doc.com/content/11/1020/00/7891073_157586269.shtml
- linux查找某个文件中单词出现的次数
文件名称:list 查找单词名称:test 操作命令: (1)more list | grep -o test | wc -l (2)cat list | grep -o test | wc -l ( ...
- 【Mysql进阶技巧(1)】 MySQL的多表关联与自连接
自连接 测试数据准备 CREATE TABLE `t2` ( `id` int(11) NOT NULL, `gid` char(1) DEFAULT NULL, `col1` int(11) DEF ...
- spring事物的传播行为
1.spring事物的传播行为,主要是用来解决业务层拥有事物的方法,相互调用的问题. 2.声明事物, 在代码执行前,开启事务.代码执行完,提交事务 3.spring并没有提供事务具体的处理,而只是调用 ...
- 分布式java应用
大型应用,通常会拆分为多个子系统来实现. 对Java来说,这些子系统可能部署在同一台机器的多个不同的JVM中,也可能部署在不同的机器上,但这些子系统又不是完全独立的,要相互通信来共同实现业 ...
- Android开源滤镜 仿instagram
前段时间做一个项目的时候发现一个不错的滤镜库,是仿Instagram效果的,能够实现Lomo在内的十几种滤镜效果,git地址是: https://github.com/beartung/insta-f ...
- Android 签名(1)为什么要签名
所有的应用程序都必须有数字证书,Android系统不会安装一个没有数字证书的应用程序 签名可以: 1,用特权,2完整性鉴别,3安全保证, 1,专用权限或特权要签名 一些特权要经签名才允许.签名可用:S ...
- Android开发UI之手动显示和隐藏软键盘
1.方法一(如果输入法在窗口上已经显示,则隐藏,反之则显示) InputMethodManager imm = (InputMethodManager) getSystemService(Contex ...
- Android横竖屏切换总结
Android横竖屏切换总结(Android资料) Android横竖屏要解决的问题应该就两个: 一.布局问题 二.重新载入问题 1.布局问题:如果不想让软件在横竖屏之间切换,最简单的办法就是在项目的 ...