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.
如果两个人不互相认识的话,那么两个人就不能在一个集合中。这样把不能在集合中的人连边,题目就变成了二分图染色。
对于每个连通分量,可以选择把他的“两部分”分别分给两个集合,带来的收益就是=/-两部分大小的差。这样就变成了每个元素必须选择的01背包,最后的要求是尽量接近零。
可以把“两部分”的组成存到vector里整体当成“元素”,在dp时记录怎样选即可输出方案。
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
vector<int> to[110],val,in[3],g[3];
vector<vector<int> > sf,op;
int n,vis[110],pre[110][510];
bool b[110],ille,dp[110][510];
int dfs(int u,int fa,int c)
{
int i,v,ret=1;
in[c].push_back(u);
vis[u]=c;
for (i=0;i<to[u].size();i++)
{
v=to[u][i];
if (v!=fa)
{
if (vis[v])
{
if (vis[v]==c)
{
ille=1;
return 0;
}
}
else ret-=dfs(v,u,3-c);
if (ille) return 0;
}
}
return ret;
}
int main()
{
int i,j,k,p,q,u,v,x,y,z,ans,now;
scanf("%d",&n);
for (i=1;i<=n;i++)
{
memset(b,0,sizeof(b));
while (scanf("%d",&x)&&x)
b[x]=1;
for (j=1;j<=n;j++)
if (j!=i&&!b[j])
{
to[i].push_back(j);
to[j].push_back(i);
}
}
for (i=1;i<=n;i++)
if (!vis[i])
{
in[1].clear();
in[2].clear();
val.push_back(dfs(i,-1,1));
sf.push_back(in[1]);
op.push_back(in[2]);
if (ille)
{
printf("No solution\n");
return 0;
}
}
dp[0][2*n]=1;
for (i=1;i<=val.size();i++)
{
x=val[i-1];
for (j=-n;j<=n;j++)
{
if (dp[i-1][j-x+2*n])
{
dp[i][j+2*n]=1;
pre[i][j+2*n]=1;
}
if (dp[i-1][j+x+2*n])
{
dp[i][j+2*n]=1;
pre[i][j+2*n]=2;
}
}
}
for (ans=0;;ans++)
if (dp[val.size()][ans+2*n])
{
now=ans+2*n;
for (i=val.size();i;i--)
if (pre[i][now]==1)
{
for (j=0;j<sf[i-1].size();j++)
g[1].push_back(sf[i-1][j]);
for (j=0;j<op[i-1].size();j++)
g[2].push_back(op[i-1][j]);
now-=val[i-1];
}
else
{
for (j=0;j<sf[i-1].size();j++)
g[2].push_back(sf[i-1][j]);
for (j=0;j<op[i-1].size();j++)
g[1].push_back(op[i-1][j]);
now+=val[i-1];
}
printf("%d %d",g[1].size(),g[1][0]);
for (i=1;i<g[1].size();i++)
printf(" %d",g[1][i]);
printf("\n");
printf("%d %d",g[2].size(),g[2][0]);
for (i=1;i<g[2].size();i++)
printf(" %d",g[2][i]);
printf("\n");
return 0;
}
}