您的位置:首页 > 其它

POJ 1112 Team Them Up! (二分图染色+连通分量+DP)

2013-01-09 11:28 387 查看
Team Them Up!

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 5530 Accepted: 1471 Special Judge
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

这题写得着实蛋碎,不看题解真心不会写,搞图论太折寿了T.T......具体题解就不说了,可以去这个大牛微博上看http://www.cppblog.com/linyangfei/archive/2008/08/08/58295.html

我只做些自己代码的注释

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=222;
bool map[maxn][maxn];
int n,m,connect_num,color[maxn];//connect_num代表联通分量的个数,color[]代表染色情况
bool dp[maxn][2*maxn];//DP数组,dp[i][j]表示第i个连通分量能否达到两队人数差值j
int team[2][maxn];//表示两队人员结果
int ans[maxn][2*maxn][2];//DP过程中对于状态的存储
struct node
{
int count[2]; //表示第i个连通分量中染色为0和1的个数
int path[2][maxn];//记录第i个连通分量染色分别为0和1的顶点
} data[maxn];
inline int f(int a)
{
return a<0?(-a):a;
}
void inversion()//求补图
{
for(int i=1; i<=n; i++)
{
map[i][i]=false;
for(int j=i+1; j<=n; j++)
{
if(map[i][j]&&map[j][i])
map[i][j]=map[j][i]=false;
else map[i][j]=map[j][i]=true;
}
}
}
bool dfs(int now,int col)//DFS染色并求连通分量
{
data[connect_num].count[color[now]=col]++;
data[connect_num].path[col][data[connect_num].count[col]]=now;
for(int i=1; i<=n; i++)
{
if(map[now][i])
{
if(color[i]==-1)
{
if(!dfs(i,col^1))
return false;
}
else if(color[i]==color[now])
return false;
}
}
return true;
}
void output()//查找dp[connect_num-1][j]中为true的最小的差值,并输出当前差值下的背包状态
{
int j=100-n;
int t1,t2;
t1=t2=0;
for(int i=100-n; i<=100+n; i++)
if(dp[connect_num-1][i]&&f(100-j)>=f(100-i))
j=i;
for(int i=connect_num-1; i>=0; i--)
{
if(ans[i][j][0]==0)
{
for(int k=0; k<data[i].count[0]; k++)
team[0][t1++]=data[i].path[0][k+1];
for(int k=0; k<data[i].count[1]; k++)
team[1][t2++]=data[i].path[1][k+1];
}
else
{
for(int k=0; k<data[i].count[1]; k++)
team[0][t1++]=data[i].path[1][k+1];
for(int k=0; k<data[i].count[0]; k++)
team[1][t2++]=data[i].path[0][k+1];
}
j=ans[i][j][1];
}
cout<<t1;
for(int i=0; i<t1; i++)
cout<<" "<<team[0][i];
cout<<endl;
cout<<t2;
for(int i=0; i<t2; i++)
cout<<" "<<team[1][i];
cout<<endl;
}
int main()
{
while(cin>>n&&n)
{
memset(map,0,sizeof(map));
for(int i=1; i<=n; i++)
{
int j;
while(cin>>j&&j)
map[i][j]=true;
}
bool flag=false;
connect_num=0;
inversion();
memset(color,-1,sizeof(color));
for(int i=0; i<n; i++)
memset(data[i].count,0,sizeof(data[i].count));
for(int i=1; i<=n; i++)
{
if(color[i]==-1)
{
if(!dfs(i,0))
{
flag=true;
break;
}
else connect_num++;
}
}
if(flag)
{
cout<<"No solution"<<endl;
continue;
}
memset(dp,0,sizeof(dp));
memset(ans,0,sizeof(ans));//一下是DP过程
dp[0][100+data[0].count[0]-data[0].count[1]]=true;
ans[0][100+data[0].count[0]-data[0].count[1]][0]=0;
ans[0][100+data[0].count[0]-data[0].count[1]][1]=100;
dp[0][100-data[0].count[0]+data[0].count[1]]=true;
ans[0][100-data[0].count[0]+data[0].count[1]][0]=1;
ans[0][100-data[0].count[0]+data[0].count[1]][1]=100;
for(int i=1; i<connect_num; i++)
{
for(int j=100+n; j>=100-n; j--)//从最大差值到最小差值枚举状态
{
if(dp[i-1][j])
{
int tmp=j+data[i].count[0]-data[i].count[1];
if(!dp[i][tmp])
{
dp[i][tmp]=true;
ans[i][tmp][0]=0;
ans[i][tmp][1]=j;
}
tmp=j-data[i].count[0]+data[i].count[1];
if(!dp[i][tmp])
{
dp[i][tmp]=true;
ans[i][tmp][0]=1;
ans[i][tmp][1]=j;
}
}
}
}
output();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: