maximum clique 1

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
Special Judge, 64bit IO Format: %lld

题目描述

You are given a set S containing n distinct positive integers a1,a2,…,an.

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

说明

3 (112) and 1 (012) has only 1 different bit so they can not be in the set together. 4 (1002) and 1 (0012) has 2 different bits so they can be in the set together.
Following unordered pairs are allowed to be in the set together: <1, 2>, <1, 4>, <2, 4>, <2, 5>, <3, 4>, <3, 5>. Thus the maximum set is of size 3 ({1, 2, 4}).

题意:在一个集合中求一个最大的子集,使得子集里任意两个数之间至少有两个二进制位不同。
思路:两个相异的数至少两个bit不一样的否命题是:恰一个bit不一样。然后考虑到A和B一个bit不一样,A和C一个bit不一样,A,B,C各不相同,那么B和C一定不会只有一个bit不一样,所以若是两个数只有一个bit不一样,我们就在它们之间连一条边,可以发现这个图是个二分图。答案就是这个二分图的最大点独立集。
#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的更多相关文章

  1. 【最大团】【HDU1530】【Maximum Clique】

    先上最大团定义: 最大团问题(Maximum Clique Problem, MCP)是图论中一个经典的组合优化问题,也是一类NP完全问题,在国际上已有广泛的研究,而国内对MCP问题的研究则还处于起步 ...

  2. Maximum Clique

    Maximum Clique Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...

  3. 回溯法——最大团问题(Maximum Clique Problem, MCP)

    概述: 最大团问题(Maximum Clique Problem, MCP)是图论中一个经典的组合优化问题,也是一类NP完全问题.最大团问题又称为最大独立集问题(Maximum Independent ...

  4. hdu 1530 Maximum Clique (最大包)

    Maximum CliqueTime Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  5. 2019牛客多校第五场 F maximum clique 1 状压dp+最大独立集

    maximum clique 1 题意 给出一个集合s,求每个子集的最大独立集的权值和(权值是独立集的点个数) 分析 n比较小,一股浓浓的暴力枚举每一个子集的感觉,但是暴力枚举模拟肯定会T,那么想一想 ...

  6. hdu 1530 Maximum Clique

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1530 题目分类:最大团问题 DP + DFS 代码: #include<bits/stdc++. ...

  7. ZOJ 1492 Maximum Clique 搜索最大团

    ZOJ1492 题意:给一个无向图 求最大团的大小.节点数小于50 数据有限,考虑记忆化搜索,方程很好给出. 令 Si={vi,vi+1.....vn} mc[i]表示Si最大团的大小,倒着推算. 必 ...

  8. HDU1530 Maximum Clique dp

    正解:dp 解题报告: 这儿是传送门 又是个神仙题趴QAQ 这题就直接说解法辣?主要是思想比较难,真要说有什么不懂的知识点嘛也没有,所以也就没什么好另外先提一下的知识点QAQ 首先取反,就变成了求最大 ...

  9. 【HDU1530】【ZOJ1492】Maximum Clique

    Position: http://poj.org/problem?id=3241 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCod ...

随机推荐

  1. java-day07

    API 应用程序编程接口 Scanner类 将键盘输入的数据到程序中 1.导包 2.创建 Scanner 对象名 = new Scanner(); 3.使用 int num = 对象名.nextInt ...

  2. 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 ...

  3. angular 页签

    HTML: <link rel="stylesheet" href="views/show/tab.css"/> <div> <u ...

  4. UMP系统架构 Controller服务器

  5. js正则笔记

    //内容 var innerhtml = $('.reading_box_m').html().replace(/ tag="?[一二三四五六七八九十]+"?/ig, " ...

  6. 学习 debug

    要在代码编辑器中设置源代码断点,有以下 4 种操作方式. (1) 把光标移到要设为断点的行上,按下 F5 键. (2) 用鼠标左键单击要设为断点的行的最左端. (3) 用鼠标右键单击要设为断点的行,在 ...

  7. Android开发 使用SparseArray代替HashMap[转载]

    源作者:Android小Y链接:https://www.jianshu.com/p/1828f14d7955来源:简书 前言 Android开发中,一个好的应用,除了要有吸引人的功能和交互之外,在性能 ...

  8. JUC 一 CopyOnWriteArrayList 和 CopyOnWriteArraySet

    java.util.concurrent; 简介 CopyOnWriteArrayList是一个线程安全的ArrayList,通过内部的volatile数组和显式锁ReentrantLock来实现线程 ...

  9. 什么是 MIME TYPE

    首先,我们要了解浏览器是如何处理内容的.在浏览器中显示的内容有 HTML.有 XML.有 GIF.还有 Flash ……那么,浏览器是如何区分它们,决定什么内容用什么形式来显示呢?答案是 MIME T ...

  10. apache netbeans不再集成tomcat

    老版本 8.2之前的,集成tomcat,安装使用比较简单https://netbeans.org/features/index.html 新版本的阿帕奇netbeans,则不集成tomcat,使用时, ...