POJ1112 Team Them Up!
Team them up!
| Input file | teams.in |
| Output file | teams.out |
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 solution does not exist.
Input
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
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 the team in any order.
Sample input #1
5
3 4 5 0
1 3 5 0
2 1 4 5 0
2 3 5 0
1 2 3 4 0
Output for the sample input #1
No solution
Sample input #2
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 for the sample input #2
3 1 3 5
2 2 4
简单地说,就是,一个N个节点的有向图,将节点分成两个集合,满足以下四个条件:
1. 每个节点属于其中一个集合
2. 每个集合至少有一个节点
3. 集合里的每一个节点都有边连向同一个集合里的其他点
4. 被分成的两个集合的大小要尽量接近
如果不能满足上述条件,输出 No solution ,否则输出这两个集合
题解
参照ylfdrib的题解。
首先,求原图的补图,同时把有向图转化为无向图,即:对于节点u, v,如果没有边<u, v>或<v, u>,则建无向边(u, v)
分析一下现在这个图,如果u, v相连,表明u, v不能在同一个集合里,对于这个问题我们就有了感觉,先求图的连通分量,对于同一个连通分量,我们用二分图着色法,把整个连通分量里的点分到两个集合里,当然,如果着色失败,则无解,输出 No solution ,否则,这m个连通分量就构成了m个集合对(xi, yi),xi表示第i个连通分量中着色为0的集合,yi表示第i个连通分量中着色为1的集合,这样问题就转化成:对这m个集合对,每对里选出一个集合,构成A集合,剩余的构成B集合,要求A,B大小尽量接近,这样我们就可以用动态规划来搞定了。
DP方程:dp[i][j] = (dp[i][j - cnt[i][0]] | dp[i][j - cnt[i][1]] ) (1 <= i <= scc, 0 <= j <= N / 2)
dp[i][j] = 1 表示前i个连通分支,可以构成符合要求的节点数为j的集合
这里暂且用scc表示连通分支个数,N表示总节点个数,cnt[i][0]表示第i个分支中被着色成0的节点个数,cnt[i][1]表示第i个分支中被着色成1的节点个数,同时记录dp路径,这样这道题就算彻底搞定了
#include<iostream>
#include<cstring>
#include<vector>
#define rg register
#define il inline
#define co const
template<class T>il T read(){
rg T data=0,w=1;rg char ch=getchar();
for(;!isdigit(ch);ch=getchar())if(ch=='-') w=-w;
for(;isdigit(ch);ch=getchar()) data=data*10+ch-'0';
return data*w;
}
template<class T>il T read(rg T&x) {return x=read<T>();}
typedef long long ll;
using namespace std;
co int N=201;
int n,a[N],c[N],num[2],cnt,f[N][N],g[N][N];
pair<int,int> p[N];
#define x first
#define y second
vector<int> e[N],d[N][2],ans[2];
bool dfs(int x,int color){
c[x]=color,++num[color],d[cnt][color].push_back(x);
for(unsigned i=0;i<e[x].size();++i){
int y=e[x][i];
if(c[y]==-1){
if(!dfs(y,color^1)) return 0;
}
else if(c[y]==color) return 0;
}
return 1;
}
void print(int k,int x){
if(!k) return;
int t=g[k][x];
for(int i=0;i<2;++i)
for(unsigned j=0;j<d[k][i].size();++j)
ans[i^t].push_back(d[k][i][j]);
print(k-1,x+d[k][t].size()-d[k][t^1].size());
}
int main(){
read(n);
memset(c,-1,sizeof c);
for(int i=1;i<=n;++i){
memset(a,0,sizeof a);
for(int x;read(x);) a[x]=1;
for(int j=1;j<=n;++j)if(j!=i&&!a[j])
e[i].push_back(j),e[j].push_back(i);
}
for(int i=1;i<=n;++i)if(c[i]==-1){
num[0]=num[1]=0,++cnt;
if(!dfs(i,0)) return puts("No solution"),0;
p[cnt]=make_pair(num[0],num[1]);
}
f[0][100]=1;
for(int i=1;i<=cnt;++i)
for(int j=0;j<=200;++j)if(f[i-1][j]){
int x=p[i].x,y=p[i].y;
f[i][j+x-y]=1,g[i][j+x-y]=1;
f[i][j+y-x]=1,g[i][j+y-x]=0;
}
for(int i=0;i<=100;++i)
if(f[cnt][100-i]) {print(cnt,i+100);break;}
printf("%llu ",ans[0].size());
for(unsigned i=0;i<ans[0].size();++i) printf("%d ",ans[0][i]);
printf("\n%llu ",ans[1].size());
for(unsigned i=0;i<ans[1].size();++i) printf("%d ",ans[1][i]);
return 0;
}
POJ1112 Team Them Up!的更多相关文章
- POJ1112 Team Them Up![二分图染色 补图 01背包]
Team Them Up! Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7608 Accepted: 2041 S ...
- OUC_OptKernel_oshixiaoxiliu_好题推荐
poj1112 Team Them Up! 补图二分图+dp记录路径codeforces 256A Almost Arithmetical Progression dp或暴力 dp[i][j] = d ...
- 【poj1112】 Team Them Up!
http://poj.org/problem?id=1112 (题目链接) 题意 将n个人分成两组,每个人有认识的人,要求每一组中的人互相认识,并且两组人数之差尽可能的小,求如何分. Solution ...
- Configure a VLAN on top of a team with NetworkManager (nmcli) in RHEL7
SOLUTION VERIFIED September 13 2016 KB1248793 Environment Red Hat Enterprise Linux 7 NetworkManager ...
- Create a Team in RHEL7
SOLUTION VERIFIED September 13 2016 KB2620131 Environment Red Hat Enterprise Linux 7 NetworkManager ...
- Team Leader 你不再只是编码, 来炖一锅石头汤吧
h3{ color: #000; padding: 5px; margin-bottom: 10px; font-weight: bolder; background-color: #ccc; } h ...
- Configure bridge on a team interface using NetworkManager in RHEL 7
SOLUTION IN PROGRESS February 29 2016 KB2181361 environment Red Hat Enterprise Linux 7 Teaming,Bridg ...
- BZOJ 4742: [Usaco2016 Dec]Team Building
4742: [Usaco2016 Dec]Team Building Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 21 Solved: 16[Su ...
- 关于 feature team 的一些内容
矩阵式管理,是常见的经典管理架构.其最早起源于美国的航空航天部门,然后被美国人带到了日本,然后被日本人带到了台湾,然后台湾人带到大陆...矩阵管理最典型的特征是,组织架构按职能与专业划分,项目由跨越部 ...
随机推荐
- php递归删除所有文件
function del_file($dir) { if (@rmdir($dir)==false && is_dir($dir)) { if ($dp = opendir($dir) ...
- java_Ninja实战过程
使用Ninja马上两年了,之前多多少少的都是跟着项目模仿着写,今年上半年准备从一个小项目开始从始至终走一遍; 首先官网:http://www.ninjaframework.org; github: h ...
- 关于js sort排序方法
sort() 方法用于对数组的元素进行排序. 语法:arrayObject.sort(sortby):参数sortby可选.规定排序顺序.必须是函数. 当方法不带参数的时候,将按照字符编码顺序进行排序 ...
- JVM虚拟机调参
一.堆大小设置JVM 中最大堆大小有三方面限制:相关操作系统的数据模型(32-bt还是64-bit)限制:系统的可用虚拟内存限制:系统的可用物理内存限制.32位系统下,一般限制在1.5G~2G:64为 ...
- Django下MEDIA_ROOT, MEDIA_URL, STATIC_ROOT, STATIC_URL解惑
Django中settings中的四个设置参数的一些故事: MEDIA_ROOT, MEDIA_URL, STATIC_ROOT, STATIC_URL 1.MEDIA_ROOT与MEDIA_URL ...
- AJAX请求时status返回状态明细表
AJAX请求时status返回状态明细表 readyState的五种状态2010-03-04 18:24对于readyState的五种状态的描述或者说定义,很多Ajax书(英文原版)中大都语焉不详 在 ...
- C语言伪随机数的注意事项
不要将srand(time(NULL))或srand(time(0))放到循环中,因为我们两次调用srand()函数设置随机数种子之间的时间间隔不超过1s,等价于使用了一个固定的随机数种子,会出现相同 ...
- Linux电源管理(2)-Generic PM基本概念和软件架构【转】
本文转载自:http://www.wowotech.net/pm_subsystem/generic_pm_architecture.html 1. 前言 这里的Generic PM,是蜗蜗自己起的名 ...
- 【反思】一个价值两天的BUG,无论工作还是学习C语言的朋友都看看吧!
博文原创,转载请联系博主! 使用C语言也有两个年头了,BUG写出来过不少,也改过不少BUG.但是偏偏就是有这么一个BUG让我手头的项目停工了两天,原因从百度找到谷歌,资料从MAN手册找到RFC也没有找 ...
- Mac系统给移动硬盘分区(图文)
刚买的硬盘500G 准备分几个区 移动硬盘分区格式化有3中形式: 1.Mac OS 扩展日志 格式 此格式mac专用,这种格式的硬盘在PC上不可见,可以用来给 Time Machine 备份, T ...