POJ1112 Team Them Up![二分图染色 补图 01背包]
Time Limit: 1000MS | Memory Limit: 10000K | |||
Total Submissions: 7608 | Accepted: 2041 | Special Judge |
Description
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.
Input
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
Sample Input
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
3 1 3 5
2 2 4
Source
题意:
白书
一个N个节点的有向图,将节点分成两个集合,满足以下四个条件:
1。每个节点属于其中一个集合
2。每个集合至少有一个节点
3。集合里的每一个节点都有边连向同一个集合里的其他点
4。被分成的两个集合的大小要尽量接近
//更新写法
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
const int N=;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
}
int n,g[N][N];
struct edge{
int v,ne;
}e[N*N<<];
int h[N],cnt=;
void ins(int u,int v){
cnt++;
e[cnt].v=v;e[cnt].ne=h[u];h[u]=cnt;
cnt++;
e[cnt].v=u;e[cnt].ne=h[v];h[v]=cnt;
}
int col[N],cc=,tm[N][][N],p[N][];//team p
bool dfs(int u,int c){
col[u]=c;
tm[cc][c][++p[cc][c]]=u;
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v;
if(col[u]==col[v]) return false;
if(!col[v]&&!dfs(v,-c)) return false;
}
return true;
}
int w[N];
bool init(){
for(int i=;i<=n;i++) if(!col[i]){
cc++;
if(!dfs(i,)) return false;
w[cc]=p[cc][]-p[cc][];//printf("w %d %d\n",cc,w[cc]);
}
return true;
}
int f[N][N<<],pa[N][N<<]; void dp(){
f[][+n]=;
for(int i=;i<cc;i++)
for(int j=-n;j<=n;j++)
if(f[i][j+n]) f[i+][j+n+w[i+]]=f[i+][j+n-w[i+]]=;
} int t1[N],t2[N],c1,c2;
void print(int s){
for(int i=cc;i>=;i--){
int flag=;
if(f[i-][s+n-w[i]]){flag=;s-=w[i];}//the color for t1
else{flag=;s+=w[i];}
//printf("s %d\n",s);
for(int j=;j<=p[i][flag];j++) t1[++c1]=tm[i][flag][j];
flag=-flag;
for(int j=;j<=p[i][flag];j++) t2[++c2]=tm[i][flag][j];
} printf("%d ",c1);
for(int i=;i<=c1;i++) printf("%d ",t1[i]);
printf("\n%d ",c2);
for(int i=;i<=c2;i++) printf("%d ",t2[i]);
} int main(int argc, const char * argv[]) {
n=read();
for(int i=;i<=n;i++){
int v=read();
while(v!=) g[i][v]=,v=read();
}
for(int i=;i<=n;i++)
for(int j=i+;j<=n;j++)
if(g[i][j]==||g[j][i]==) ins(i,j);
if(!init()||n==) printf("No solution");
else{
dp();
for(int i=;i<=n;i++){
//printf("hi %d %d %d\n",cc,n+i,n-i);
if(f[cc][n+i]) {print(i);break;}
if(f[cc][n-i]) {print(-i);break;}
}
}
return ;
}
//普通
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
const int N=;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
}
int n,g[N][N];
struct edge{
int v,ne;
}e[N*N<<];
int h[N],cnt=;
void ins(int u,int v){
cnt++;
e[cnt].v=v;e[cnt].ne=h[u];h[u]=cnt;
cnt++;
e[cnt].v=u;e[cnt].ne=h[v];h[v]=cnt;
}
int col[N],cc=,tm[N][][N],p[N][];//team p
bool dfs(int u,int c){
col[u]=c;
tm[cc][c][++p[cc][c]]=u;
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v;
if(col[u]==col[v]) return false;
if(!col[v]&&!dfs(v,-c)) return false;
}
return true;
}
int w[N];
bool init(){
for(int i=;i<=n;i++) if(!col[i]){
cc++;
if(!dfs(i,)) return false;
w[cc]=p[cc][]-p[cc][];//printf("w %d %d\n",cc,w[cc]);
}
return true;
}
int f[N][N<<],pa[N][N<<]; void dp2(){
f[][+n]=;
for(int i=;i<=cc;i++)
for(int j=-n;j<=n;j++){
if(n+j-w[i]>=&&f[i-][n+j-w[i]]){
f[i][j+n]=;
pa[i][j+n]=;//zheng zhe fen pei
}else if(n+j+w[i]<=*n&&f[i-][n+j+w[i]]){
f[i][j+n]=;
pa[i][j+n]=-;
}
//printf("f %d %d %d\n",i,j,f[i][j]);
}
} int t1[N],t2[N],c1,c2; void print2(int s){
for(int i=cc;i>=;i--){
int flag=;
if(pa[i][s+n]==) {flag=;s-=w[i];}
else {flag=;s+=w[i];} for(int j=;j<=p[i][flag];j++) t1[++c1]=tm[i][flag][j];
flag=-flag;
for(int j=;j<=p[i][flag];j++) t2[++c2]=tm[i][flag][j];
}
printf("%d ",c1);
for(int i=;i<=c1;i++) printf("%d ",t1[i]);
printf("\n%d ",c2);
for(int i=;i<=c2;i++) printf("%d ",t2[i]);
} int main(int argc, const char * argv[]) {
n=read();
for(int i=;i<=n;i++){
int v=read();
while(v!=) g[i][v]=,v=read();
}
for(int i=;i<=n;i++)
for(int j=i+;j<=n;j++)
if(g[i][j]==||g[j][i]==) ins(i,j);
if(!init()||n==) printf("No solution");
else{
dp2(); for(int i=;i<=n;i++){
//printf("hi %d %d %d\n",cc,n+i,n-i);
if(f[cc][n+i]) {print2(i);break;}
if(f[cc][n-i]) {print2(-i);break;}
}
}
return ;
}
POJ1112 Team Them Up![二分图染色 补图 01背包]的更多相关文章
- POJ2942 Knights of the Round Table[点双连通分量|二分图染色|补图]
Knights of the Round Table Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 12439 Acce ...
- POJ 1112 Team Them Up! 二分图判定+01背包
题目链接: http://poj.org/problem?id=1112 Team Them Up! Time Limit: 1000MSMemory Limit: 10000K 问题描述 Your ...
- POJ2942 Knights of the Round Table【Tarjan点双联通分量】【二分图染色】【补图】
LINK 题目大意 有一群人,其中有一些人之间有矛盾,现在要求选出一些人形成一个环,这个环要满足如下条件: 1.人数大于1 2.总人数是奇数 3.有矛盾的人不能相邻 问有多少人不能和任何人形成任何的环 ...
- HDU 5313 Bipartite Graph(二分图染色+01背包水过)
Problem Description Soda has a bipartite graph with n vertices and m undirected edges. Now he wants ...
- 【POJ 2942】Knights of the Round Table(点双连通分量,二分图染色)
圆桌会议必须满足:奇数个人参与,相邻的不能是敌人(敌人关系是无向边). 求无论如何都不能参加会议的骑士个数.只需求哪些骑士是可以参加的. 我们求原图的补图:只要不是敌人的两个人就连边. 在补图的一个奇 ...
- POJ 2942Knights of the Round Table(tarjan求点双+二分图染色)
Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 13954 Accepted: 4673 Description Bein ...
- poj2942 点-双联通+二分图染色
题意:有一群骑士要坐在一个圆形的桌子上,他们之间有些人相互讨厌,所以不能挨着,要求算出一次也不能坐在桌子上的人,每次会议桌子必须奇数个人,一个人不能开会 题解:可以先建一个补图,要满足题目条件我们只要 ...
- [多校联考2019(Round 5 T2)]蓝精灵的请求(二分图染色+背包)
[多校联考2019(Round 5)]蓝精灵的请求(二分图染色+背包) 题面 在山的那边海的那边住着 n 个蓝精灵,这 n 个蓝精灵之间有 m 对好友关系,现在蓝精灵们想要玩一个团队竞技游戏,需要分为 ...
- LUOGU P5061 秘密任务(背包+二分图染色)
传送门 解题思路 \(orz\)出题人的神仙做法.本蒟蒻看不懂,就水个求补图再二分图染色的方法来\(%1%\)出题人. 首先我们对图中\(m\)个关系连边,发现这样是没法做的,因为我们最后要关注的是谁 ...
随机推荐
- Windows Server 2008设置远程桌面连接的最大数量
远程桌面连接的默认数量是2,当有多个用户需要同时远程桌面连接时很不方便,可以设置远程桌面连接的最大数量. 1. 运行gpedit.msc: 2. 选择计算机配置-->管理模板-->Wind ...
- ["1", "2", "3"].map(parseInt)?
["1", "2", "3"].map(parseInt)得到什么? 答案是:[1, NaN, NaN]. 原因:parseInt接收的是两 ...
- 总结常见的ES6新语法特性
前言 ES6是即将到来的新版本JavaScript语言的标准,他给我们带来了更"甜"的语法糖(一种语法,使得语言更容易理解和更具有可读性,也让我们编写代码更加简单快捷),如箭头函数 ...
- autocomplete="off" 不起作用
首先来了解一下 表单自动填充的原理,当我们登录的时候,如果选择的记住登录密码,那么后续界面中如果有 <input type="text" name="field1& ...
- html5快速入门(二)—— CSS简介
前言: 1.HTML5的发展非常迅速,可以说已经是前端开发人员的标配,在电商类型的APP中更是运用广泛,这个系列的文章是本人自己整理,尽量将开发中不常用到的剔除,将经常使用的拿出来,使需要的朋友能够真 ...
- 隐式启动判断是否有匹配的Intent
一.PackageManager的resolveActivity public abstract ResolveInfo resolveActivity(Intent intent, int flag ...
- Play Framework 完整实现一个APP(十二)
1.定制CRUD管理页面 > play crud:ov --layout 替换生成文件内容 app/views/CRUD/layout.html #{extends 'admin.html' / ...
- MS SQL 监控错误日志的告警信息
SQL Server的错误消息(Error Message)按照消息的严重级别一共划分25个等级,级别越高,表示严重性也越高.但是如果你统计sys.messages,你会发现,实际上只有16(SQL ...
- MySQL frm+ibd文件还原data的办法【数据恢复】
MySQL frm+ibd文件还原data的办法[数据恢复] 此方法只适合innodb_file_per_table = 1 当误删除ibdata 该怎么办? 如下步骤即可恢复: 1 ...
- web.xml is missing and <failOnMissingWebXml> is set to true 错误解决办法
对web项目的解决方案: 右击项目——>Java EE Tools——>Generate Deployment Descriptor Stub. 然后系统会在src/main/webapp ...