Description

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.

Input

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 <= 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

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.

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 个人的认识关系有向图,将他们分为 2 组人,每组的人要相互认识,且两组人数相差尽量小。

构建新图,两个人不相互认识则连一条无向边。然后二分染色(同时记录每个联通块内有哪些点son[i][++num[i]],数量差dis[i]: 0 颜色 - 1 颜色),如果不是二分图,那么就无解。

dp[i][j]表示前i个联通块分两组后第1组-第2组=j的分法是否存在。

差值可能是负数,因此坐标平移,j=差值+100。

dp[i-1][j]==1 则 dp[i][j+dis[i]]=1,dp[i][j-dis[i]]=1;

同时记录新状态是否是由前一状态的差值-dis[i]转移来的:

path[i][j+dis[i]]=,path[i][j-dis[i]=;

dp[cnt][ d+100]==1中绝对值最小的 d 就是最小差值,然后从第cnt个联通块推回去,如果path[i][j]^color[当前联通块的点]==1说明这个点是第1组的,因为path[i][j]==1代表第i个联通块的0颜色给第一组。异或为1,则path为0,color为1或者path为1,color为0。

path[i][j]为1,则第 i-1 个联通块时的差值就是当前差值+dis[i]。否则-dis[i]。

#include <cstdio>
#define N 205
#define sf(a) scanf("%d",&a)
using namespace std;
int n,g[N][N],gg[N][N];//原图和新图
int color[N];//染色
int dis[N];//一个联通块里0色-1色的数量差
int cnt,son[N][N];//储存1~cnt联通块的节点
int num[N];//1~cnt联通块的个数
int dp[N][N],path[N][N];//dp和路径记录
int tot[],team[][N];//记录两个队的人数和队员
int cl[];//0色和1色的数量
int dfs(int x,int c){//将x染上c色(0,1)
color[x]=c;
for(int i=;i<=n;i++)if(gg[x][i]&&i!=x){
if(color[i]==c)return ;
if(color[i]==-&&!dfs(i,!c))return ;
}
cl[c]++;
son[cnt][++num[cnt]]=x;
return ;
}
int check(){
for(int i=;i<=n;i++)if(color[i]==-){
cnt++;
cl[]=cl[]=;
if(!dfs(i,))return ;
dis[cnt]=cl[]-cl[];
}
return ;
}
void solve(){
dp[][]=;
for(int i=;i<=cnt;i++)
for(int j=;j<=;j++)if(dp[i-][j]){
dp[i][j+dis[i]]=;
path[i][j+dis[i]]=;
if(j>dis[i]){
dp[i][j-dis[i]]=;
path[i][j-dis[i]]=;
}
}
int d=;
for(;d<&&!(dp[cnt][d+]||dp[cnt][-d+]);d++);
if(dp[cnt][d+])d+=;else d=-d;
for(int i=cnt;i;i--){
for(int j=;j<=num[i];j++){
if(path[i][d]^color[j])team[][++tot[]]=son[i][j];
else team[][++tot[]]=son[i][j];
}
if(path[i][d]) d-=dis[i];
else d+=dis[i];
}
for(int j=;j<;j++)
{
printf("%d ",tot[j]);
for(int i=;i<=tot[j];i++)
printf("%d ",team[j][i]);
puts("");
}
}
int main() {
sf(n);
for(int i=;i<=n;i++){
int x;
while(sf(x),x)
g[i][x]=;
color[i]=-;
}
for(int i=;i<=n;i++)
for(int j=i+;j<=n;j++)
if(!g[i][j]||!g[j][i])
gg[i][j]=gg[j][i]=;
if(check()) solve();
else puts("No solution");
}

  

【POJ 1112】Team Them Up!(二分图染色+DP)的更多相关文章

  1. POJ 1112 Team Them Up! 二分图判定+01背包

    题目链接: http://poj.org/problem?id=1112 Team Them Up! Time Limit: 1000MSMemory Limit: 10000K 问题描述 Your ...

  2. 图论+dp poj 1112 Team Them Up!

    题目链接: http://poj.org/problem?id=1112 题目大意: 有编号为1~n的n个人,给出每个人认识的人的编号,注意A认识B,B不一定认识A,让你将所有的人分成两组,要求每组的 ...

  3. POJ1112 Team Them Up![二分图染色 补图 01背包]

    Team Them Up! Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7608   Accepted: 2041   S ...

  4. poj-1112 (二分图染色+dp分组)

    #include <iostream> #include <algorithm> #include <cstring> using namespace std; ; ...

  5. NOIP2008双栈排序[二分图染色|栈|DP]

    题目描述 Tom最近在研究一个有趣的排序问题.如图所示,通过2个栈S1和S2,Tom希望借助以下4种操作实现将输入序列升序排序. 操作a 如果输入序列不为空,将第一个元素压入栈S1 操作b 如果栈S1 ...

  6. 【POJ 2942】Knights of the Round Table(点双连通分量,二分图染色)

    圆桌会议必须满足:奇数个人参与,相邻的不能是敌人(敌人关系是无向边). 求无论如何都不能参加会议的骑士个数.只需求哪些骑士是可以参加的. 我们求原图的补图:只要不是敌人的两个人就连边. 在补图的一个奇 ...

  7. POJ 2942Knights of the Round Table(tarjan求点双+二分图染色)

    Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 13954   Accepted: 4673 Description Bein ...

  8. POJ2942 Knights of the Round Table【Tarjan点双联通分量】【二分图染色】【补图】

    LINK 题目大意 有一群人,其中有一些人之间有矛盾,现在要求选出一些人形成一个环,这个环要满足如下条件: 1.人数大于1 2.总人数是奇数 3.有矛盾的人不能相邻 问有多少人不能和任何人形成任何的环 ...

  9. 洛谷P1330封锁阳光大学[二分图染色]

    题目描述 曹是一只爱刷街的老曹,暑假期间,他每天都欢快地在阳光大学的校园里刷街.河蟹看到欢快的曹,感到不爽.河蟹决定封锁阳光大学,不让曹刷街. 阳光大学的校园是一张由N个点构成的无向图,N个点之间由M ...

随机推荐

  1. javascript特效——烟花燃放的效果[xyytit]

    春节临近,要做活动促销专题页面,百度了下,找到一些烟花燃放的特效,整理并添加了修改烟花各种参数的注释,便于大家修改使用,版权归原作者所有. 1. 示例效果:点击这里   下载源码:点击这里 2. Ht ...

  2. Android RecyclerView 动画展开item显示详情

    stackoverflow上看到这个问题,答主给了个demo http://stackoverflow.com/questions/27446051/recyclerview-animate-item ...

  3. 为什么mysql设置了密码之后,本地还可以直接访问,不需要输入密码就可以登录数据库了?

    应为数据库里面有空用户 select * from mysql.user where user=''; 查询如果有,把他删了然后重启mysql服务. 他有空用户你删除了 然后重启mysql生效,这个是 ...

  4. hadoop怎么读?怎么发音

    hadoop不是一个英文单词,是作者发明的词,hadoop名称来源作者小孩的一个}h毛填充黄色大象玩具. 它的发音是:[hædu:p]

  5. Meet Python: little notes 3 - function

    Source: http://www.liaoxuefeng.com/ ♥ Function In python, name of a function could be assigned to a ...

  6. js 事件冒泡是什么如何用jquery阻止事件冒泡

    什么是事件起泡:一个事件不能凭空产生,这就是事件的发生等等,接下来为大家介绍下jquery阻止事件起泡以及关于js事件起泡的验证,感兴趣的朋友可以参考下哈       (1)什么是事件起泡 首先你要明 ...

  7. BZOJ 1016 【JSOI2008】 最小生成树计数

    Description 现在给出了一个简单无向加权图.你不满足于求出这个图的最小生成树,而希望知道这个图中有多少个不同的最小生成树.(如果两颗最小生成树中至少有一条边不同,则这两个最小生成树就是不同的 ...

  8. [Azure] 使用 Azure 快速搭建 Redis 服务器

    Redis相信玩开源,大数据的朋友们并不陌生,大家最熟悉的使用者就是新浪微博,微博的整体数据缓存都是基于Redis的,而新浪对Redis的使用也非常深,据说是一组64G内存的Redis集群.前段时间我 ...

  9. [LINK]php的三种CLI常量:STDIN,STDOUT,STDERR

    FROM : http://www.cnblogs.com/thinksasa/archive/2013/02/27/2935158.html PHP CLI(command line interfa ...

  10. Ztree 随笔记

    Ztree方法是一个单体对象方法,假如一个页面有多个的话,每申明一个新对象前面的Ztree对象就要被覆盖. 解决:在生成tree控件的时候就要设置其控件ID,然后通过$.fn.zTree.getZTr ...