【Uva 1627】Team them up!
【Link】:
【Description】
给你n个人;
有一些人之间有认识关系
a认识b,b不一定认识a
让你把这n个人分成两组
使得这两组中的每一组:
组内的人与人之间都相互认识.
并且,使得两组的人数之差尽可能小;
输出分组的方案;
【Solution】
如果A和B不是相互认识
那么他们俩肯定是在不同的组
则,在不是相互认识的人之间建一条边;
这样就能形成一张图了;
并且可能有多个连通块
然后,对每个连通块做二分染色;
如果全都能做二分染色;
则有解
这样,每个连通块都有的点被染成0,有的被染成1
我们相当于要对每个连通块都做出决策
到底是把0放到A组,1放到B组
还是把0放到B组,1放到A组;
动态规划;
设f[i][j]表示前i个连通块,A组的人数比B组多j的情况是否能得到;
设x为第i+1个连通块0的个数,y为第i+1个连通块1的个数;
如果f[i][j]==1
则
f[i+1][j+x−y]=1
f[i+1][j+y−x]=1
两个赋值,对应了两种决策;
最后j从0..n枚举
找最小的f[tot][abs(j)]==1的j;
然后递归获得方案即可;
需要用一个数组记录方案
【NumberOf WA】
0
【Reviw】
图论模型的转化.
动态规划解决问题。
【Code】
#include <bits/stdc++.h>
using namespace std;
const int N = 100;
vector <int> G[N+10],v[2];
map <int,int> dic[N+10];
map <int,pair<int,pair<int,int> > > pre[N+10];
int n,g[N+10][N+10],a[N+10][2],cnt[2],tot,idx;
pair <int,int> color[N+10];
pair <int,int> temp;
bool dfs(int x,int p){
color[x] = make_pair(p,idx);
cnt[p]++;
bool ok = true;
int len = G[x].size();
for (int i = 0; i <= len-1;i++){
int y = G[x][i];
if (color[y].first!=-1){
if (color[y].first != (1-p))
ok = false;
}else
ok =ok && dfs(y,1-p);
}
return ok;
}
void print(int now,int j){
if (now==0) return;
int xx = pre[now][j].second.first,yy = pre[now][j].second.second;
for (int i = 1;i <= n;i++)
if (color[i].second==now){
if (color[i].first == 0)
v[xx].push_back(i);
else
v[yy].push_back(i);
}
print(now-1,pre[now][j].first);
}
int main()
{
//freopen("F:\\rush.txt","r",stdin);
int T;
scanf("%d",&T);
while (T--){
for (int i = 1;i <= N;i++) {
G[i].clear();
a[i][0] = a[i][1] = 0;
color[i] = make_pair(-1,0);
dic[i].clear();
}
for (int i = 1;i <= N;i++)
for (int j = 1;j <= N;j++)
g[i][j] = 0;
scanf("%d",&n);
for (int i = 1;i <= n;i++){
int x;
scanf("%d",&x);
while (x!=0){
g[i][x] = 1;
scanf("%d",&x);
}
}
for (int i = 1;i <= n;i++)
for (int j = i+1;j <= n;j++){
if (g[i][j] && g[j][i]) continue;
G[i].push_back(j),G[j].push_back(i);
}
tot = 0;
bool sol = true;
for (int i = 1;i <= n;i++)
if (color[i].first==-1){
cnt[0] = cnt[1] = 0;
idx = ++tot;
sol = sol && dfs(i,0);
a[tot][0] = cnt[0];
a[tot][1] = cnt[1];
}
if (!sol){
puts("No solution");
puts("");
continue;
}
dic[0][0] = 1;
for (int i = 0;i <= tot-1;i++)
for (int j = -n;j <= n;j++)
if (dic[i][j]){
int x = a[i+1][0],y = a[i+1][1];
int t = x-y;
if (!dic[i+1][j+t]){
dic[i+1][j+t] = 1;
pre[i+1][j+t] = make_pair(j,make_pair(0,1));
}
if (!dic[i+1][j-t]){
dic[i+1][j-t] = 1;
pre[i+1][j-t] = make_pair(j,make_pair(1,0));
}
}
v[0].clear(),v[1].clear();
for (int j = 0;j <= n;j++){
if (dic[tot][j]){
print(tot,j);
break;
}
if (dic[tot][-j]){
print(tot,j);
break;
}
}
if (v[0].empty() || v[1].empty())
puts("No solution");
else
for (int t = 0;t <= 1;t++){
printf("%d ",(int) v[t].size());
for (int i = 0;i <= (int) v[t].size()-1;i++){
printf("%d",v[t][i]);
if (i==(int) v[t].size()-1)
puts("");
else
putchar(' ');
}
}
puts("");
}
return 0;
}
【Uva 1627】Team them up!的更多相关文章
- 【UVA - 540】Team Queue (map,队列)
Team Queue Descriptions: Queues and Priority Queues are data structures which are known to most comp ...
- 【巧妙算法系列】【Uva 11464】 - Even Parity 偶数矩阵
偶数矩阵(Even Parity, UVa 11464) 给你一个n×n的01矩阵(每个元素非0即1),你的任务是把尽量少的0变成1,使得每个元素的上.下.左.右的元素(如果存在的话)之和均为偶数.比 ...
- 【贪心+中位数】【UVa 11300】 分金币
(解方程建模+中位数求最短累积位移) 分金币(Spreading the Wealth, UVa 11300) 圆桌旁坐着n个人,每人有一定数量的金币,金币总数能被n整除.每个人可以给他左右相邻的人一 ...
- 【UVa 10881】Piotr's Ants
Piotr's Ants Porsition:Uva 10881 白书P9 中文改编题:[T^T][FJUT]第二届新生赛真S题地震了 "One thing is for certain: ...
- 【UVa 116】Unidirectional TSP
[Link]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- 【UVa 1347】Tour
[Link]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- 【UVA 437】The Tower of Babylon(记忆化搜索写法)
[题目链接]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- 【uva 1025】A Spy in the Metro
[题目链接]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- 【POJ 2259】 Team Queue
[题目链接] http://poj.org/problem?id=2259 [算法] 由题,一个人入队时,若这个人所在的组已经有人在队列中,则加入队列,否则排到队末 因此我们发现,这个队列一定是由连续 ...
随机推荐
- iOS RegexKitLite 提取匹配的内容
使用RegexKitLite正则表达式需要以下工作: 1.RegexKitLite官方网址(内含使用教程):http://regexkit.sourceforge.net/RegexK ...
- 13款用于拍摄全景照片的iOS应用
全景图是一种大画幅.用来展示尽量多的周围环境的照片,甚至能够展示一个球状的完整空间,让观赏者直接“站在”摄影师的位置,在照片里将该环境一览无余.全景照片能够以最直观的方式向人们展示和记录一个美丽风景的 ...
- 腾讯之困,QQ与微信各有各的烦恼
QQ渐渐在腾讯内部弱化 在PC时代,QQ是即时通讯领域当之无愧的王者.但在微信崛起后,手机QQ未来会被微信替代的判断喧嚣至上. 早在2012年就有传言腾讯在游戏领域開始去"娱乐化" ...
- C# Winform 模拟QQ新闻弹出框
一开始做的时候,觉得这个太简单了.真心做的时候还是遇到了不少的坑啊. 1)循环播放新闻内容,建议使用showdialog(),不要用show(),不太好控制前后之间的停顿. 2)窗口的初始位置为有下角 ...
- bzoj1193: [HNOI2006]马步距离(贪心+bfs)
1193: [HNOI2006]马步距离 题目:传送门 题解: 毒瘤题... 模拟赛时的一道题,刚开始以为是一道大难题...一直在拼命找规律 结果.... 还是说正解吧: 暴力的解法肯定是直接bfs, ...
- 泪奔,配好了bioconductor环境
最近因为极度忙,没有写总结.今天补一下总结. 今天完成关静最后给的大project这个作业来说,结合自己的研究方向是个让我纠结一周多的事.好在找到了对应的研究内容. R的书目前还是很多的.R我一开始觉 ...
- <Sicily>Pair
一.题目描述 The N cities of Estiah are connected by N-1 roads. The roads are built in a way that it's alw ...
- vue.js技巧小计
//删除数组索引方法01 del (index) { this.arr.splice(index ,1); } //删除数组索引方法01 del (index) { this.$delete(this ...
- Qihoo 360 altas 实践
Qihoo 360 altas 实践 简介 Atlas是由 Qihoo 360公司Web平台部基础架构团队开发维护的一个基于MySQL协议的数据中间层项目.它在MySQL官方推出的MySQL-Prox ...
- 888E - Maximum Subsequence 中途相遇法
Code: #include<cstdio> #include<algorithm> #include<cstring> #include<string> ...