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. Java_文件夹拷贝

    一.思路 * 文件夹的拷贝 1.递归查找子孙级文件 2.文件复制 文件夹创建 二.代码 package com.ahd.File; import java.io.File; import java.i ...

  2. 1. 七种join的sql编写

    一.join图 二.sql演示 a.创建演示表及数据 SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ----------------------- ...

  3. Netty实战十四之案例研究(一)

    1.Droplr——构建移动服务 Bruno de Carvalho,首席架构师 在Droplr,我们在我的基础设施的核心部分.从我们的API服务器到辅助服务的各个部分都使用了Netty. 这是一个关 ...

  4. Verification and validation

    Verification Verification is the process to make sure the product satisfies the conditions imposed a ...

  5. C#设计模式之二十职责链模式(Chain of Responsibility Pattern)【行为型】

    一.引言 今天我们开始讲“行为型”设计模式的第八个模式,该模式是[职责链模式],英文名称是:Chain of Responsibility Pattern.让我们看看现实生活中的例子吧,理解起来可能更 ...

  6. Android BrocastReceiver解析

    简介 BroadcastReceiver(广播接收器)是Android四大组件之一,是一个用来响应系统范围内的广播组件,可以从Android系统和其它app发送或接收广播消息,类似于发布 - 订阅设计 ...

  7. 关于 Socket 设置 setSoTimeout 误用的说明

    做网络开发的想必对setSoTimeout这个方法很熟悉,知道是设置的超时事件.但是很多人都认为这个是设置链路的超时时间,但是查看相关文档的此方法的说明: HttpConnectionParams: ...

  8. 04-HTML-图片标签

    <html> <head>  <title>图片标签学习</title>  <meta charset="utf-8"/> ...

  9. iPhone手机怎么投屏到电脑上

    如今生活水平不断上升,人们更加追求高质量.高享受的生活,所以可以利用一切资源提高生活质量,享受更好的生活体验,比如说手机投屏电脑就可以提高我们的视觉体验,所以更多的人去尝试,那么iPhone手机怎么投 ...

  10. BDD实战篇 - .NET Core里跑Specflow - 可以跑集成测试和单元测试

    这是<如何用ABP框架快速完成项目 >系列中和DevOps系列文章其中一篇文章.   BDD很赞!比TDD先进很多,能够大大提高编码效率.   上一篇文章说了如何在.NET Core里安装 ...