UVa 1627 Team them up! (01背包+二分图)
题意:给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背包+二分图)的更多相关文章
- 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 ...
 - 【暑假】[深入动态规划]UVa 1627 Team them up!
		
UVa 1627 Team them up! 题目: Team them up! Time Limit: 3000MS Memory Limit: Unknown 64bit IO Forma ...
 - UVA 562 Dividing coins (01背包)
		
题意:给你n个硬币,和n个硬币的面值.要求尽可能地平均分配成A,B两份,使得A,B之间的差最小,输出其绝对值.思路:将n个硬币的总价值累加得到sum, A,B其中必有一人获得的钱小于等于sum/2 ...
 - UVA 562 Dividing coins(dp + 01背包)
		
Dividing coins It's commonly known that the Dutch have invented copper-wire. Two Dutch men were figh ...
 - uva 562 Dividing coins(01背包)
		
Dividing coins It's commonly known that the Dutch have invented copper-wire. Two Dutch men were f ...
 - UVa 12563 劲歌金曲(0-1背包)
		
https://vjudge.net/problem/UVA-12563 题意: 在一定的时间内连续唱歌,最后一首唱11分钟18秒的劲歌金曲,问最多能长多长时间. 思路: 0-1背包问题,背包容量为t ...
 - UVA 562 Dividing coins【01背包 / 有一堆各种面值的硬币,将所有硬币分成两堆,使得两堆的总值之差尽可能小】
		
It's commonly known that the Dutch have invented copper-wire. Two Dutch men were fighting over a nic ...
 - UVA 1627 Team them up!
		
https://cn.vjudge.net/problem/UVA-1627 题目 有n(n≤100)个人,把他们分成非空的两组,使得每个人都被分到一组,且同组中的人相互认识.要求两组的成员人数尽量接 ...
 - POJ 1112 Team Them Up! 二分图判定+01背包
		
题目链接: http://poj.org/problem?id=1112 Team Them Up! Time Limit: 1000MSMemory Limit: 10000K 问题描述 Your ...
 
随机推荐
- 命令+mybatis-generator插件自己主动生成Mapper映射文件
			
学mybatis的时候,自己写各种 *Mapper.xml和 *Mapper.java,注意各种sql语句中的 id 是否匹配.xml中的namespace是否正确,非常麻烦有木有?今天博客内容就是高 ...
 - Process Autocad by python
			
一.处理AutoCad模块 -pyautocad 1.安装 pip install pyautocad 注:1.该操作会自动安装 comtypes模块,如果其他方式安装,请自行安装comtypes模块 ...
 - Windows App开发之集合控件与数据绑定
			
为ListView和GridView加入数据 ListView採用垂直堆叠得方式显示数据.而GridView则採用水平堆叠得方式. 长相的话嘛,它们都几乎相同. <Grid Name=" ...
 - .NET 4.0 WCF WebConfig aspNetCompatibilityEnabled 属性
			
近来被一个问题困扰了好久,好好的一个WCF后台服务,在发布机器上可用.在自己机器上没法跑起来. 一直提示兼容性问题,后来在网上找来解决方案,但问题依旧.没办法又从客户的服务器上重新把配置内容 拿下来审 ...
 - 最新wap手机agent
			
名称 agent 铃声格式 和弦数 数据量 删除 LGE-CU8080 LGE-CU8080/1.0 UP.Browser/4.1.26l UP.Link/5.1.2.9 pmd2.0 40 ...
 - RS485总线典型电路介绍
			
一.RS485总线介绍: RS485总线是一种常见的串行总线标准,采用平衡发送与差分接收的方式,因此具有抑制共模干扰的能力.在一些要求通信距离为几十米到上千米的时候,RS485总线是一种应用最为广泛的 ...
 - 【BZOJ4811】[Ynoi2017]由乃的OJ 树链剖分+线段树
			
[BZOJ4811][Ynoi2017]由乃的OJ Description 由乃正在做她的OJ.现在她在处理OJ上的用户排名问题.OJ上注册了n个用户,编号为1-",一开始他们按照编号排名. ...
 - windows  复制 文本文件内容  到剪切板
			
shell 打开 type filename | clip
 - yuicompressor
			
yui/yuicompressor: YUI Compressor https://github.com/yui/yuicompressor YUI Compressor 详细介绍 YUI ...
 - Asynchronous programming with async and await (C#)
			
Asynchronous Programming with async and await (C#) | Microsoft Docs https://docs.microsoft.com/en-us ...