Team them up!
题意:
给出n个人以及认识其他人的情况,现在要把所有人分成两队,每队至少一人,求使两队人数差距最小且每队内部的人都相互认识的分队情况。
分析:
这道题让我学习到了不少,首先看到使差距最小就想到了背包,但是不会表示分队情况。看了别人的思路,这个很明显是要判断是否是二分图,让不是相互认识的两人连一条边,若不是二分图则不能分成两队。利用染色法判断二分图,则每个连通分量都包含0,1两个集合。
dp[i][j]表示i个联通分量能组成符合条件的人数为j的一队,par[i][j]记录路径
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define N 110
#define read freopen("in.txt", "r", stdin)
const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod = ;
int n,tim,num;
int dp[N][N],par[N][N],mapc[N][N],part[N][];
int color[N],dfn[N],f[N],road[N][N][];
vector<int>e[N];
int dfs(int u){
dfn[u]=++tim;
road[num][++part[num][color[u]]][color[u]]=u;
for(int i=;i<e[u].size();++i){
int v=e[u][i];
if(dfn[v]==){
color[v]=(!color[u]);
if(dfs(v)==)return ;
}
else{
if(v!=u&&color[u]==color[v])return ;
}
}
return ;
}
void hand(){
int v;
for(v=n/;v>=;--v){
if(dp[num][v])
break;
}
if(v==){printf("No solution\n");return;}
memset(f,,sizeof(f));
int tmp=v,t;
for(int i=num;i>=;--i){
t=par[i][tmp];
tmp-=part[i][t];
for(int j=;j<=part[i][t];++j)
f[road[i][j][t]]=;
}
printf("%d",v);
for(int i=;i<=n;++i)
if(f[i])
printf(" %d",i);
printf("\n");
printf("%d",n-v);
for(int i=;i<=n;++i)
if(!f[i])
printf(" %d",i);
printf("\n");
}
void solve(){
memset(part,,sizeof(part));
memset(color,,sizeof(color));
memset(dfn,,sizeof(dfn));
memset(dp,,sizeof(dp));
tim=num=;
int k;
for(k=;k<=n;++k){
if(!dfn[k]){
++num;
if(dfs(k)==)break;
}
}
if(k<=n)printf("No solution\n");
else{
dp[][]=;
for(int i=;i<=num;++i)
for(int j=;j<=n/;++j){
int tmp=j-part[i][];
if(tmp>=&&dp[i-][tmp]){
dp[i][j]=;
par[i][j]=;
}
tmp=j-part[i][];
if(tmp>=&&dp[i-][tmp])
{
dp[i][j]=;
par[i][j]=;
}
}
hand();
}
}
int main()
{
int ca=;
scanf("%d",&ca);
while(ca--){
scanf("%d",&n);
int a;
memset(mapc,,sizeof(mapc));
for(int i=;i<=n;++i)
e[i].clear();
for(int i=;i<=n;++i){
while(~scanf("%d",&a)&&a){
mapc[i][a]=;
}
}
for(int i=;i<=n;++i)
for(int j=i+;j<=n;++j)
if(mapc[i][j]==||mapc[j][i]==){
e[i].push_back(j);
e[j].push_back(i);
}
solve();
if(ca)printf("\n");
}
return ;
}
Team them up!的更多相关文章
- 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 的一些内容
矩阵式管理,是常见的经典管理架构.其最早起源于美国的航空航天部门,然后被美国人带到了日本,然后被日本人带到了台湾,然后台湾人带到大陆...矩阵管理最典型的特征是,组织架构按职能与专业划分,项目由跨越部 ...
- 病毒四度升级:安天AVL Team揭露一例跨期两年的电信诈骗进化史
自2014年9月起,安天AVL移动安全团队持续检测到一类基于Android移动平台的间谍类病毒,病毒样本大多伪装成名为"最高人民检察院"的应用.经过反编译逆向分析以及长期的跟踪调查 ...
- iOS 真机测试时报错:Provisioning profile "iOS Team Provisioning Profile: XXX” doesn't include the currently selected device “XXX”.
这几天因工作需要,去给客户演示iOS项目打包的过程.之前演示都是顺利的,但后来客户自己操作时打电话说遇到了问题,出现报错. 就过去看了一下,发现一个很陌生的错误提示: The operation co ...
- diff/merge configuration in Team Foundation - common Command and Argument values - MSDN Blogs
One of the extensibility points we have in Team Foundation V1 is that you can configure any other di ...
- 测试环境搭建心得 vs2008+SQL2008 PHP+APACHE+mysql Team Foundation Server2013
大四即将结束,大学的最后一个假期,找到一份实习工作,担任测试工程师.在过年前的最后一周入职,干了一周的活儿.主要工作就是搭建测试环境. VMware 主要熟悉VMware软件,装系统基本都没什么问题. ...
随机推荐
- ubuntu安装google 输入法
12.04 LTS Precise sudo apt-get install ibus ibus-clutter ibus-gtk ibus-gtk3 ibus-qt4 sudo apt-get in ...
- 随机森林分类(Random Forest Classification)
其实,之前就接触过随机森林,但仅仅是用来做分类和回归.最近,因为要实现一个idea,想到用随机森林做ensemble learning才具体的来看其理论知识.随机森林主要是用到决策树的理论,也就是用决 ...
- XML DOS 攻击
内容来自此文:http://msdn.microsoft.com/en-us/magazine/ee335713.aspx DoS(Denial of service:拒绝服务)攻击由来已久(1992 ...
- POJ1118 Lining Up
快弄死我了 最后的原因是abs和fabs的区别... 说点收获:1.cmp函数返回的是int,所以不要直接返回double相减的结果2.define inf 1e9和eps 1e-93.在整数相除得到 ...
- BCB一个问过100遍啊100遍的问题
一个问过100遍啊100遍的问题作者: ---------- ,如转载请保证本文档的完整性,并注明出处.欢迎光临 C++ Builder 研究, http://www.ccrun.com/doc/go ...
- Larave 多图片上传
首先使用Dropzone.js前端组件 {{ HTML::style('css/basic.css');}} {{ HTML::script('js/dropzone.js') }} form部分只需 ...
- python mysql 简单总结(MySQLdb模块 需另外下载)
python 通过DB-API规范了它所支持的不同的数据库,使得不同的数据库可以使用统一的接口来访问和操作. 满足DB-API规范的的模块必须提供以下属性: 属性名 描述 apilevel DB-AP ...
- FastDFS_v5.05安装配置
废话不多讲,启动FastDFS文件服务器的命令是 #/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf #/usr/bin/fdfs_storaged /etc ...
- 离线应用Application Cache详解
特点: 1.离线可访问 - 没有网络仍可访问整个应用 2.很小的服务器负载 - 缓存在本地,不需要到服务器请求 3.高速 ...
- PHP程序员最常犯的11个MySQL错误
对于大多数web应用来说,数据库都是一个十分基础性的部分.如果你在使用PHP,那么你很可能也在使用MySQL—LAMP系列中举足轻重的一份子. 对于很多新手们来说,使用PHP可以在短短几个小时之内轻松 ...