【暑假】[深入动态规划]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 思路: 一圈人围坐 ...
随机推荐
- 1045: [HAOI2008] 糖果传递 - BZOJ
Description 有n个小朋友坐成一圈,每人有ai个糖果.每人只能给左右两人传递糖果.每人每次传递一个糖果代价为1.Input 小朋友个数n 下面n行 aiOutput 求使所有人获得均等糖果的 ...
- mysql语句中把string类型字段转datetime类型
mysql语句中把string类型字段转datetime类型 在mysql里面利用str_to_date()把字符串转换为日期 此处以表h_hotelcontext的Start_time和En ...
- 让阿里云的Centos,PHP组件 ImageMagick支持png和jpeg格式
我们在Centos安装ImageMagick教程中讲述了如何安装ImageMagick,安装完毕之后发现程序并不支持png和jpeg格式的图片,但是这两种图片又是我们平时所常见的,所以我们还要进一步地 ...
- Altium designer中级篇-名称决定多边形连接样式
在工作中积累了诸多小技巧,可以让工作变的更简单,就比如这个多边形铺铜,与大部分规则的不同之处在于,通过更改多边形的名称,就能达到控制多边形规则的效果.这样多边形铺铜变的及其灵活,下面将对这个经验做一个 ...
- Android:实现退出确认对话框
在Android平台上捕获Back键的事件,super.onBackPressed()是执行系统的默认动作,就是退出当前activity,我们要做的就是重写onBackPressed()函数, pub ...
- 关于hadoop的环境变量
export PATH export JAVA_HOME=/opt/jdk1.7 export PATH=$PATH:$JAVA_HOME/bin 为什么/etc/profile 添加了环境变量had ...
- WinCE NAND flash - FAL
http://blog.csdn.net/renpine/article/details/4572347 http://msdn.microsoft.com/en-US/library/ee48203 ...
- Unity3D GUI中的图片跟随鼠标旋转脚本
var Mid : Texture2D; var mouse : Texture2D; //鼠标图片 var mousePs = Vector2.zero; //鼠标的位置 private var a ...
- EXCEL VB
全面控制 Excel首先创建 Excel 对象,使用ComObj:Dim ExcelID as Excel.ApplicationSet ExcelID as new Excel.Applicatio ...
- sh脚本执行Java程序
1.不引用Jar包或者资源文件夹 最简单的程序Hello World. 首先创建Hello.java public class Hello { public static void main(Stri ...