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
Sample Input
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!(二分图判断+动态规划)的更多相关文章
- 【THUWC2017】随机二分图(动态规划)
[THUWC2017]随机二分图(动态规划) 题面 BZOJ 洛谷 题解 如果每天边的限制都是\(0.5\)的概率出现或者不出现的话,可以把边按照二分图左侧的点的编号排序,然后设\(f[i][S]\) ...
- POJ 1112 Team Them Up! 二分图判定+01背包
题目链接: http://poj.org/problem?id=1112 Team Them Up! Time Limit: 1000MSMemory Limit: 10000K 问题描述 Your ...
- POJ1112 Team Them Up![二分图染色 补图 01背包]
Team Them Up! Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7608 Accepted: 2041 S ...
- hdu 4751 2013南京赛区网络赛 二分图判断 **
和以前做过的一个二分图颇为相似,以前的是互相不认识的放在一组,这个是互相认识的,本质上是相同的 是 hdu 2444 #include<cstdio> #include<iostre ...
- hdu 2444 二分图判断与最大匹配
题意:有n个学生,有m对人是认识的,每一对认识的人能分到一间房,问能否把n个学生分成两部分,每部分内的学生互不认识,而两部分之间的学生认识.如果可以分成两部分,就算出房间最多需要多少间,否则就输出No ...
- hdu 2444 The Accomodation of Students(最大匹配 + 二分图判断)
http://acm.hdu.edu.cn/showproblem.php?pid=2444 The Accomodation of Students Time Limit:1000MS Me ...
- HDU 3478 Catch (连通性&&二分图判断)
链接 [https://vjudge.net/contest/281085#problem/C] 题意 一个n个点,m条边的图,开始的点是s 每次必须移动到相邻的位置,问你是否存在某个时刻所有点都可能 ...
- HDU 2444 二分图判断 (BFS染色)+【匈牙利】
<题目链接> 题目大意: 有N个人,M组互相认识关系互相认识的两人分别为a,b,将所有人划分为两组,使同一组内任何两人互不认识,之后将两个组中互相认识的人安排在一个房间,如果出现单人的情况 ...
- HDU 2444 - The Accomodation of Students - [二分图判断][匈牙利算法模板]
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2444 Time Limit: 5000/1000 MS (Java/Others) Mem ...
随机推荐
- 阅读Java Native源码前的准备
前言 读java native源代码时,我们一般会去网站下载openjdk8源码http://download.java.net/openjdk/jdk8/promoted/b132/openjdk- ...
- 使用virtualenv进行python环境隔离
按照以下步骤安装 TensorFlow: 1.打开终端(一个 shell),你将在这个终端中执行随后的步骤 2.通过以下命令安装 pip 和 virtualenv sudo easy_install ...
- Advanced redirection features
here are three types of I/O, which each have their own identifier, called a file descriptor: standar ...
- Mybatis框架可视化(1)
Mybatis整体架构视图: 接 口 层 SqlSession (定义了Mybatis暴露给应用程序调用的API) 核 心 处 理 层 配置解析 (加载核心配置.映射配置. mapper接口注解信息, ...
- Flask 系列之 LoginManager
说明 操作系统:Windows 10 Python 版本:3.7x 虚拟环境管理器:virtualenv 代码编辑器:VS Code 实验目标 通过使用 flask-login 进行会话管理的相关操作 ...
- Java web.xml笔记
Javaweb项目中, web.xml文件其中的各种设置, 就是简单的标注 <?xml version="1.0" encoding="UTF-8"?&g ...
- 【代码笔记】Web-JavaScript-JavaScript 类型转换
一,效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...
- WPF:完美自定义MeaagseBox 2.0
很久前做个一个MessageBox,原文链接:http://www.cnblogs.com/DoNetCoder/p/3843658.html. 不过对比MessageBox还有一些瑕疵.这些天有时间 ...
- leetcode-978. 最长湍流子数组
leetcode-978. 最长湍流子数组 Points 数组 DP 题意 当 A 的子数组 A[i], A[i+1], ..., A[j] 满足下列条件时,我们称其为湍流子数组: 若 i <= ...
- Bullet3的一些理解
Bullet3应该是第三大物理引擎了,拥有宽松的授权方式,开源.在我的项目中将采用它. 碰撞世界(btCollisionWorld)是最基本的环境类. 动态世界(btDynamicsWorld)从碰撞 ...