ZOJ_1462 Team Them Up! 求完连通分量后,再进行背包,之后背包路径回溯

5 篇文章 0 订阅
2 篇文章 0 订阅



Team Them Up!

Time Limit: 2 Seconds       Memory Limit: 65536 KB       Special Judge

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.


This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.


Sample Input

2

5
3 4 5 0
1 3 5 0
2 1 4 5 0
2 3 5 0
1 2 3 4 0

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

No solution

3 1 3 5
2 2 4


解法就是把不认识的人连一条边, 如果发现这个人在集合X,则他所连的点都放到集合Y。     之后对这些产生的集合做背包处理。  背包处理完,再利用路径回溯。。

#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cstring>
using namespace std;
int n,scnt;
const int N = 111;
int g[N][N],b[N][N];
int vis[N];
int dp[N][N][N];
int color[N];
vector<int> v[N][2];
bool dfs(int u,int col){
    vis[u] = col;
    v[scnt][col].push_back(u);
    bool flag = true;
    for(int i=1;i<=n;i++){
        if(g[u][i] && vis[i] == -1){
            if(!dfs(i,col ^ 1)){
                flag = false;
                break;
            }
        }else if(g[u][i] && vis[i] == col){
            flag = false;
            break;
        }
    }
    return flag;
}
int main(){
    //freopen("zoj1462.in","r",stdin);
    int t;
    scanf("%d",&t);
    bool ff = true;
    while(t--){
        if(ff){ff=false;}
        else printf("\n");
        memset(g,0,sizeof(g));
        memset(b,0,sizeof(b));
        memset(vis,-1,sizeof(vis));
        scanf("%d",&n);
        for(int i=1,a;i<=n;i++){
            while(scanf("%d",&a) != EOF && a){
                b[i][a]++;
            }
        }
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                if(i!=j && (!b[i][j])){
                    g[i][j] = g[j][i] = 1;
                }
        for(int i=1;i<=n;i++)
            for(int j=0;j<2;j++)
                v[i][j].clear();
        scnt = 1;
        bool flag = true;
        for(int i=1;i<=n;i++){
            if(vis[i]==-1){
                if(dfs(i,0))
                    scnt++;
                else{
                    flag = false;
                    break;
                }
            }
        }

        //for(int i=1;i<scnt;i++){
        //    for(int j=0;j<2;j++)
        //        printf("v[%d][%d].size=%d\n",i,j,(int)v[i][j].size());
        //}
        memset(dp,0,sizeof(dp));
        dp[0][0][0]=1;
        memset(color,-1,sizeof(color));
        if(flag){
            int max_n = 0;
            for(int i=1;i<scnt;i++){
                int first = v[i][0].size(),second = v[i][1].size();
                for(int j=min(first,second);j<=n;j++){
                    for(int k=min(first,second);k<=n;k++){
	
                        if(j>=first && k >= second && dp[i-1][j-first][k-second] != 0){

                            dp[i][j][k] = 1;
                            if(i == scnt -1 &&j <= n/2 && max_n < j){
                                max_n = j;
                            }
                        }
                        if(j>=second && k >= first && dp[i-1][j-second][k-first] != 0){
                            dp[i][j][k] = 2;
							if(i == scnt -1 &&j <= n/2 && max_n < j){
                                max_n = j;
                            }
				
                        }
                    }
                }
            }
            int p = max_n, q = n - max_n;
            for(int i=scnt-1; i >= 1; i--){
				int sz0 = v[i][0].size() , sz1 = v[i][1].size();
				if(p>=sz0 && q>= sz1 && dp[i-1][p-sz0][q-sz1]>0){
					for(int j = 0;j < v[i][0].size() ;j++){
                        int u = v[i][0][j];
                        color[u] = 0;
                    }
                    for(int j = 0;j < v[i][1].size() ;j++){
                        int u = v[i][1][j];
                        color[u] = 1;
                    }
					p-=v[i][0].size(), q-=v[i][1].size();
				}else if(q>=sz0 && p >= sz1 && dp[i-1][p-sz1][q-sz0] > 0){
					for(int j = 0;j < v[i][0].size() ;j++){
                        int u = v[i][0][j];
                        color[u] = 1;
                    }
                    for(int j = 0;j < v[i][1].size() ;j++){
                        int u = v[i][1][j];
                        color[u] = 0;
                    }
                    p-=v[i][1].size(),q-=v[i][0].size();
				}//else while(1){}
            }

            vector<int> as[2];
            for(int i=1;i<=n;i++)
                as[color[i]].push_back(i);

            for(int i=0;i<2;i++){
                printf("%d",(int)as[i].size());
                for(int j=0;j<as[i].size();j++){
                    printf(" %d",as[i][j]);
                }
                printf("\n");
            }

        }else{
            puts("No solution");
        }
    }
    return 0;
}


调试了挺久的。。 AC时还是挺意外的。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值