题意:给n个分成两个组,保证每个组的人都相互认识,并且两组人数相差最少,给出一种方案。

析:首先我们可以知道如果某两个人不认识,那么他们肯定在不同的分组中,所以我们可以根据这个结论构造成一个图,如果两个不相互认识,

那么就加一条边,然后如果这个图是二分图,那么这分组是可以,否则就是不可能的。然后dp[i][j]表示那两个组相差人数为 j 是不是可以达到,

当然可能为负数,所以可以提前加上n,然后就是逆序输出答案即可。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<double, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-5;
const int maxn = 100 + 10;
const int mod = 1e6;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
int color[maxn];
bool g[maxn][maxn];
vector<int> v[maxn][2];
int diff[maxn], cnt;
bool dp[maxn][maxn*2]; bool dfs(int u, int val){
color[u] = val;
v[cnt][val-1].push_back(u);
for(int i = 1; i <= n; ++i){
if(i == u) continue;
if(g[u][i] && g[i][u]) continue;
if(color[i] == color[u]) return false;
if(!color[i] && !dfs(i, 3-val)) return false;
}
return true;
} void print(int ans){
vector<int> v1, v2;
for(int i = cnt-1; i >= 0; --i){
int t;
if(dp[i][ans+n+diff[i]]){ t = 0; ans += diff[i]; }
else { t = 1; ans -= diff[i]; }
for(int j = 0; j < v[i][t].size(); ++j)
v1.push_back(v[i][t][j]);
for(int j = 0; j < v[i][t^1].size(); ++j)
v2.push_back(v[i][t^1][j]);
}
printf("%d", v1.size());
for(int i = 0; i < v1.size(); ++i) printf(" %d", v1[i]);
printf("\n");
printf("%d", v2.size());
for(int i = 0; i < v2.size(); ++i) printf(" %d", v2[i]);
printf("\n");
} void solve(){
memset(dp, 0, sizeof dp);
dp[0][n] = true;
for(int i = 0; i < cnt; ++i){
for(int j = -n; j <= n; ++j) if(dp[i][j+n]){
dp[i+1][j+n+diff[i]] = true;
dp[i+1][j+n-diff[i]] = true;
}
}
for(int i = 0; i <= n; ++i){
if(dp[cnt][i+n]){ print(i); return ; }
if(dp[cnt][-i+n]){ print(-i); return ; }
}
} int main(){
int T; cin >> T;
while(T--){
scanf("%d", &n);
memset(g, 0, sizeof g);
for(int i = 1; i <= n; ++i){
while(scanf("%d", &m) == 1 && m) g[i][m] = true;
}
memset(color, 0, sizeof color);
bool ok = true;
cnt = 0;
for(int i = 1; i <= n && ok; ++i) if(!color[i]){
v[cnt][0].clear();
v[cnt][1].clear();
ok = dfs(i, 1);
diff[cnt] = (int)v[cnt][0].size() - v[cnt][1].size();
++cnt;
}
if(!ok) puts("No solution");
else solve();
if(T) printf("\n");
}
return 0;
}

UVa 1627 Team them up! (01背包+二分图)的更多相关文章

  1. UVa 1627 - Team them up!——[0-1背包]

    Your task is to divide a number of persons into two teams, in such a way, that: everyone belongs to ...

  2. 【暑假】[深入动态规划]UVa 1627 Team them up!

    UVa 1627 Team them up! 题目: Team them up! Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Forma ...

  3. UVA 562 Dividing coins (01背包)

    题意:给你n个硬币,和n个硬币的面值.要求尽可能地平均分配成A,B两份,使得A,B之间的差最小,输出其绝对值.思路:将n个硬币的总价值累加得到sum,   A,B其中必有一人获得的钱小于等于sum/2 ...

  4. UVA 562 Dividing coins(dp + 01背包)

    Dividing coins It's commonly known that the Dutch have invented copper-wire. Two Dutch men were figh ...

  5. uva 562 Dividing coins(01背包)

      Dividing coins  It's commonly known that the Dutch have invented copper-wire. Two Dutch men were f ...

  6. UVa 12563 劲歌金曲(0-1背包)

    https://vjudge.net/problem/UVA-12563 题意: 在一定的时间内连续唱歌,最后一首唱11分钟18秒的劲歌金曲,问最多能长多长时间. 思路: 0-1背包问题,背包容量为t ...

  7. UVA 562 Dividing coins【01背包 / 有一堆各种面值的硬币,将所有硬币分成两堆,使得两堆的总值之差尽可能小】

    It's commonly known that the Dutch have invented copper-wire. Two Dutch men were fighting over a nic ...

  8. UVA 1627 Team them up!

    https://cn.vjudge.net/problem/UVA-1627 题目 有n(n≤100)个人,把他们分成非空的两组,使得每个人都被分到一组,且同组中的人相互认识.要求两组的成员人数尽量接 ...

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

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

随机推荐

  1. UVA1422-Processor(二分法+优先队列)

    option=com_onlinejudge&Itemid=8&category=512&page=show_problem&problem=4168"> ...

  2. Ionic + AngularJS angular-translate 国际化本地化解决方案

    欢迎访问我们的网站,网站上有更多关于技术性的交流:http://www.ncloud.hk/技术分享/ionic-plus-angularjs-angular-translate-国际化本地化解决方案 ...

  3. CentOS安装配置

    1.准备安装 1.1 系统简介 CentOS 是什么? CentOS是一个基于Red Hat 企业级 Linux 提供的可自由使用的源代码企业级的 Linux 发行版本.每个版本的 CentOS 都会 ...

  4. mvc 发送QQ邮件

    试图部分代码: @{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; } ...

  5. 九度OJ 1095:2的幂次方 (递归)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:913 解决:626 题目描述: Every positive number can be presented by the exponent ...

  6. (t,p,o) t:p>=o there cannot be more consumer instances in a consumer group than partitions

    https://kafka.apache.org/intro.html Kafka as a Messaging System How does Kafka's notion of streams c ...

  7. C#实现(递归和非递归)高速排序和简单排序等一系列排序算法

        本人由于近期工作用到了一些排序算法.就把几个简单的排序算法.想冒泡排序,选择排序,插入排序.奇偶排序和高速排序等整理了出来,代码用C#代码实现,而且通过了測试.希望能给大家提供參考.     ...

  8. (linux)BSP板级支持包开发理解

    1. 概述 嵌入式系统由硬件环境.嵌入式操作系统和应用程序组成,硬件环境是操作系统和应用程序运行的硬件平台,它随应用的不同而有不同的要求.硬件平台的多样性是嵌入式系统的主要特点,如何使嵌入式操作系统在 ...

  9. vue定义全局变量和全局方法

    一.全局引入文件 1.先定义共用组件 common.vue <script type="text/javascript"> // 定义一些公共的属性和方法 const ...

  10. yii的增删改查

    要想实现数据库的操作,必须首先初始化数据模型. 一.yii的数据库模型的初始化有两种.第一种是利用模型的静态方法model:第二种是实例化一个新的模型.以例子说明: //数据的查询关键代码 $good ...