Problem UVA1627-Team them up!

Total Submissions:1228  Solved:139

Time Limit: 3000 mSec

Problem 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. Thefirstlineintheinputfilecontainsasingleintegernumber N (2 ≤ N ≤ 100) —thetotalnumber 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 i-th 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

题解:有一阵子没写博客了,感到很内疚,这个东西还是要坚持。这个题用的东西比较杂,但每一部分都不算难,首先是二分图的判断,染色法dfs很好做,之后就是一个01背包的变形,不过做法似乎和背包没什么关系,数据小,比较暴力的方式就能过,最后是输出解,用的是lrj之前讲的方法。

 #include <bits/stdc++.h>

 using namespace std;

 const int maxn =  + ;

 int n, tot, belong[maxn];
bool gra[maxn][maxn];
vector<int> team[maxn][];
int delta[maxn]; bool dfs(int u,int flag) {
belong[u] = flag;
team[tot][flag - ].push_back(u);
for (int v = ; v <= n; v++) {
if (gra[u][v] && u != v) {
if (belong[v] == flag) return false;
if (!belong[v] && !dfs(v, - flag)) return false;
}
}
return true;
} bool build_graph() {
memset(belong, , sizeof(belong));
for (int u = ; u <= n; u++) {
if (!belong[u]) {
tot++;
team[tot][].clear();
team[tot][].clear();
if (!dfs(u, )) return false;
}
}
return true;
} bool dp[maxn][maxn << ];
vector<int> ans, ans1; void DP() {
for (int i = ; i <= tot; i++) {
delta[i] = team[i][].size() - team[i][].size();
//printf("(%d-%d) = %d\n", team[i][0].size(), team[i][1].size(), delta[i]);
}
memset(dp, false, sizeof(dp));
dp[][ + n] = true;
for (int i = ; i <= tot; i++) {
for (int j = -n; j <= n; j++) {
if (dp[i][j + n]) dp[i + ][j + n + delta[i + ]] = dp[i + ][j + n - delta[i + ]] = true;
}
} int res = ;
for (int j = ; j <= n; j++) {
if (dp[tot][n + j]) {
res = n + j;
break;
}
if (dp[tot][n - j]) {
res = n - j;
break;
}
}
ans.clear(), ans1.clear();
for (int i = tot; i >= ; i--) {
if (dp[i - ][res - delta[i]]) {
for (int j = ; j < (int)team[i][].size(); j++) {
ans.push_back(team[i][][j]);
}
for (int j = ; j < (int)team[i][].size(); j++) {
ans1.push_back(team[i][][j]);
}
res -= delta[i];
}
else if (dp[i - ][res + delta[i]]) {
for (int j = ; j < (int)team[i][].size(); j++) {
ans1.push_back(team[i][][j]);
}
for (int j = ; j < (int)team[i][].size(); j++) {
ans.push_back(team[i][][j]);
}
res += delta[i];
}
}
printf("%d", ans.size());
for (int i = ; i < (int)ans.size(); i++) printf(" %d", ans[i]);
printf("\n");
printf("%d", ans1.size());
for (int i = ; i < (int)ans1.size(); i++) printf(" %d", ans1[i]);
printf("\n");
} int main()
{
//freopen("input.txt", "r", stdin);
int iCase;
scanf("%d", &iCase);
while (iCase--) {
tot = ;
memset(gra, false, sizeof(gra));
scanf("%d", &n);
int v;
for (int u = ; u <= n; u++) {
while (true) {
scanf("%d", &v);
if (v == ) break;
gra[u][v] = true;
}
}
for (int u = ; u <= n; u++) {
for (int v = ; v < u; v++) {
if (!gra[u][v] || !gra[v][u]) gra[u][v] = gra[v][u] = true;
else gra[u][v] = gra[v][u] = false;
}
} if (n == || !build_graph()) printf("No solution\n");
else DP(); if (iCase) printf("\n");
}
return ;
}

UVA1627-Team them up!(二分图判断+动态规划)的更多相关文章

  1. 【THUWC2017】随机二分图(动态规划)

    [THUWC2017]随机二分图(动态规划) 题面 BZOJ 洛谷 题解 如果每天边的限制都是\(0.5\)的概率出现或者不出现的话,可以把边按照二分图左侧的点的编号排序,然后设\(f[i][S]\) ...

  2. POJ 1112 Team Them Up! 二分图判定+01背包

    题目链接: http://poj.org/problem?id=1112 Team Them Up! Time Limit: 1000MSMemory Limit: 10000K 问题描述 Your ...

  3. POJ1112 Team Them Up![二分图染色 补图 01背包]

    Team Them Up! Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7608   Accepted: 2041   S ...

  4. hdu 4751 2013南京赛区网络赛 二分图判断 **

    和以前做过的一个二分图颇为相似,以前的是互相不认识的放在一组,这个是互相认识的,本质上是相同的 是 hdu 2444 #include<cstdio> #include<iostre ...

  5. hdu 2444 二分图判断与最大匹配

    题意:有n个学生,有m对人是认识的,每一对认识的人能分到一间房,问能否把n个学生分成两部分,每部分内的学生互不认识,而两部分之间的学生认识.如果可以分成两部分,就算出房间最多需要多少间,否则就输出No ...

  6. hdu 2444 The Accomodation of Students(最大匹配 + 二分图判断)

    http://acm.hdu.edu.cn/showproblem.php?pid=2444 The Accomodation of Students Time Limit:1000MS     Me ...

  7. HDU 3478 Catch (连通性&&二分图判断)

    链接 [https://vjudge.net/contest/281085#problem/C] 题意 一个n个点,m条边的图,开始的点是s 每次必须移动到相邻的位置,问你是否存在某个时刻所有点都可能 ...

  8. HDU 2444 二分图判断 (BFS染色)+【匈牙利】

    <题目链接> 题目大意: 有N个人,M组互相认识关系互相认识的两人分别为a,b,将所有人划分为两组,使同一组内任何两人互不认识,之后将两个组中互相认识的人安排在一个房间,如果出现单人的情况 ...

  9. HDU 2444 - The Accomodation of Students - [二分图判断][匈牙利算法模板]

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2444 Time Limit: 5000/1000 MS (Java/Others) Mem ...

随机推荐

  1. VM扩展磁盘大小

    1.通过扩展磁盘的方法增大磁盘大小 2.然后开启Linux 此时查看磁盘  df -h 并没有增加,使用 fdisk -l 查看发现已经扩展 使用 root 用户,进入到 ~ 家目录下面. 3.使用 ...

  2. [工具配置]requirejs 多页面,多入口js文件打包总结

    需要明确以下几点: 1.本地前端调试代码肯定是调用原始的路径以及代码,但是线上运行的肯定是通过打包后的另一个路径,这儿就是生成的dist文件夹了. 2.requirejs的引入,线上跟线下的路径怎么控 ...

  3. Linux扩展分区记录

    Vmware中新建的Linux虚拟机,数据盘规划太小,数据量超出磁盘大小,本文介绍如何快速扩充空间 参考:https://blog.csdn.net/lyd135364/article/details ...

  4. 通过 python ssh库连接并发送命令给设备

    import paramiko import time hostname = '192.168.248.156' port = 22 user = 'zhou' passwd = ' paramiko ...

  5. 微信小程序 text属性设置 WXSS样式

    >微信小程序的布局css样式 参考自  珺L 文字 width: fit-content;font-size:20px;      /*设置文字字号*/color:red;           ...

  6. html-edm(邮件营销)编写规则

    最近写了一个edm邮件 以前没有接触过  使用的是很老的html页面编写规则  只能用table标签  在此记录一下edm编写的一些规则 个人参考的是这两个网址,转载一下 http://www.zco ...

  7. vertical-align属性探究

    在前端开发中我们经常需要将元素垂直居中对齐,我们很自然的想到使用vertical-align属性,但是使用后却发现有时候能起作用,有时候却又不起作用.究其原因还是因为我们没有将vertical-ali ...

  8. js每隔一段时间执行函数

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  9. SQL中常用字符串函数

    --CHARINDEX 返回指定字符的位置--2个参数,第一个是要查找的字符串 第二个参数:要搜索的字符串 参数3:开始查找的位置--查找位置从1开始,返回结果为0时表示为结果为空 SELECT CH ...

  10. SQL Server中sys.syslogin中updatedate字段的浅析

    在系统视图sys.syslogins中,有createdate与updatedate两个字段,分别表示创建登录名与更新登录名的日期,如果你用updatedate的值来判断一个登录名的权限是否被修改过, ...