注意这题要求互相认识
不认识的人之间连一条线
一个人在组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. js 模拟a标签打开新网页

      在这里备份一下,方便以后查找.   var el = document.createElement("a"); document.body.appendChild(el); e ...

  2. B. Color the Fence

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  3. 如何升级xcode 中的cocos2dx 到v2.2.2以上版本

    每次升级cocos2dx版本都觉得不知道怎么弄才行. 这次升级到v2.2.2版本又花了我不少时间.因此在这里分享一下,以后也有地方可以查询. 1. 到http://cocos2d-x.org/ 下载最 ...

  4. iOS 拼音 Swift K3Pinyin

    iOS 系统方法支持直接获取拼音,避免了之前各种第三方引入各种MAP或者资源文件.下面是一个Swift版本的简单示例: // swift 4.0 func pinyin(_ string: Strin ...

  5. 骨骼蒙皮动画(Skinned Mesh)的原理解析(二)

    http://blog.csdn.net/jimoshuicao/article/details/9283071 2)蒙皮信息和蒙皮过程 2-1)Skin info的定义 上文曾讨论过,Skinned ...

  6. bzoj 1874: [BeiJing2009 WinterCamp]取石子游戏【博弈论】

    先预处理出来sg值,然后先手必败状态就是sg[a[i]]的xor和为0(nim) 如果xor和不为0,那么一定有办法通过一步让xor和为0,具体就是选一个最大的sg[a[i]],把它去成其他sg值的x ...

  7. P5072 [Ynoi2015]盼君勿忘

    传送门 一开始理解错题意了--还以为是两个子序列相同的话只算一次--结果是子序列里相同的元素只算一次-- 对于一个区间\([l,r]\),设其中\(x\)出现了\(k\)次,那么它的贡献就是它的权值乘 ...

  8. bzoj4145 [AMPPZ2014]The Prices(状压dp)

    Description 你要购买m种物品各一件,一共有n家商店,你到第i家商店的路费为d[i],在第i家商店购买第j种物品的费用为c[i][j], 求最小总费用. Input 第一行包含两个正整数n, ...

  9. jmeter远程运行GUI多用户负载

    1.在Jmeter控制机的bin目录下找到jmeter.properties文件并修改”remote_hosts”,增加负载机IP,多个IP使用英文逗号隔开,修改后要重启Jmeter.如下图: 2.添 ...

  10. python实现判断素数

    import math def is_prime_1(n): if n <= 1: return False for i in range(2, int(math.sqrt(n) + 1)): ...