注意这题要求互相认识
不认识的人之间连一条线
一个人在组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. centos7命令行模式安装&&配置_br0+kvm+虚拟机+添加硬盘+快照及恢复

    KVM创建虚拟机步骤 Submitted by zhaoley on October 18, 2016 - 10:43am 测试环境: 1: 43.243.130.89, CentOS Linux r ...

  2. Gearman1.1.12安装与启动

    1)安装 a)安装gcc4.4环境: i.  yum install gcc44 gcc44-c++ libstdc++44-devel gcc-c++ -y ii. 在/etc/profile中添加 ...

  3. VS2008 MFC截取整个屏幕并保存为jpg格式

    void CMainFrame::OnSavejpg() { // TODO: 在此添加命令处理程序代码 HWND hwnd = this->GetSafeHwnd(); //得到窗口句柄 HD ...

  4. 【211】win10快捷键大全

    参考:win10快捷键大全 win10常用快捷键 • 贴靠窗口:Win +左/右> Win +上/下>窗口可以变为1/4大小放置在屏幕4个角落 • 切换窗口:Alt + Tab(不是新的, ...

  5. 所读STL文章摘要集结

    在网上看了一些STL相关文章,这里将它们的摘要进行集结,方便以后查找. 1.黄常标,林俊义,江开勇.快速成形中STL文件拓扑信息的快速建立.2004 摘要:在分析现有建立拓扑信息方法的基础上,提出基于 ...

  6. bzoj 4403: 序列统计【lucas+组合数学】

    首先,给一个单调不降序列的第i位+i,这样就变成了单调上升序列,设原来数据范围是(l,r),改过之后变成了(l+1,r+n) 在m个数里选长为n的一个单调上升序列的方案数为\( C_m^n \),也就 ...

  7. OpenCv图像像素操作

    1:像素 有两种直接操作像素点的方法: 第一种: 将其转化为numpy.array格式,直接进行操作. 第二种:使用Opencv提供的Get1D,Get2D等函数. 2:获取行和列像素 有一下四个函数 ...

  8. UITableViewCell添加点击时改变字体的颜色、背景、图标

    改变字体颜色:cell.textLabel.highlightedTextColor 改变背景颜色:cell.selectedBackgroundView (必须是图片哦) 改变图标颜色:cell.i ...

  9. FZu Problem 2236 第十四个目标 (线段树 + dp)

    题目链接: FZu  Problem 2236 第十四个目标 题目描述: 给出一个n个数的序列,问这个序列内严格递增序列有多少个?不要求连续 解题思路: 又遇到了用线段树来优化dp的题目,线段树节点里 ...

  10. 复习Java和前端、后端框架等。

    以下便是我开始复习时做的笔记.