UVa 1627 Team them up!

题目:

Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

Submit Status

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

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.

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

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.

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

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 --------------------------------------------------------------------------------------------------------------------------------------------------------------------

思路:

给出关系图,不相识(互相)的两人必须分在不同组,要求分成两组且分组后有两组人数相差最少。

按照相反关系重新建图,如果两人不互相认识则连边,那么在一个联通块中,如何分组或是不能分组可知。如果不能构成二分图,那么问题无解因为不能满足必须分在不同组的要求。

设d[i][j+n]表示已经考虑到第i个联通块且两组相差i的情况是否存在。因为 j 属于[-n,n]所以需要+n调节j的范围。

有状态转移方程:

if(d[i][j+n]) 

             d[i+1][j+n+diff[i]]=1;

             d[i+1][j+n-diff[i]]=1;

其中diff[i]代表第i个联通块可分成的两组人数之差。

ans的得到需要按绝对值从小到大依此枚举,根据d[][]判断是否存在即可。

代码:

 #include<cstdio>
#include<cstring>
#include<vector>
#define FOR(a,b,c) for(int a=(b);a<(c);a++)
using namespace std; const int maxn = + ; int colors_num,n,m;
int d[maxn][*maxn],diff[maxn];
int G[maxn][maxn];
vector<int> team[maxn][];
int colors[maxn]; //如果不是二部图return false
bool dfs(int u,int c) {
colors[u]=c; //c==1 || 2
team[colors_num][c-].push_back(u);
FOR(v,,n)
if(u!=v && !(G[u][v]&&G[v][u])){ //不互相认识
if(colors[v]> && colors[u]==colors[v]) return false;
//u v不能在一组却出现在了一组
if(!colors[v] && !dfs(v,-c)) return false;
}
return true;
} bool build_graph() {
colors_num=;
memset(colors,,sizeof(colors)); FOR(i,,n) if(!colors[i]){
team[colors_num][].clear();
team[colors_num][].clear();
if(!dfs(i,)) return false;
diff[colors_num]=team[colors_num][].size()-team[colors_num][].size();
colors_num++;
}
return true;
} void print(int ans) {
vector<int> team1, team2;
for(int i = colors_num-; i >= ; i--) { //对 每个联通块
int t;
if(d[i][ans-diff[i]+n]) { t = ; ans -= diff[i]; } //判断+- //组号为t
else { t = ; ans += diff[i]; }
for(int j = ; j < team[i][t].size(); j++) //加入team1
team1.push_back(team[i][t][j]);
for(int j = ; j < team[i][^t].size(); j++) //加入team2
team2.push_back(team[i][^t][j]);
}
printf("%d", team1.size());
for(int i = ; i < team1.size(); i++) printf(" %d", team1[i]+);
printf("\n"); printf("%d", team2.size());
for(int i = ; i < team2.size(); i++) printf(" %d", team2[i]+);
printf("\n");
} void dp() {
//d[i][j+n] 代表考虑到第i个联通块时两组相差j的情况是否存在
memset(d,,sizeof(d));
d[][+n]=; //+n 调节范围
FOR(i,,colors_num)
FOR(j,-n,n+) if(d[i][j+n]) {
//刷表 存在
d[i+][j+n+diff[i]]=;
d[i+][j+n-diff[i]]=;
} FOR(ans,,n+) {
if(d[colors_num][n+ans]) {print(ans); return; }
if(d[colors_num][n-ans]) {print(-ans); return; }
}
} int main() {
int T; scanf("%d",&T);
while(T--) {
scanf("%d",&n);
FOR(u,,n) { //读入原图
int v;
while(scanf("%d",&v) && v) G[u][v-]=; //v-1调节序号
}
if(n== || !build_graph()) printf("No solution\n"); //n==1 -> no solution
else dp(); if(T) printf("\n");
}
return ;
}

【暑假】[深入动态规划]UVa 1627 Team them up!的更多相关文章

  1. UVa 1627 - Team them up!——[0-1背包]

    Your task is to divide a number of persons into two teams, in such a way, that: everyone belongs to ...

  2. UVA 1627 Team them up!

    https://cn.vjudge.net/problem/UVA-1627 题目 有n(n≤100)个人,把他们分成非空的两组,使得每个人都被分到一组,且同组中的人相互认识.要求两组的成员人数尽量接 ...

  3. UVa 1627 Team them up! (01背包+二分图)

    题意:给n个分成两个组,保证每个组的人都相互认识,并且两组人数相差最少,给出一种方案. 析:首先我们可以知道如果某两个人不认识,那么他们肯定在不同的分组中,所以我们可以根据这个结论构造成一个图,如果两 ...

  4. UVA.540 Team Queue (队列)

    UVA.540 Team Queue (队列) 题意分析 有t个团队正在排队,每次来一个新人的时候,他可以插入到他最后一个队友的身后,如果没有他的队友,那么他只能插入到队伍的最后.题目中包含以下操作: ...

  5. 【暑假】[深入动态规划]UVa 1628 Pizza Delivery

    UVa 1628 Pizza Delivery 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=51189 思路:    ...

  6. 【暑假】[深入动态规划]UVa 1380 A Scheduling Problem

     UVa 1380 A Scheduling Problem 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=41557 ...

  7. 【暑假】[深入动态规划]UVa 12170 Easy Climb

    UVa 12170 Easy Climb 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=24844 思路:  引别人一 ...

  8. 【暑假】[深入动态规划]UVa 10618 The Bookcase

    UVa 12099  The Bookcase 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=42067 思路:    ...

  9. 【暑假】[深入动态规划]UVa 10618 Fun Game

    UVa 10618 Fun Game 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=36035 思路:   一圈人围坐 ...

随机推荐

  1. HTML <iframe> 标签

    参考地址:http://www.w3school.com.cn/tags/tag_iframe.asp ------------------------------------------------ ...

  2. 【LCA】bzoj 2144:跳跳棋

    2144: 跳跳棋 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 248  Solved: 121[Submit][Status][Discuss] ...

  3. 【扩展欧几里得】Codevs 1200: [noip2012]同余方程

    Description 求关于 x 同余方程 ax ≡ 1 (mod b)的最小正整数解. Input Description 输入只有一行,包含两个正整数 a, b,用 一个 空格隔开. Outpu ...

  4. 团体程序设计天梯赛-练习集L1-021. 重要的话说三遍

    L1-021. 重要的话说三遍 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 这道超级简单的题目没有任何输入. 你只需要把这句 ...

  5. LESS CSS 总结

    1.LESS 简介 less是动态的样式表语言,通过简洁明了的语法定义,使编写 CSS 的工作变得非常简单 类似Jquery框架 中文网站: http://www.lesscss.net/ 2.编译工 ...

  6. java jdk自带程序分析(内存分析/线程分析)

    周末看到一个用jstack查看死锁的例子.昨天晚上总结了一下jstack(查看线程).jmap(查看内存)和jstat(性能分析)命令. 1.1.Jstack 1.1   jstack能得到运行jav ...

  7. C++中 模板Template的使用

    1.在c++Template中很多地方都用到了typename与class这两个关键字,而且好像可以替换,是不是这两个关键字完全一样呢?答:class用于定义类,在模板引入c++后,最初定义模板的方法 ...

  8. Ubuntu 学习笔记

    1.   ubuntu开启root账号,设置分配很简单,只要为root设置一个root密码就行了: $ sudo passwd root 之后会提示要输入root用户的密码,连续输入root密码,再使 ...

  9. WIN7 XP设置MTU,提升下载速度

    可能很少有雷友注意过“本机.网络”的“MTU”值对自己网络性能产生的影响.对于追求更快的下载速度来说,MTU值设置不当,就仿佛穿着高跟鞋跑步一般. MTU是什么? “MTU=最大传输单元 单位:字节” ...

  10. TextView设置样式的3种方式

    1,直接在<TextView>中设置 <TextView android:id="@+id/tv_badge_view_count" android:layout ...