ZOJ 1462 Team Them Up! (二分图+路径保存DP)

本文探讨了如何将一组人员合理地分为两支队伍,确保每个成员都能相互认识,并尽量保持队伍规模均衡。通过详细解释算法流程和输入输出规范,为解决实际团队组建问题提供了实用的指导。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



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



# include <iostream>
# include <string.h>
# include <fstream>
# include <stdio.h>
# include <cmath>
using namespace std;
int n,m;
bool mp[105][105];
int color[105];
bool vis[105];
int lt[2][105];
int p[105][105];
int dp[105][105];
int pp;
const int inf = 1<<29;
struct po{
    int x,c;
};
po route[105][105];
bool dfs(int ii,int c){
    vis[ii] = true;
    bool fg ;
    color[ii] = c;
    lt[c%2][pp]++;
    for(int i=1;i<=n;++i){
        if(mp[ii][i]){
            if(!vis[i]){
                fg = dfs(i,(c%2 == 0?c+1:c-1));
                if(fg == false) return false;
            }
            else{
                if(color[ii] == color[i]) return false;
            }
        }
    }
    return true;
}
int main()
{
    //ifstream cin("1.txt");
    int T;
    int temp;
    int ttt=0;
    while(cin>>T){
        ttt=0;
        while(T--){
            cin>>n;
            if(ttt++) cout<<"\n";
            memset(mp,false,sizeof(mp));
            memset(vis,false,sizeof(vis));
            memset(dp,0,sizeof(dp));
            memset(color,-1,sizeof(color));
            memset(lt,0,sizeof(lt));
            for(int i=1;i<=n;++i){
                //mp[i][i] = false;
                while(cin>>temp&&temp){
                    mp[i][temp] = true;
                }
            }
            for(int i=1;i<=n;++i){                           //构图 
                for(int j=i+1;j<=n;++j){
                    if(!(mp[i][j]&&mp[j][i])){
                        mp[i][j] = mp[j][i] = true;
                    }
                    else{
                        mp[i][j] = mp[j][i] = false;
                    }
                }
            }
            int pos = 0;
            bool fg = true;
            pp=0;
    
            for(int i=1;i<=n;++i){               //着色 
                if(!vis[i]){
                    fg = dfs(i,pos);
                    if(fg == false) break;
                    pos+=2;
                    pp++;
                }
            }
            if(fg == false){
                cout<<"No solution\n";
            }
            else{
    
                dp[0][0] = 1;
                int num = 0;
                for(int i=0;i<pp;++i){                               //dp+路径保存 
                    for(int j=0;j<=num;++j){
                        int k=num-j;
                        if(dp[j][k]){
                            route[j+lt[0][i]][k+lt[1][i]].c = i*2;
                            dp[j+lt[0][i]][k+lt[1][i]] = 1;
                            route[j+lt[1][i]][k+lt[0][i]].c = i*2+1;
                            dp[j+lt[1][i]][k+lt[0][i]] = 1;
                        }
                    }
                    num+=lt[0][i] + lt[1][i];
                }
    
                int posi,posj;
                int res = inf;
                for(int i=1;i<=n;++i){
                    if(dp[i][n-i]&&res>abs(i-n+i)){
                        res = abs(i-n+i);
                        posi = i;posj = n-i;
                    }
                }
                cout<<posi;
                memset(vis,false,sizeof(vis));
                int p1 = posi,p2 = posj;
                int cc = route[p1][p2].c;
                int tp = pp-1;
                while(1){
                    for(int i=1;i<=n;++i){
                        if(color[i] == cc){
                            vis[i] = true;
                            cout<<" "<<i;
                        }
                    }
                    if(cc%2==0){
                        p1-=lt[0][tp];
                        p2-=lt[1][tp];
                        tp--;
                    }
                    else{
                        p1-=lt[1][tp];
                        p2-=lt[0][tp];
                        tp--;
                    }
                    cc = route[p1][p2].c;
                    if(p1==0&&p2==0) break;
                        
                }
                cout<<endl;
                cout<<n-posi;
                for(int i=1;i<=n;++i){
                    if(!vis[i]) cout<<" "<<i;
                }
                cout<<endl;
            }
                
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值