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软件,装系统基本都没什么问题. ...
随机推荐
- UIkit的confirm,好看点
一,官方推荐的样码. <button type="button" class="uk-button" onclick="UIkit.modal. ...
- lintcode:Plus One 加一
题目: 加一 给定一个非负数,表示一个数字数组,在该数的基础上+1,返回一个新的数组. 该数字按照大小进行排列,最大的数在列表的最前面. 样例 给定 [1,2,3] 表示 123, 返回 [1,2,4 ...
- 机器学习第三课(EM算法和高斯混合模型)
极大似然估计,只是一种概率论在统计学的应用,它是参数估计的方法之一.说的是已知某个随机样本满足某种概率分布,但是其中具体的参数不清楚,参数估计就是通过若干次试验,观察其结果,利用结果推出参数的大概值. ...
- Bolt 动画
引擎内置的 种动画 --PosChangeAnimation 平移 local ani = XLGetObject("Xunlei.UIEngine.AnimationFactory&quo ...
- 轻量级MVC标准
看到标题,估计有人就开始想吐了,没关系,你可以先吐完再看,现在MVC框架多如牛毛,没必要再重复发明轮子了,要声明的是,这里不是想要发明轮子,也没那个闲工夫去发明轮子,而是看到这么多MVC框架模样都差不 ...
- Android新版本SDK打开旧版本项目报错解决
1.Description Resource Path Location Type Unable to resolve target 'android-19 解决:打开project.properti ...
- struts2更新版本操作有关事项备注
struts2更新版本操作有关事项备注, 更新主要jar包:struts2-convention-plugin-version,struts2-core-version, struts2-spring ...
- Support Library官方教程(3)android studio中导入支援包
Support Library Setup How you setup the Android Support Libraries in your development project depend ...
- 1450. Russian Pipelines(spfa)
1450 水题 最长路 #include <iostream> #include<cstdio> #include<cstring> #include<alg ...
- .propertie文件注释
在.properties文件中注释,前边加#就可以