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 ...
随机推荐
- USACO training course Number Triangles 数塔 /// DP oj10122
题目大意: ...就是数塔 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 7+3+8+7+5=30 ...
- 2018-8-10-win10-uwp-使用资源在后台创建控件
title author date CreateTime categories win10 uwp 使用资源在后台创建控件 lindexi 2018-08-10 19:17:19 +0800 2018 ...
- 04.Mybatis输出映射之ResultMap
当实体类中的字段名与数据库中的字段名不一致时需要手动设置映射关系 在Mapper.xml中定义 <!-- resultMap最终还是要将结果映射到pojo上,type就是指定映射到哪一个pojo ...
- UVA - 374
https://vjudge.net/problem/19685/origin 费马小定理优化快速幂 因为加了费马小定理优化,小心2 2 2这种情况,会出现0 0 2,也就是0的0次方,实际答案为0 ...
- LoadRunner参数化详解【转】
距离上次使用loadrunner 已经有一年多的时间了.初做测试时在项目中用过,后面项目中用不到,自己把重点放在了工具之外的东西上,认为性能测试不仅仅是会用工具,最近又想有一把好的利器毕竟可以帮助自己 ...
- 廖雪峰Java15JDBC编程-1关系数据库基础-1关系数据库简介
1.数据库 1.1 定义 数据库是按照数据结构来组合.存储和管理数据的软件. 1.2 数据库模型 数据库有层次模型.网状模型.关系模型三种模型. 2 关系数据库 关系数据库是建立在关系模型上的数据库, ...
- sqlite3-入门日记4-实现C++类封装
一.前言: 今天试了下如何用C++类实现接口封装,感觉蛮好 .用于封装的类主要有两个,SQLiteStatement类和SQLiteWrapper类,是一个老外写的.我看了下源码,主要是对C接口进 ...
- Spring Boot与监控管理
概念: 通过引入spring-boot-starter-actuator,可以使用Spring Boot为我们提供的准生产环境下的应用监控和管理功能.我们可以通过HTTP,JMX,SSH协议来进行操作 ...
- 数据库备份还原——mysqlbackup与mysqldump对比测试
1 环境描述 1.1 硬件环境 服务器类型:华为RH5885 IP: 10.148.128.100 内存: 64G 物理CPU个数:4 CPU核数:8 逻辑CPU个数:64 Int ...
- StringBuffer清空
转载自:http://blog.sina.com.cn/s/blog_56fd58ab0100qfcz.html 在开发程序的时候,经常使用StringBuffer来进行字符串的拼接.如果在循环中来反 ...