首页 技术 正文
技术 2022年11月9日
0 收藏 924 点赞 2,394 浏览 2999 个字

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

Northeastern Europe 2001

给定 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");
}

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,413
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,186
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905