题意:给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. ActiveMQ 消息持久化到Mysql数据库

    [root@txylucky local]# tar -zxvf apache-activemq-5.15.8-bin.tar.gz[root@txylucky local]# mv apache-a ...

  2. 解读OC中的load和initialize

    在 Objective-C 中,NSObject 是绝大多数类的基类.而在 NSObject 中有两个类方法 load 和 initialize,那这两个方法是在什么时机被调用呢?父类.Categor ...

  3. Graphics and Animation in iOS

     using System;using UIKit;using CoreGraphics;using Foundation; namespace GraphicsAnimation{ public c ...

  4. Android-基本控件和详解四种布局方式

    转自:https://www.cnblogs.com/ludashi/p/4883915.html 一.常用基本控件 1.TextView 看到Android中的TextView, 我不禁的想到了iO ...

  5. 个人开发者帐号--我是如何实现在另一台mac上真机调试的

    本文转载至 : http://blog.csdn.net/chenyong05314/article/details/8689534   注:本人有一台mac电脑,之前申请开发者帐号的时候直接就是在这 ...

  6. Transforming Auto-encoders

    http://www.cs.toronto.edu/~hinton/absps/transauto6.pdf The artificial neural networks that are used ...

  7. Redis 单例、主从模式、sentinel 以及集群的配置方式及优缺点对比(转)

    摘要: redis作为一种NoSql数据库,其提供了一种高效的缓存方案,本文则主要对其单例,主从模式,sentinel以及集群的配置方式进行说明,对比其优缺点,阐述redis作为一种缓存框架的高可用性 ...

  8. 关于hibernate配置步骤

    1.导入jar包,根据连接数据库不同改变数据库jar包 2.创建hibernate.cfg.xml文件 几个常用的参数作用: connection.url:表示数据库URL,不同数据库有不同写法 a. ...

  9. 02-线性结构3 Reversing Linked List(25 point(s)) 【链表】

    02-线性结构3 Reversing Linked List(25 point(s)) Given a constant K and a singly linked list L, you are s ...

  10. 通过 :hover 伪元素控制其他元素

    ---代码 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <tit ...