uva1627 Team them up!
注意这题要求互相认识
不认识的人之间连一条线
一个人在组1,那么不认识(互相认识)的人就在组0;同时这些人不认识的人就在组1.每个联通分量都可以独立推导,遇到矛盾则无解
一个联通分量有一个核心,其他的点是分支
我感觉紫书写的样例又是有点问题,应该是4在0,那么135在1,反正则反;而不是1在0,345在1
然后一个联通分量的核心在一组,分支就在另一组;
设0组比1组多d,就对应d加一个或者减一个值
相当于背包
矛盾状况就是任意个联通分量不能二分染色
#include<cstdio>
#include<cstring>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std; const int maxn = + ; int n, G[maxn][maxn], color[maxn], diff[maxn], cc;
vector<int> team[maxn][]; // team[cc][c] is the list of people in connected-component cc, color c // returns false if not bipartite graph
bool dfs(int u, int c) {
color[u] = c;
team[cc][c-].push_back(u);
for(int v = ; v < n; v++) {
if(u != v && !(G[u][v] && G[v][u])) { // u and v 不互相认识
if(color[v] > && color[v] == color[u]) return false;
if(!color[v] && !dfs(v, -c)) return false;
}
}
return true;
} bool build_graph() {
memset(color, , sizeof(color));
cc = ; // current connected-component
for(int i = ; i < n; i++)
if(!color[i]) {
team[cc][].clear(); //存染成1的
team[cc][].clear(); //染成2的
if(!dfs(i, ))
return false;
diff[cc] = team[cc][].size() - team[cc][].size();
cc++;
} return true;
} // d[i][j+n] = 1 iff we can arrange first i cc so that team 1 has j more people than team 2.
int d[maxn][maxn*], teamno[maxn]; void print(int ans) {
vector<int> team1, team2;
for(int i = cc-; i >= ; i--) {
int t;
if(d[i][ans-diff[i]+n]) {
t = ;
ans -= diff[i];
}
else {
t = ;
ans += diff[i];
}
for(int j = ; j < team[i][t].size(); j++)
team1.push_back(team[i][t][j]);
for(int j = ; j < team[i][^t].size(); j++)
team2.push_back(team[i][^t][j]);
} printf("%d", team1.size());
for(int i = ; i < team1.size(); i++)
printf(" %d", team1[i]+);
printf("\n");
printf("%d", team2.size());
for(int i = ; i < team2.size(); i++)
printf(" %d", team2[i]+);
printf("\n");
} void dp() {
memset(d, , sizeof(d));
d[][+n] = ;
for(int i = ; i < cc; i++)
for(int j = -n; j <= n; j++) if(d[i][j+n]) {
d[i+][j+diff[i]+n] = ;
d[i+][j-diff[i]+n] = ;
}
for(int ans = ; ans <= n; ans++) { //差的绝对值尽量小即可
if(d[cc][ans+n]) { print(ans); return; }
if(d[cc][-ans+n]) { print(-ans); return; }
}
} int main() {
int T;
cin >> T;
while(T--) {
cin >> n;
memset(G, , sizeof(G));
for(int u = ; u < n; u++) {
int v;
while(cin >> v && v) G[u][v-] = ; //认识
} if(n == || !build_graph())
cout << "No solution\n";
else
dp();
cout << "\n";
}
return ;
}
uva1627 Team them up!的更多相关文章
- 【杂题总汇】UVa-1627 Team them up!
[UVa-1627] Team them up! 借鉴了一下hahalidaxin的博客……了解了思路,但是莫名Wa了:最后再找了一篇dwtfukgv的博客才做出来
- Configure a VLAN on top of a team with NetworkManager (nmcli) in RHEL7
SOLUTION VERIFIED September 13 2016 KB1248793 Environment Red Hat Enterprise Linux 7 NetworkManager ...
- Create a Team in RHEL7
SOLUTION VERIFIED September 13 2016 KB2620131 Environment Red Hat Enterprise Linux 7 NetworkManager ...
- Team Leader 你不再只是编码, 来炖一锅石头汤吧
h3{ color: #000; padding: 5px; margin-bottom: 10px; font-weight: bolder; background-color: #ccc; } h ...
- Configure bridge on a team interface using NetworkManager in RHEL 7
SOLUTION IN PROGRESS February 29 2016 KB2181361 environment Red Hat Enterprise Linux 7 Teaming,Bridg ...
- BZOJ 4742: [Usaco2016 Dec]Team Building
4742: [Usaco2016 Dec]Team Building Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 21 Solved: 16[Su ...
- 关于 feature team 的一些内容
矩阵式管理,是常见的经典管理架构.其最早起源于美国的航空航天部门,然后被美国人带到了日本,然后被日本人带到了台湾,然后台湾人带到大陆...矩阵管理最典型的特征是,组织架构按职能与专业划分,项目由跨越部 ...
- 病毒四度升级:安天AVL Team揭露一例跨期两年的电信诈骗进化史
自2014年9月起,安天AVL移动安全团队持续检测到一类基于Android移动平台的间谍类病毒,病毒样本大多伪装成名为"最高人民检察院"的应用.经过反编译逆向分析以及长期的跟踪调查 ...
- iOS 真机测试时报错:Provisioning profile "iOS Team Provisioning Profile: XXX” doesn't include the currently selected device “XXX”.
这几天因工作需要,去给客户演示iOS项目打包的过程.之前演示都是顺利的,但后来客户自己操作时打电话说遇到了问题,出现报错. 就过去看了一下,发现一个很陌生的错误提示: The operation co ...
随机推荐
- data对象转化成后端需要的json格式
data=JSON.stringify(json_data); $.ajax({type:'post',url:url+'warehouse/create_alliance_out/',data:da ...
- 关于base64编码的原理及实现
我们的图片大部分都是可以转换成base64编码的data:image. 这个在将canvas保存为img的时候尤其有用.虽然除ie外,大部分现代浏览器都已经支持原生的基于base64的encode和d ...
- centos7 编译安装新版LNMP环境
centos7 编译安装新版LNMP环境 环境版本如下: 1.系统环境:Centos 7 x86_64 2.NGINX:nginx-1.11.3.tar.gz 3.数据库:mariadb-10.0.2 ...
- VS2010中编写x64汇编的具体方法
编写涉及系统特性的一些底层程序,特别是ShellCode,不可避免地要采用直接编写汇编代码的方式. 在目标平台为x86模式时,可以直接使用内联汇编,这个很多人都比较熟悉了,也非常地方便. 但是当目标平 ...
- k8s-基于canel的网络策略-十九
一.前提 上一节学习了flannel,但是我们应该了解flannel只能提供网络通讯,而不能提供网络策略.因此,我们本节学习canal,让它来提供网络策略,来配合flannel使用. canal是ca ...
- E20180408-hm
interaction n. 互动; 一起活动; 合作; 互相影响; interface n. 界面; <计>接口; 交界面; v. (使通过界面或接口) 接合,连接; [计算机] ...
- hdoj1528【二分匹配】
题意: 在一幅扑克牌里,有两个人在比大小,第二个人最多能赢第一个人几张牌. 思路: 这道题目用一下二分匹配还是很明显的. 那么就是建图似乎要麻烦一下,但还是很方便的.将扑克牌一次进行编号,然后牌面比他 ...
- bzoj 4551: [Tjoi2016&Heoi2016]树【并查集】
看起来像是并查集,但是是拆集合,考虑时间倒流,先把标记都打上,然后把并查集做出来 每次到一个修改点就把这个点的计数s[u]--,当这个s为0时就把这个点和他的父亲合并(因为可能有多次标记) #incl ...
- mui中一级联动
<!doctype html><html> <head> <meta charset="utf-8"> <title>& ...
- vlc media player
还是很好用的目前来看 倍速播放: [ 减速播放 ] 加速播放 = 恢复原速度