maximum clique 1
maximum clique 1
空间限制:C/C++ 262144K,其他语言524288K
Special Judge, 64bit IO Format: %lld
题目描述
Please find a subset of S which has the maximum size while satisfying the following constraint:
The binary representations of any two numbers in this subset must have at least two different bits.
If there are multiple valid subsets, please output any one of them.
输入描述:
The input contains only two lines. The first line contains an integer N denoting the size of S.
The second line contains N positive integers a1,a2,…,aN denoting the members of set S. * 1≤N≤5000 * 1≤ai≤1e9 * all ai are distinct
输出描述:
You should output two lines. The first line contains one integer m denoting the size of the subset you found. The second line contains m positive integers denoting the member of this subset.
输入
5
3 1 4 2 5
输出
3
4 1 2
说明
题意:在一个集合中求一个最大的子集,使得子集里任意两个数之间至少有两个二进制位不同。
#include<bits/stdc++.h>
#define N 5050
using namespace std; typedef struct
{
int to,next;
long long flow;
bool is_match;
}ss; ss edg[*N];
int now_edge=,s,t;
int head[N]; void addedge(int u,int v)
{
edg[now_edge]=(ss){v,head[u],};
head[u]=now_edge++;
} void addedge(int u,int v,long long flow)
{
// printf("%d %d\n",u,v); edg[now_edge]=(ss){v,head[u],flow};
head[u]=now_edge++; edg[now_edge]=(ss){u,head[v],};
head[v]=now_edge++;
} int dis[N]; bool bfs()
{
memset(dis,,sizeof(dis));
queue<int>q;
q.push(s);
dis[s]=; while(!q.empty())
{
int now=q.front();
q.pop();
for(int i=head[now];i!=-;i=edg[i].next)
{
ss &e=edg[i];
if(e.flow>&&dis[e.to]==)
{
dis[e.to]=dis[now]+;
q.push(e.to);
}
}
} if(dis[t]==)return ;
return ;
} int current[N];
long long dfs(int x,long long maxflow)
{
if(x==t)return maxflow;
for(int i=current[x];i!=-;i=edg[i].next)
{
current[x]=i;
ss &e=edg[i];
if(e.flow>&&dis[e.to]==dis[x]+)
{
long long flow=dfs(e.to,min(maxflow,e.flow));
if(flow!=)
{
e.flow-=flow;
edg[i^].flow+=flow;
return flow;
}
}
}
return ;
} long long dinic()//最大流
{
long long ans=,flow;
while(bfs())
{
for(int i=;i<N;i++)current[i]=head[i];
while(flow=dfs(s,LLONG_MAX/))ans+=flow;
}
return ans;
} void init()
{
for(int i=;i<N;i++)head[i]=-;
now_edge=;
} int arr[N];
int color[N]={}; void Bipartite_graph_coloring(int x,int now_color)//二分图染色
{
if(color[x])return;
color[x]=now_color;
for(int i=head[x];i!=-;i=edg[i].next)Bipartite_graph_coloring(edg[i].to,now_color*-);
} bool is_match_vertex[N]={};
int is_ans[N]={};
void dfs(int x,int type)//寻找最小点覆盖集中的点,不在该集合中的点就是最大点独立集的点
{
if(is_ans[x])return;
is_ans[x]=;
for(int i=head[x];i!=-;i=edg[i].next)
{
int v=edg[i].to;
if(v==s||v==t)continue; if(edg[i].is_match==(bool)type)dfs(v,-type);
}
} int main()
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)scanf("%d",&arr[i]); init();
for(int i=;i<=n;i++)
{
for(int j=i+;j<=n;j++)
{
int now=arr[i]^arr[j];
if(now==(now&-now))
{
addedge(i,j);
addedge(j,i);
// printf("%d %d\n",arr[i],arr[j]);
}
}
} for(int i=;i<=n;i++)
if(!color[i])Bipartite_graph_coloring(i,);//先将图划分成二分图 /* for(int i=1;i<=n;i++)if(color[i]==1)printf("%d ",arr[i]);
printf("\n");
for(int i=1;i<=n;i++)if(color[i]==-1)printf("%d ",arr[i]);
printf("\n");*/ init(); s=n+,t=s+;
for(int i=;i<=n;i++)
{
if(color[i]==)addedge(s,i,);
else
addedge(i,t,); if(color[i]==)
{
for(int j=;j<=n;j++)
{
int now=arr[i]^arr[j];
if(now==(now&-now))
{
addedge(i,j,);//建边准备跑二分图匹配
}
}
}
}
int ans=n-dinic();
printf("%d\n",ans); for(int i=;i<=n;i++)
{
if(color[i]==)
{
for(int j=head[i];j!=-;j=edg[j].next)
{
if(edg[j].to!=s&&edg[j^].flow)
{
edg[j].is_match=edg[j^].is_match=true;
is_match_vertex[i]=is_match_vertex[edg[j].to]=true;
}
else
{
edg[j].is_match=edg[j^].is_match=false;
}
}
}
} for(int i=;i<=n;i++)
{
if(color[i]==-&&is_match_vertex[i]==false)
{
dfs(i,);
}
} for(int i=;i<=n;i++)
{
if(color[i]==&&!is_ans[i]||color[i]==-&&is_ans[i])printf("%d%c",arr[i],--ans== ? '\n': ' ');
}
return ;
}
maximum clique 1的更多相关文章
- 【最大团】【HDU1530】【Maximum Clique】
先上最大团定义: 最大团问题(Maximum Clique Problem, MCP)是图论中一个经典的组合优化问题,也是一类NP完全问题,在国际上已有广泛的研究,而国内对MCP问题的研究则还处于起步 ...
- Maximum Clique
Maximum Clique Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- 回溯法——最大团问题(Maximum Clique Problem, MCP)
概述: 最大团问题(Maximum Clique Problem, MCP)是图论中一个经典的组合优化问题,也是一类NP完全问题.最大团问题又称为最大独立集问题(Maximum Independent ...
- hdu 1530 Maximum Clique (最大包)
Maximum CliqueTime Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- 2019牛客多校第五场 F maximum clique 1 状压dp+最大独立集
maximum clique 1 题意 给出一个集合s,求每个子集的最大独立集的权值和(权值是独立集的点个数) 分析 n比较小,一股浓浓的暴力枚举每一个子集的感觉,但是暴力枚举模拟肯定会T,那么想一想 ...
- hdu 1530 Maximum Clique
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1530 题目分类:最大团问题 DP + DFS 代码: #include<bits/stdc++. ...
- ZOJ 1492 Maximum Clique 搜索最大团
ZOJ1492 题意:给一个无向图 求最大团的大小.节点数小于50 数据有限,考虑记忆化搜索,方程很好给出. 令 Si={vi,vi+1.....vn} mc[i]表示Si最大团的大小,倒着推算. 必 ...
- HDU1530 Maximum Clique dp
正解:dp 解题报告: 这儿是传送门 又是个神仙题趴QAQ 这题就直接说解法辣?主要是思想比较难,真要说有什么不懂的知识点嘛也没有,所以也就没什么好另外先提一下的知识点QAQ 首先取反,就变成了求最大 ...
- 【HDU1530】【ZOJ1492】Maximum Clique
Position: http://poj.org/problem?id=3241 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCod ...
随机推荐
- flexbox属性速览及常见布局实现
CSS3 弹性盒子(Flex Box)弹性盒子是即 CSS2 浮动布局后, CSS3 的一种新的布局模式. CSS3 弹性盒( Flexible Box 或 flexbox),是一种当页面需要适应不同 ...
- Python3中面向对象 OOP
Python3中面向对象 OOP 定义: python中通过关键字 class 实现类的定义: class ClassName(object): pass 获取成员变量:ClassName.变量名 修 ...
- Python中else的用法
Python中else除了可以与if组成条件语句外,还可以和while .for .try一起串联使用. else和while配合使用: count=0 while count>12: if ( ...
- umount:将文件设备卸载
[root@centos57 ~]# umount /dev/hda1 用设备文件名来卸载 [root@centos57 ~]# umount /aixi 用挂 ...
- JS数组 团里添加新成员(向数组增加一个新元素)只需使用下一个未用的索引,任何时刻可以不断向数组增加新元素。myarray[5]=88;
团里添加新成员(向数组增加一个新元素) 上一节中,我们使用myarray变量存储了5个人的成绩,现在多出一个人的成绩,如何存储呢? 只需使用下一个未用的索引,任何时刻可以不断向数组增加新元素. my ...
- node vue 微信公众号(四)配置环境 本地测试
1.去natap 配置端口号 //本地项目是8080端口,natapp就配置8080端口 2.ngrok配合vue-cli实现外网访问 1.去 https://ngrok.com/download 下 ...
- barrel shifter, logarthmic shifter and funnel shifter
1,shifter小集合 (1) simple shift 左移或右移补0 (2) arthmetic shift 左移补0,右移补符号位 (3) barrel shifter 桶型,顾名思义,应该头 ...
- 【JZOJ3379】查询
description 对于一个整数序列,查询区间第k大数可以在O(logN)的时间内轻松完成.现在我们对这个问题进行推广. 考虑带重复数的集合(multiset).定义在该类集合上的并操作" ...
- 洛谷P1640 【SCOI2010】连续攻击游戏
原题传送门 题目描述 lxhgww最近迷上了一款游戏,在游戏里,他拥有很多的装备,每种装备都有2个属性,这些属性的值用[1,10000]之间的数表示.当他使用某种装备时,他只能使用该装备的某一个属性. ...
- 菜鸟nginx源码剖析数据结构篇(一)动态数组ngx_array_t[转]
菜鸟nginx源码剖析数据结构篇(一)动态数组ngx_array_t Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog.csdn ...