注意这题要求互相认识
不认识的人之间连一条线
一个人在组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. 在线制作一键生成微信小程序实现原理之需求分析

    随着微信小程序接口不断的放开,小程序在今年或许是明年必将成为商家的一个标配,这个标配的标准就是要开发周期短,费用低,功能实用.只有这样才能让线下的广大商家快速接入.现在也有好多公司开发出了一键生成快速 ...

  2. 多线程-threading模块2

    从上面例子中发现线程的创建是颇为麻烦的,每创建一个线程都需要创建一个 t(t1.t2....),如果创建的线程较多时这样极其不方便.下面对通过例子进行改进:   #coding:utf-8 impor ...

  3. Table View Programming Guide for iOS---(四)---Navigating a Data Hierarchy with Table Views

    Navigating a Data Hierarchy with Table Views 导航数据表视图层次 A common use of table views—and one to which ...

  4. python: 使用matplotlib的pyplot绘制图表

    工作中需要观察数据的变化趋势,用python写了一段小程序来用显示简单图表,分享出来方便有同样需求的人,matplotlib是个很不错的库. #!encode=utf8 from matplotlib ...

  5. Lua变量

    Lua 变量 变量在使用前,必须在代码中进行声明,即创建该变量. 编译程序执行代码之前编译器需要知道如何给语句变量开辟存储区,用于存储变量的值. Lua 变量有三种类型:全局变量.局部变量.表中的域. ...

  6. bzoj 3872: [Poi2014]Ant colony【树形dp+二分】

    啊我把分子分母混了WA了好几次-- 就是从食蚁兽在的边段成两棵树,然后dp下去可取的蚂蚁数量区间,也就是每次转移是l[e[i].to]=l[u](d[u]-1),r[e[i].to]=(r[u]+1) ...

  7. 第六篇 .NET高级技术之拆箱装箱

    拆箱.装箱 值类型赋值给Object类型变量的时候,会发生装箱:包装成Object.ValueType不也是继承自Object吗(CLR内部处理): Object类型变量赋值给值类型赋值的时候会发生拆 ...

  8. pycharm 添加个人信息

    2. 可以使用搜索快速找到"File and Code Templates", 右侧菜单选择"Python Script",对模板进行编辑 格式为: ${< ...

  9. MySql | 常用操作总结

    创建数据库: CREATE DATABASE 数据库名; 删除数据库名: drop database <数据库名>; 选择数据库: use 数据库名; 创建数据表: CREATE TABL ...

  10. Codeforces Round #402 (Div. 2) D

    Description Little Nastya has a hobby, she likes to remove some letters from word, to obtain another ...