poj 1112 Team Them Up! (补图+dp)

来源:互联网 发布:彩票免费计划软件 编辑:程序博客网 时间:2024/06/05 22:59

Team Them Up!
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 7616 Accepted: 2044 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

52 3 5 01 4 5 3 01 2 5 01 2 3 04 3 2 1 0

Sample Output

3 1 3 52 2 4

Source

Northeastern Europe 2001

[Submit]   [Go Back]   [Status]   [Discuss]


题解:补图+dp

题目大意:把n个人分成2各组,每一个人有他所认识的人。所分的组有四点要求:

1、 每个人都必需属于一个组。

2、 每个组至少有一个人。

3、 每个组里面的每个人必需互相认识。

4、 两个组的成员的数目应尽量接近。

1、 先读入数据建立有向图,然后对这个有向图进行处理,如果两个点之间的边是单向边,就认为两个点之间无边(因为这两个人不互相认识),对于两个点间的双向边,即建立一条无向边(这两个人互相认识),这样就可以把一个有向图转化为一个无向图。

2、 将这个无向图转化为它的反图。即有边的把边删去,无边的添上一条边。(其实1,2步在程序实现时可以一次完成)。

3、 在这个反图中的不同的连通分量中的两个人都是互相认识的,那些互不认识的人就在一个连通分量里面。

4、 遍历每一个连通分量,对连通分量中的点进行黑白染色,要求有边连接的两点颜色不同。这样DFS结束后,就可以根据颜色把连通分量中的点分成两个组,在不同两个组的人是不可能分到一个team内的。到这里要做一个特判,同一组里的任意两个人,如果反图中存在一条边连接这两个人的无向边,则说明无解,输出"No solution"。

我们将每个连通分量分成了两组,这两组不能同时入选同一队,所以我们可以进行背包dp。

f[i][j]表示到第i个连通分量,一队的总人数为j是否可行。然后记录方案,最后逆推回去即可。

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#define N 203using namespace std;int n,map[N][N];int point[N*N],next[N*N],v[N*N],h[N][N],a[N],b[N];int block[N][3][N],dp[N][N],g[N][N],vis[N],tot,cnt;void add(int x,int y){tot++; next[tot]=point[x]; point[x]=tot; v[tot]=y;tot++; next[tot]=point[y]; point[y]=tot; v[tot]=x;}void dfs(int x,int k){block[cnt][k][0]++;int t=block[cnt][k][0];block[cnt][k][t]=x;for (int i=point[x];i;i=next[i]) if (!vis[v[i]]) { vis[v[i]]=1; dfs(v[i],k^1); }}int main(){scanf("%d",&n);for (int i=1;i<=n;i++) {while (true){int x; scanf("%d",&x);if (!x) break;map[i][x]=1;}}for (int i=1;i<=n-1;i++) for (int j=i+1;j<=n;j++) { if (i==j) continue; if (!map[i][j]||!map[j][i]) add(i,j),h[i][j]=1,h[j][i]=1; }for (int i=1;i<=n;i++) if (!vis[i]) { cnt++; vis[i]=1; dfs(i,0); }for (int i=1;i<=cnt;i++){for (int j=0;j<=1;j++){int t=block[i][j][0];for (int k=1;k<=t-1;k++) for (int l=k+1;l<=t;l++){ int x=block[i][j][k]; int y=block[i][j][l]; if (h[x][y]) { printf("No solution\n"); return 0; } }}}memset(dp,0,sizeof(dp));int x=block[1][0][0]; int y=block[1][1][0];dp[1][x]=1; g[1][x]=0;dp[1][y]=1; g[1][y]=1;for (int i=2;i<=cnt;i++) for (int j=0;j<=n;j++) { if (dp[i][j]) continue; int x=block[i][0][0]; int y=block[i][1][0]; if (j-x>=0&&dp[i-1][j-x]) dp[i][j]=1,g[i][j]=0; if (j-y>=0&&dp[i-1][j-y]) dp[i][j]=1,g[i][j]=1; }int pos=n; int ans=n;for (int i=n;i>=1;i--) if (dp[cnt][i]&&ans>abs(i-n/2)) ans=abs(i-n/2),pos=i;    for (int i=cnt;i>=1;i--) {    int t=g[i][pos]; int x=block[i][0][0]; int y=block[i][1][0];    for (int j=1;j<=block[i][t][0];j++) a[++a[0]]=block[i][t][j];    if (t) pos-=y;    else pos-=x;}printf("%d ",a[0]);memset(vis,0,sizeof(vis));for (int i=1;i<=a[0];i++) vis[a[i]]=1,printf("%d ",a[i]); printf("\n");printf("%d ",n-a[0]);for (int i=1;i<=n;i++) if(!vis[i]) printf("%d ",i); printf("\n");}



0 0