题意:

给出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!的更多相关文章

  1. 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 ...

  2. Create a Team in RHEL7

    SOLUTION VERIFIED September 13 2016 KB2620131 Environment Red Hat Enterprise Linux 7 NetworkManager ...

  3. Team Leader 你不再只是编码, 来炖一锅石头汤吧

    h3{ color: #000; padding: 5px; margin-bottom: 10px; font-weight: bolder; background-color: #ccc; } h ...

  4. 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 ...

  5. BZOJ 4742: [Usaco2016 Dec]Team Building

    4742: [Usaco2016 Dec]Team Building Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 21  Solved: 16[Su ...

  6. 关于 feature team 的一些内容

    矩阵式管理,是常见的经典管理架构.其最早起源于美国的航空航天部门,然后被美国人带到了日本,然后被日本人带到了台湾,然后台湾人带到大陆...矩阵管理最典型的特征是,组织架构按职能与专业划分,项目由跨越部 ...

  7. 病毒四度升级:安天AVL Team揭露一例跨期两年的电信诈骗进化史

    自2014年9月起,安天AVL移动安全团队持续检测到一类基于Android移动平台的间谍类病毒,病毒样本大多伪装成名为"最高人民检察院"的应用.经过反编译逆向分析以及长期的跟踪调查 ...

  8. iOS 真机测试时报错:Provisioning profile "iOS Team Provisioning Profile: XXX” doesn't include the currently selected device “XXX”.

    这几天因工作需要,去给客户演示iOS项目打包的过程.之前演示都是顺利的,但后来客户自己操作时打电话说遇到了问题,出现报错. 就过去看了一下,发现一个很陌生的错误提示: The operation co ...

  9. 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 ...

  10. 测试环境搭建心得 vs2008+SQL2008 PHP+APACHE+mysql Team Foundation Server2013

    大四即将结束,大学的最后一个假期,找到一份实习工作,担任测试工程师.在过年前的最后一周入职,干了一周的活儿.主要工作就是搭建测试环境. VMware 主要熟悉VMware软件,装系统基本都没什么问题. ...

随机推荐

  1. hdu 1452 Happy 2004

    因子和: 的因子是1,2,3,6; 6的因子和是 s(6)=1+2+3+6=12; 的因子是1,2,4,5,10,20; 20的因子和是 s(20)=1+2+4+5+10+20=42; 的因子是1,2 ...

  2. Python新式类和旧式类的区别

    新式类是为了统一**而在2.2中开始引入的. 代码讲解 上面的例子比较明白的说明了问题. B是定义的新式类.那么输入b的时候,不论是type(b),还是b.__class__都是输出的<clas ...

  3. 52. N-Queens II

    题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...

  4. swift:自动引用计数ARC

    Swift自动引用计数:ARC    原文链接:https://numbbbbb.gitbooks.io/-the-swift-programming-language-/content/chapte ...

  5. Ubuntu 安装mod_python配置Apache2

    在Ubuntu上搭建Python运行环境,mod_python是不可少的(据说mod_swgi也是可以的,没有亲测).使用命令安装mod_python. 安装: apt-get install lib ...

  6. android的helloworld工程目录学习

    android的helloworld工程目录学习 Android工程的主要目录有src.gen.Android X.X.bin.res等文件夹. 1.     Src文件夹 Src文件夹包含java源 ...

  7. BZOJ 1923 外星千足虫(高斯消元)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1923 题意:有n个数字,m次测试.每个数字为0或者1.每次测试选出一些数字出来把他们加起 ...

  8. 简单易懂的命名空间及use的使用

    最近一段时间在研究php框架,一直想的什么时候才能开发出自己的框架,当然这是为了提升自己的编程水平,同时能把平时学的零散的东西糅合在一块熟练应用.但是开发一个框架根本不知道如何做起,先开发什么,虽然p ...

  9. poj 3277 City Horizon (线段树 扫描线 矩形面积并)

    题目链接 题意: 给一些矩形,给出长和高,其中长是用区间的形式给出的,有些区间有重叠,最后求所有矩形的面积. 分析: 给的区间的范围很大,所以需要离散化,还需要把y坐标去重,不过我试了一下不去重 也不 ...

  10. android中给TextView或者Button的文字添加阴影效果

    1在代码中添加文字阴影 TextView 有一个方法 /**      * Gives the text a shadow of the specified radius and color, the ...