注意这题要求互相认识
不认识的人之间连一条线
一个人在组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!的更多相关文章

  1. 【杂题总汇】UVa-1627 Team them up!

    [UVa-1627] Team them up! 借鉴了一下hahalidaxin的博客……了解了思路,但是莫名Wa了:最后再找了一篇dwtfukgv的博客才做出来

  2. 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 ...

  3. Create a Team in RHEL7

    SOLUTION VERIFIED September 13 2016 KB2620131 Environment Red Hat Enterprise Linux 7 NetworkManager ...

  4. Team Leader 你不再只是编码, 来炖一锅石头汤吧

    h3{ color: #000; padding: 5px; margin-bottom: 10px; font-weight: bolder; background-color: #ccc; } h ...

  5. 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 ...

  6. BZOJ 4742: [Usaco2016 Dec]Team Building

    4742: [Usaco2016 Dec]Team Building Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 21  Solved: 16[Su ...

  7. 关于 feature team 的一些内容

    矩阵式管理,是常见的经典管理架构.其最早起源于美国的航空航天部门,然后被美国人带到了日本,然后被日本人带到了台湾,然后台湾人带到大陆...矩阵管理最典型的特征是,组织架构按职能与专业划分,项目由跨越部 ...

  8. 病毒四度升级:安天AVL Team揭露一例跨期两年的电信诈骗进化史

    自2014年9月起,安天AVL移动安全团队持续检测到一类基于Android移动平台的间谍类病毒,病毒样本大多伪装成名为"最高人民检察院"的应用.经过反编译逆向分析以及长期的跟踪调查 ...

  9. iOS 真机测试时报错:Provisioning profile "iOS Team Provisioning Profile: XXX” doesn't include the currently selected device “XXX”.

    这几天因工作需要,去给客户演示iOS项目打包的过程.之前演示都是顺利的,但后来客户自己操作时打电话说遇到了问题,出现报错. 就过去看了一下,发现一个很陌生的错误提示: The operation co ...

随机推荐

  1. NOIP2007 矩阵取数游戏(区间DP)

    传送门 这道题第一眼看上去可能让人以为是贪心……不过贪心并不行,因为每次的操作是有2的幂次方的权值的.这样的话直接每次贪心最小的就目光短浅.所以那我们自然想到了DP. 据说这是一道很正常的区间DP? ...

  2. 使用strtok_s函数从一个字符串中分离出单词

    下面的代码从含有多个结束符的字符串中分离出单词来,需要对strtok_s有清楚的认识.这段代码是我在写一个处理文件中单词个数时用来分离读取到的字符串中的单词时写的,亲测可用~ 1 2 3 4 5 6 ...

  3. ARC和MRC混合使用

    在一些项目中尤其是做迭代的项目经常会出现MRC的项目,但是我们习惯了ARC环境,反之也是一样.这是我们不必去修改代码去掉release之类的,按照如下方案去做就可以了. 项目 -> Build ...

  4. 跟我一起玩Win32开发(11):使用控件——先来耍一下按钮

    用户通过控件与应用程序交互,在吹牛之前,先介绍一个工具,这是官方的工具,使用它,你可以预览常用控件的外观.样式,以及对控进行操作时接收和发送哪些消息.下载地址如下: http://www.micros ...

  5. C++11六大函数(构造函数,移动构造函数,移动赋值操作符,复制构造函数,赋值操作符,析构函数)

    在C++中,有三大函数复制控制(复制构造函数,赋值操作符,析构函数),而在C++11中,加入了移动构造函数,移动赋值操作符.我就斗胆将他们命名为六大函数好了. 一.构造函数 c++primer中说过: ...

  6. Android课程设计第三天帧动画区间动画

    注意:课程设计只为完成任务,不做细节描述~ 点火是帧动画,发射是区间动画,于是 <?xml version="1.0" encoding="utf-8"? ...

  7. April Fools Contest 2017 E

    Description Input The input consists of four lines, each line containing a single digit 0 or 1. Outp ...

  8. 模拟 HDOJ 5095 Linearization of the kernel functions in SVM

    题目传送门 /* 题意:表达式转换 模拟:题目不难,也好理解题意,就是有坑!具体的看测试样例... */ #include <cstdio> #include <algorithm& ...

  9. 动手实现 React-redux(二):结合 context 和 store

    既然要把 store 和 context 结合起来,我们就先构建 store.在 src/index.js 加入之前创建的 createStore 函数,并且构建一个 themeReducer 来生成 ...

  10. c++ 如何对拍

    首先要写好两个要对拍程序(假设是A,B),和一个制造数据的程序(设为made)   (要放在同一文件夹内) 编译得到A.exe , B.exe ,  made.exe 写一个对拍器 格式如下 @ech ...