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时还是挺意外的。。

这道题是一个典型的搜索问题,可以使用深度优先搜索(DFS)或广度优先搜索(BFS)来解决。以下是使用DFS的代码实现: ```c++ #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAXN = 20; const int MAXM = 20; int n, m, sx, sy, ex, ey; char maze[MAXN][MAXM]; // 迷宫 int vis[MAXN][MAXM]; // 标记数组 int dx[] = {0, 0, 1, -1}; // 方向数组 int dy[] = {1, -1, 0, 0}; void dfs(int x, int y) { if (x == ex && y == ey) { // 到达终点 printf("(%d,%d)", x, y); return; } for (int i = 0; i < 4; i++) { // 依次尝试四个方向 int nx = x + dx[i]; int ny = y + dy[i]; if (nx >= 0 && nx < n && ny >= 0 && ny < m && maze[nx][ny] != '#' && !vis[nx][ny]) { vis[nx][ny] = 1; // 标记已访问 printf("(%d,%d)->", x, y); dfs(nx, ny); return; } } } int main() { while (scanf("%d%d", &n, &m) == 2) { memset(vis, 0, sizeof(vis)); for (int i = 0; i < n; i++) { scanf("%s", maze[i]); for (int j = 0; j < m; j++) { if (maze[i][j] == 'S') { sx = i; sy = j; } else if (maze[i][j] == 'T') { ex = i; ey = j; } } } vis[sx][sy] = 1; dfs(sx, sy); printf("\n"); } return 0; } ``` 代码实现中,使用了一个标记数组 `vis` 来标记每个位置是否已经被访问过,避免走重复的路线。使用DFS的时候,每次从当前位置依次尝试四个方向,如果某个方向可以走,则标记该位置已经被访问过,并输出当前位置的坐标,然后递归进入下一个位置。如果当前位置是终点,则直接输出并返回。 在输出路径的时候,由于是递归调用,所以输出的路径是反向的,需要将其反转过来,即从终点往起点遍历输出。 需要注意的是,题目中要求输出的路径是 `(x1,y1)->(x2,y2)->...->(xn,yn)` 的形式,每个坐标之间用 `->` 连接。所以在输出的时候需要特别处理第一个坐标和最后一个坐标的格式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值