POJ 1112 Team Them Up! 二分图判定+01背包
题目链接:
http://poj.org/problem?id=1112
Team Them Up!
Time Limit: 1000MSMemory Limit: 10000K
#### 问题描述
> 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.
#### 输入
> 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 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.
####样例输入
> 5
> 2 3 5 0
> 1 4 5 3 0
> 1 2 5 0
> 1 2 3 0
> 4 3 2 1 0
样例输出
3 1 3 5
2 2 4
题意
给你若干个人,要把他们分成两组,其中同一组内要保证任意两个人互相认识(题目给的边都是单向边),求一个使得两组人数最接近的方案,要求输出一个可行分组方案。
题解
首先,把关系图建处理(对于(u,v)如果不存在(v,u)相当于这条边不存在),处理出补图,然后题目就转换成了一个二分图问题了,跑下黑白染色,判断是否有可行解,如果有,则用dp跑下,求出最优解就ok啦。dp[i][j]表示已经处理了i个联通分量(跑黑白染色会处理出若干个联通分量),其中一组能凑出j个人的方案。
代码
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#include<sstream>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;
const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0);
//start----------------------------------------------------------------------
const int maxn=111;
int n;
bool gra[maxn][maxn];
bool dp[maxn][maxn];
VI lis[maxn][3];
int color[maxn],tot;
//黑白染色
bool dfs(int u) {
for(int v=1; v<=n; v++) {
if(!gra[u][v]) continue;
if(!color[v]) {
color[v]=3-color[u];
lis[tot][color[v]].pb(v);
bool su=dfs(v);
if(!su) return false;
} else {
if(color[v]==color[u]) return false;
}
}
return true;
}
VI ans[3];
bool used[maxn];
//处理出最优方案
void print(int i,int j) {
if(i==0) {
return;
}
int a=lis[i][1].sz(),b=lis[i][2].sz();
if(j>=a&&dp[i-1][j-a]) {
print(i-1,j-a);
rep(ii,0,lis[i][1].sz()) {
ans[1].pb(lis[i][1][ii]);
}
} else if(j>=b&&dp[i-1][j-b]) {
print(i-1,j-b);
rep(ii,0,lis[i][2].sz()) {
ans[1].pb(lis[i][2][ii]);
}
}
}
int main() {
while(scf("%d",&n)==1&&n) {
clr(gra,0);
for(int i=1; i<=n; i++) {
int v;
while(scf("%d",&v)==1&&v) {
gra[i][v]=1;
}
}
for(int i=1; i<=n; i++) {
for(int j=1; j<i; j++) {
gra[i][j]=gra[j][i]=!(gra[i][j]&&gra[j][i]);
}
}
//黑白染色
clr(color,0);
rep(i,0,maxn) lis[i][1].clear(),lis[i][2].clear();
tot=0;
bool su=1;
for(int i=1; i<=n; i++) {
if(!color[i]) {
++tot;
color[i]=1;
lis[tot][1].pb(i);
su=dfs(i);
}
if(!su) break;
}
if(!su) {
prf("No solution\n");
continue;
}
//01背包,不是选和不选,而是选1还是选2.
clr(dp,0);
dp[0][0]=1;
for(int i=1; i<=tot; i++) {
int a=lis[i][1].sz(),b=lis[i][2].sz();
for(int j=0; j<=100; j++) {
if(j>=a&&j>=b) {
dp[i][j]=dp[i-1][j-a]|dp[i-1][j-b];
} else if(j>=a) {
dp[i][j]=dp[i-1][j-a];
} else if(j>=b) {
dp[i][j]=dp[i-1][j-b];
}
}
}
//输出最优方案
int Mi=INF,pos=-1;
for(int i=1; i<=100; i++) {
if(dp[tot][i]) {
if(Mi>abs(i-(n-i))) {
Mi=abs(i-(n-i));
pos=i;
}
}
}
ans[1].clear(),ans[2].clear();
clr(used,0);
print(tot,pos);
rep(i,0,ans[1].sz()) used[ans[1][i]]=1;
for(int i=1; i<=n; i++) {
if(!used[i]) ans[2].pb(i);
}
sort(all(ans[1]));
sort(all(ans[2]));
for(int i=1; i<=2; i++) {
prf("%d ",ans[i].sz());
rep(j,0,ans[i].sz()) prf("%d%c",ans[i][j],j==ans[i].sz()-1?'\n':' ');
}
}
return 0;
}
//end-----------------------------------------------------------------------
/*
3
2 0
1 3 0
2 0
*/
POJ 1112 Team Them Up! 二分图判定+01背包的更多相关文章
- HDU 5313 Bipartite Graph(二分图染色+01背包水过)
Problem Description Soda has a bipartite graph with n vertices and m undirected edges. Now he wants ...
- DP:Cow Exhibition(POJ 2184)(二维问题转01背包)
牛的展览会 题目大意:Bessie要选一些牛参加展览,这些牛有两个属性,funness和smartness,现在要你求出怎么选,可以使所有牛的smartness和funness的最大,并且这两 ...
- POJ 2923 Relocation (状态压缩,01背包)
题意:有n个(n<=10)物品,两辆车,装载量为c1和c2,每次两辆车可以运一些物品,一起走.但每辆车物品的总重量不能超过该车的容量.问最少要几次运完. 思路:由于n较小,可以用状态压缩来求解. ...
- poj 2184 Cow Exhibition(dp之01背包变形)
Description "Fat and docile, big and dumb, they look so stupid, they aren't much fun..." - ...
- 图论+dp poj 1112 Team Them Up!
题目链接: http://poj.org/problem?id=1112 题目大意: 有编号为1~n的n个人,给出每个人认识的人的编号,注意A认识B,B不一定认识A,让你将所有的人分成两组,要求每组的 ...
- POJ 2923 Relocation(状压DP+01背包)题解
题意:给你汽车容积c1,c2,再给你n个包裹的体积,问你最少运几次能全运走 思路:用2进制表示每次运送时某物在不在此次运送之中,1在0不在.我们把运送次数抽象成物品价值,把状态抽象成体积,用一个dp[ ...
- POJ 2184 Cow Exhibition 奶牛展(01背包,变形)
题意:有只奶牛要证明奶牛不笨,所以要带一些奶牛伙伴去证明自己.牛有智商和幽默感,两者可为负的(难在这),要求所有牛的智商和之 / 幽默感之和都不为负.求两者之和的最大值. 思路:每只牛可以带或不带上, ...
- POJ 1948 Triangular Pastures【二维01背包】
题意:给出n条边,用这n条边构成一个三角形,求三角形的最大面积. 先求面积,用海伦公式,s=sqrt(p*(p-a)*(p-b)*(p-c)),其中a,b,c分别为三角形的三条边,p为三角形的半周长, ...
- POJ 3624 Charm Bracelet(01背包裸题)
Charm Bracelet Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 38909 Accepted: 16862 ...
随机推荐
- 小米路由器设置端口转发远程登录WEB管理页及安装MT工具箱
1. 将小米路由器ROM升级到开发版 这一点是必须的,如果是稳定版是不行的 2. 获取高级管理权限 再次确认当前使用的是开发版ROM 到这个网址http://d.miwifi.com/rom/ssh ...
- 7.Classes-类(Dart中文文档)
Dart是一个面向对象的语言,同时增加了混入(mixin)继承的特性.对象都是由类初始化生成的,所有的类都由Object对象继承.混入继承意味着尽管所有类(除了Object类)只有一个父类,但是类的代 ...
- 20155236范晨歌 Exp2后门原理与实践
## 实验二 后门原理与实践 1.Windows获得Linux Shell 在windows下,打开CMD,使用ipconfig指令查看本机IP ncat.exe -l -p 5236监听本机的523 ...
- Scikit-Learn机器学习入门
现在最常用的数据分析的编程语言为R和Python.每种语言都有自己的特点,Python因为Scikit-Learn库赢得了优势.Scikit-Learn有完整的文档,并实现很多机器学习算法,而每种算法 ...
- 使用ASP.NET Identity以手机短信实现双重验证
这篇文章将展示怎么使用SMS短信启动双重验证 创建一个ASP.NET 5项目 一开始,使用Visual studio 2015创建一个新的ASP.NET Web应用程序: 在下一步中选择ASP.NET ...
- C语言动态内存的申请和释放
什么是动态内存的申请和释放? 当程序运行到需要一个动态分配的变量时,必须向系统申请取得堆中的一块所需大小的存储空间,用于存储该变量.当不再使用该变量时,也就是它的生命结束时,要显式释放它所占用的存储空 ...
- 【敏捷】7.showcase,开发中必须引起重视的小环节
有人说,测试者来自火星,开发者来自金星.这是因为软件测试员和软件开发者就好比一对冤家,里面的缘由说不清也道不明.开发代表着创造,而测试则代表着摧毁,因为测试的目的就是以各种方式不断地从开发出的产品中发 ...
- 「专题训练」Collecting Bugs(POJ-2096)
题意与分析 题意大致是这样的:给定一个\(n\times s\)的矩阵,每次可以随机的在这个矩阵内给一个格子染色(染过色的仍然可能被选中),问每一行和每一列都有格子被染色的次数的期望. 这题如果从概率 ...
- Xming 多屏选项
最早接触xming是从GrADS软件弹出的那个窗口开始的.到后来发现它是一个显示图形界面的软件,设置multiwindow 选项,xshell+xming连远程服务器,屡试不爽.随着设备升级,用上了双 ...
- SQL常见面试题
1.用一条SQL 语句 查询出每门课都大于80 分的学生姓名 name kecheng fenshu张三 语文 81张三 数学 75李四 语文 ...