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 ...
随机推荐
- ORA-00959: tablespace 'PSAPTEMP' does not exist
错误 : ORA-00959: tablespace 'PSAPTEMP' does not exist 解决办法: CREATE TEMPORARY TABLESPACE PSAPTEMP TEM ...
- Java基础篇——JVM之GC原理(干货满满)
原创不易,如需转载,请注明出处https://www.cnblogs.com/baixianlong/p/10697554.html ,多多支持哈! 一.什么是GC? GC是垃圾收集的意思,内存处理是 ...
- Add Again(重复元素排序) UVA11076
Add Again Summation of sequence of integers is always a common problem in Computer Science. Rather t ...
- 处理JavaScript异常的正确姿势
译者按: 错误是无法避免的,妥善处理它才是最重要的! 原文: A Guide to Proper Error Handling in JavaScript Related Topics: 译者: Fu ...
- Vivox9怎么录制屏幕
手机怎么录屏是很多手机党一直提出的问题,而且经常发生录制的视频没有声音的现象,现在就给大家推荐一款软件,不仅能完美的录制视频,而且还可以完整的将视频声音录制下来,下面看看Vivox9怎么录制屏幕吧! ...
- Android为TV端助力 帧动画
首先在res/drawable/name1.xml/定义一组图片集合: <?xml version="1.0" encoding="utf-8"?> ...
- input range样式优化
首先HTML代码: <input id="snrPollInterval" type="range" min="1" max=&quo ...
- PhpStorm和WAMP配置调试参数,问题描述Error. Interpreter is not specified or invalid. Press “Fix” to edit your project configuration.
PhpStorm和WAMP配置调试参数,解决实际问题. 问题描述: Error. Interpreter is not specified or invalid. Press "Fix&qu ...
- Centos7安装netstat及简单使用
Centos7默认不安装netstat组件,需要使用时需要自己安装. 1.查看当前机器net-tools包所在位置 2.安装net-tools包 3.使用netstat命令查看端口占用情况 4.查看指 ...
- ORA-1652: unable to extend temp segment by 128 in tablespace xxx Troubleshootin
当收到告警信息ORA-01652: unable to extend temp segment by 128 in tablespace xxxx 时,如何Troubleshooting ORA-16 ...