【暑假】[深入动态规划]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 思路: 一圈人围坐 ...
随机推荐
- BZOJ 3160 万径人踪灭 解题报告
这个题感觉很神呀.将 FFT 和 Manacher 有机结合在了一起. 首先我们不管那个 “不能连续” 的条件,那么我们就可以求出有多少对字母关于某一条直线对称,然后记 $T_i$ 为关于直线 $i$ ...
- [转载]jQuery.lazyload详解 - 图片延时加载
jQuery实现图片延迟加载,不知道是否可以节省带宽呢?有人知道吗? 这究竟只是一个视觉特效还是真的能延迟加载减少服务器的请求呢? <script type="text/javascr ...
- 如何在DJANGO里获取?带数据的东东,基于CBV
用DEF的,有现成的,而用CLASS的,就要作一下变通. 如下: if self.request.GET: if self.request.GET.get('search_pk'): search_p ...
- [主席树]HDOJ4417 Super Mario
题意:n个数 m个询问 ($n.m \le 10^5$) 每个询问有l, r, k 问的是[l, r]区间内有多少个数小于等于k 用主席树做的话查询第i小的数与k比较即可 #define lson ...
- aop aspect
所以“<aop:aspect>”实际上是定义横切逻辑,就是在连接点上做什么,“<aop:advisor>”则定义了在哪些连接点应用什么<aop:aspect>.Sp ...
- SQL Server 2012 连接到数据库引擎
第 1 课:连接到数据库引擎 https://msdn.microsoft.com/zh-cn/library/ms345332(v=sql.110).aspx 本课将介绍主要的工具以及如何连接并 ...
- SRM 587 DIV1
要掉到DVI2了..好不容这次的250那么简单,500的题知道怎么做,可惜没调出来500. 250的题很简单,从第1步到第N步,每次要么不做,要么走i步,且X不能走,问说最远走多远. #include ...
- 关于TableView中出现deallocated问题
Message sent to deallocated instance 关于的ios 开发中 deallocated问题,相信大家遇到了不少了: 关于怎么查找解决这个问题,特别是当问题在tableV ...
- cookie分析
浏览器cookie分析 简单点说就是数据存储,通过JavaScript将要保存的数据保存在客户端浏览器,下次打开网页时调用保存的cookie.浏览器中cookie保存的形式:user=aa; pwd= ...
- Descending Order
Descending Order Description: Your task is to make a function that can take any non-negative integer ...