POJ1112 Team Them Up![二分图染色 补图 01背包]
Time Limit: 1000MS | Memory Limit: 10000K | |||
Total Submissions: 7608 | Accepted: 2041 | Special Judge |
Description
everyone belongs to one of the teams;
every team has at least one member;
every person in the team knows every other person in his team;
teams are as close in their sizes as possible.
This task may have many solutions. You are to find and output any solution, or to report that the solution does not exist.
Input
The first line in the input file contains a single integer number N (2 <= N <= 100) - the total number of persons to divide into teams, followed by N lines - one line per person in ascending order of their identifiers. Each line contains the list of distinct numbers Aij (1 <= Aij <= N, Aij != i) separated by spaces. The list represents identifiers of persons that ith person knows. The list is terminated by 0.
Output
Sample Input
5
2 3 5 0
1 4 5 3 0
1 2 5 0
1 2 3 0
4 3 2 1 0
Sample Output
3 1 3 5
2 2 4
Source
题意:
白书
一个N个节点的有向图,将节点分成两个集合,满足以下四个条件:
1。每个节点属于其中一个集合
2。每个集合至少有一个节点
3。集合里的每一个节点都有边连向同一个集合里的其他点
4。被分成的两个集合的大小要尽量接近
//更新写法
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
const int N=;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
}
int n,g[N][N];
struct edge{
int v,ne;
}e[N*N<<];
int h[N],cnt=;
void ins(int u,int v){
cnt++;
e[cnt].v=v;e[cnt].ne=h[u];h[u]=cnt;
cnt++;
e[cnt].v=u;e[cnt].ne=h[v];h[v]=cnt;
}
int col[N],cc=,tm[N][][N],p[N][];//team p
bool dfs(int u,int c){
col[u]=c;
tm[cc][c][++p[cc][c]]=u;
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v;
if(col[u]==col[v]) return false;
if(!col[v]&&!dfs(v,-c)) return false;
}
return true;
}
int w[N];
bool init(){
for(int i=;i<=n;i++) if(!col[i]){
cc++;
if(!dfs(i,)) return false;
w[cc]=p[cc][]-p[cc][];//printf("w %d %d\n",cc,w[cc]);
}
return true;
}
int f[N][N<<],pa[N][N<<]; void dp(){
f[][+n]=;
for(int i=;i<cc;i++)
for(int j=-n;j<=n;j++)
if(f[i][j+n]) f[i+][j+n+w[i+]]=f[i+][j+n-w[i+]]=;
} int t1[N],t2[N],c1,c2;
void print(int s){
for(int i=cc;i>=;i--){
int flag=;
if(f[i-][s+n-w[i]]){flag=;s-=w[i];}//the color for t1
else{flag=;s+=w[i];}
//printf("s %d\n",s);
for(int j=;j<=p[i][flag];j++) t1[++c1]=tm[i][flag][j];
flag=-flag;
for(int j=;j<=p[i][flag];j++) t2[++c2]=tm[i][flag][j];
} printf("%d ",c1);
for(int i=;i<=c1;i++) printf("%d ",t1[i]);
printf("\n%d ",c2);
for(int i=;i<=c2;i++) printf("%d ",t2[i]);
} int main(int argc, const char * argv[]) {
n=read();
for(int i=;i<=n;i++){
int v=read();
while(v!=) g[i][v]=,v=read();
}
for(int i=;i<=n;i++)
for(int j=i+;j<=n;j++)
if(g[i][j]==||g[j][i]==) ins(i,j);
if(!init()||n==) printf("No solution");
else{
dp();
for(int i=;i<=n;i++){
//printf("hi %d %d %d\n",cc,n+i,n-i);
if(f[cc][n+i]) {print(i);break;}
if(f[cc][n-i]) {print(-i);break;}
}
}
return ;
}
//普通
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
const int N=;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
}
int n,g[N][N];
struct edge{
int v,ne;
}e[N*N<<];
int h[N],cnt=;
void ins(int u,int v){
cnt++;
e[cnt].v=v;e[cnt].ne=h[u];h[u]=cnt;
cnt++;
e[cnt].v=u;e[cnt].ne=h[v];h[v]=cnt;
}
int col[N],cc=,tm[N][][N],p[N][];//team p
bool dfs(int u,int c){
col[u]=c;
tm[cc][c][++p[cc][c]]=u;
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v;
if(col[u]==col[v]) return false;
if(!col[v]&&!dfs(v,-c)) return false;
}
return true;
}
int w[N];
bool init(){
for(int i=;i<=n;i++) if(!col[i]){
cc++;
if(!dfs(i,)) return false;
w[cc]=p[cc][]-p[cc][];//printf("w %d %d\n",cc,w[cc]);
}
return true;
}
int f[N][N<<],pa[N][N<<]; void dp2(){
f[][+n]=;
for(int i=;i<=cc;i++)
for(int j=-n;j<=n;j++){
if(n+j-w[i]>=&&f[i-][n+j-w[i]]){
f[i][j+n]=;
pa[i][j+n]=;//zheng zhe fen pei
}else if(n+j+w[i]<=*n&&f[i-][n+j+w[i]]){
f[i][j+n]=;
pa[i][j+n]=-;
}
//printf("f %d %d %d\n",i,j,f[i][j]);
}
} int t1[N],t2[N],c1,c2; void print2(int s){
for(int i=cc;i>=;i--){
int flag=;
if(pa[i][s+n]==) {flag=;s-=w[i];}
else {flag=;s+=w[i];} for(int j=;j<=p[i][flag];j++) t1[++c1]=tm[i][flag][j];
flag=-flag;
for(int j=;j<=p[i][flag];j++) t2[++c2]=tm[i][flag][j];
}
printf("%d ",c1);
for(int i=;i<=c1;i++) printf("%d ",t1[i]);
printf("\n%d ",c2);
for(int i=;i<=c2;i++) printf("%d ",t2[i]);
} int main(int argc, const char * argv[]) {
n=read();
for(int i=;i<=n;i++){
int v=read();
while(v!=) g[i][v]=,v=read();
}
for(int i=;i<=n;i++)
for(int j=i+;j<=n;j++)
if(g[i][j]==||g[j][i]==) ins(i,j);
if(!init()||n==) printf("No solution");
else{
dp2(); for(int i=;i<=n;i++){
//printf("hi %d %d %d\n",cc,n+i,n-i);
if(f[cc][n+i]) {print2(i);break;}
if(f[cc][n-i]) {print2(-i);break;}
}
}
return ;
}
POJ1112 Team Them Up![二分图染色 补图 01背包]的更多相关文章
- POJ2942 Knights of the Round Table[点双连通分量|二分图染色|补图]
Knights of the Round Table Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 12439 Acce ...
- POJ 1112 Team Them Up! 二分图判定+01背包
题目链接: http://poj.org/problem?id=1112 Team Them Up! Time Limit: 1000MSMemory Limit: 10000K 问题描述 Your ...
- POJ2942 Knights of the Round Table【Tarjan点双联通分量】【二分图染色】【补图】
LINK 题目大意 有一群人,其中有一些人之间有矛盾,现在要求选出一些人形成一个环,这个环要满足如下条件: 1.人数大于1 2.总人数是奇数 3.有矛盾的人不能相邻 问有多少人不能和任何人形成任何的环 ...
- HDU 5313 Bipartite Graph(二分图染色+01背包水过)
Problem Description Soda has a bipartite graph with n vertices and m undirected edges. Now he wants ...
- 【POJ 2942】Knights of the Round Table(点双连通分量,二分图染色)
圆桌会议必须满足:奇数个人参与,相邻的不能是敌人(敌人关系是无向边). 求无论如何都不能参加会议的骑士个数.只需求哪些骑士是可以参加的. 我们求原图的补图:只要不是敌人的两个人就连边. 在补图的一个奇 ...
- POJ 2942Knights of the Round Table(tarjan求点双+二分图染色)
Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 13954 Accepted: 4673 Description Bein ...
- poj2942 点-双联通+二分图染色
题意:有一群骑士要坐在一个圆形的桌子上,他们之间有些人相互讨厌,所以不能挨着,要求算出一次也不能坐在桌子上的人,每次会议桌子必须奇数个人,一个人不能开会 题解:可以先建一个补图,要满足题目条件我们只要 ...
- [多校联考2019(Round 5 T2)]蓝精灵的请求(二分图染色+背包)
[多校联考2019(Round 5)]蓝精灵的请求(二分图染色+背包) 题面 在山的那边海的那边住着 n 个蓝精灵,这 n 个蓝精灵之间有 m 对好友关系,现在蓝精灵们想要玩一个团队竞技游戏,需要分为 ...
- LUOGU P5061 秘密任务(背包+二分图染色)
传送门 解题思路 \(orz\)出题人的神仙做法.本蒟蒻看不懂,就水个求补图再二分图染色的方法来\(%1%\)出题人. 首先我们对图中\(m\)个关系连边,发现这样是没法做的,因为我们最后要关注的是谁 ...
随机推荐
- webapi filter过滤器中获得请求的方法详情(方法名,Attributes)
public class GlobalActionFilter : ActionFilterAttribute { private string _requestId; public override ...
- Redis 学习笔记(C#)
Redis安装及简单操作 Windows下安装步骤: 1. 第一步当然是先下载咯~ 地址:https://github.com/dmajkic/redis/downloads (根据自己实际情况选择 ...
- 背水一战 Windows 10 (27) - 控件(文本类): TextBlock
[源码下载] 背水一战 Windows 10 (27) - 控件(文本类): TextBlock 作者:webabcd 介绍背水一战 Windows 10 之 控件(文本类) TextBlock 示例 ...
- poj2186--tarjan+缩点
题目大意: 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎. 这 种关系是具有传递性的,如果A认为B受欢迎,B认为C受欢迎,那么牛A也 ...
- spi 10方式编写
//第一个CS变低的时候要sclk为高电平,第一个跳变沿进行赋值 module spi(input clk,input rst_n,output reg sclk,output reg cs,outp ...
- rest api参数与content-type
最近为项目组提供rest api 时遇到了关于接口参数的传递问题,主要是没有充分考虑到第三方调用者的使用方式,应该尽量的去兼容公司之前提供出去的接口调用方式,这样可以降低第三方调用者的学习成本,尽管之 ...
- java多线程-锁
自 Java 5 开始,java.util.concurrent.locks 包中包含了一些锁的实现,因此你不用去实现自己的锁了.但是你仍然需要去了解怎样使用这些锁. 一个简单的锁 让我们从 java ...
- IE9 IE8 ajax跨域问题的解决
项目中用到的跨域 ,在除IE9以下的浏览器上运行都是没有问题的,IE8 IE9中报错,error :no transport; 网上解决办法均是 在发起请求之前添加 jQuery.support.co ...
- Android高效计算——RenderScript(二)
3 RenderScript运行时层与反射层 3.1 RenderScript运行时层 RenderScript运行时层是指.rs代码运行时所在的层级.当对安卓项目进行编译的时候,.rs或者.rsh中 ...
- SQL Server 复制快照执行错误 错误代码 14068
问题描述: 使用基于快照初始化的事务复制,在上次发布的时候,添加项,执行快照agent,报错,错误信息: Error messages:Message: The subscription status ...