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. 【MAC】安装神器brew

    安装方法:命令行输入 /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/ma ...

  2. SpringBoot的Autowierd失败

    通常是以下几种可能: 1.没有加@Service注解,或者是这个bean没有放在标注了@Configuration这个注解的类下. 2.SpringBoot启动类没有开启扫描 @ComponentSc ...

  3. 处理JavaScript异常的正确姿势

    译者按: 错误是无法避免的,妥善处理它才是最重要的! 原文: A Guide to Proper Error Handling in JavaScript Related Topics: 译者: Fu ...

  4. 【读书笔记】iOS-使用钥匙串保护数据

    一,将应用从设备上删除时,并不会删除其钥匙串项,这使得调试工作困难得多.模拟器有一个Reset Contents and Settings选项,可用于将钥匙串项移除.因此,强烈建议在模拟器上确定Key ...

  5. CNN中,1X1卷积核到底有什么作用呢?

    CNN中,1X1卷积核到底有什么作用呢? https://www.jianshu.com/p/ba51f8c6e348 Question: 从NIN 到Googlenet mrsa net 都是用了这 ...

  6. Android assets文件夹之位置放置和作用

    Android 的assets文件夹的放置位置,Eclipse创建项目时就生成了的,Android Studio则不太一样,AS可以包含几种方式, 1:可以在build.gradle文件下配置,加如下 ...

  7. echarts雷达图点击事件 包含(2.x,3.85,4.02)测试

    最近看见别人问的问题,点击雷达图的拐点,获取点击数据的问题,直接上代码. echarts 2x 的点击事件 echarts配置问题:https://www.douban.com/note/509404 ...

  8. Android开发学习之RecyclerView

    1.在app/build.gradle中添加RecyclerView依赖 implementation 'com.android.support:recyclerview-v7:28.0.0' 注意依 ...

  9. <自动化测试方案_5>第五章、代码单元自动化测试

    第五章.代码单元自动化测试 代码单元测试需要根据编程语言,选择单元测试框架,然后访问类方法,函数.代码单元测试做自动化,相比API.UI自动化做起来更加麻烦,建议放到待API自动化测试.UI自动化测试 ...

  10. float、double、BigDecimal的一些精度问题

    float f = 280.8f;System.out.println(f*100);结果是什么?结果是:28080.0f(我是这么想的)实际结果是:28079.998 既然float处理有问题换do ...