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. 程序员如何面对 HR 面试的 40 个问题!

    讲一个身边朋友亲身经历的故事吧. 一个技术非常牛的朋友去阿里面试,成功通过了几轮技术车轮战,最后躺在了 HR 面上...所以,尽管你技术再牛逼,你回答不好 HR 的问题,赢得不了 HR 的认可,你最终 ...

  2. JVM实战

    一.内存溢出 虚拟机栈和本地方法栈溢出:-Xss256k package com.jedis; import java.util.LinkedList; import java.util.List; ...

  3. 面试系列 31 zk都有哪些使用场景

    大致来说,zk的使用场景如下,我就举几个简单的,大家能说几个就好了: (1)分布式协调:这个其实是zk很经典的一个用法,简单来说,就好比,你A系统发送个请求到mq,然后B消息消费之后处理了.那A系统如 ...

  4. 记因为电脑名更改而导致sql server一直连接失败

    安装的是sql server2016.原先一直用的好好的,直到有一天觉得电脑名称,嗯要改下.改完后,嗯,就忘了. 然后打开sql server,连接失败.一开始以为是sql server配置管理器中的 ...

  5. AutoIt自动化编程(4)【转】

    五.自动化操作轻松入门系列5 控件操作 然而,在真正实现自动化时仅靠上面的技术往往难以达到预期目的.下面开始进入最为重要的控件操作. 1.设置文本 在安装软件的过程中用户往往需要提供一些必需信息,比如 ...

  6. 图论最短路径算法——Dijkstra

    说实在的,这算法很简单,很简单,很简单--因为它是贪心的,而且码量也小,常数比起SPFA也小. 主要思想 先初始化,dis[起点]=0,其它皆为无限大. 还要有一个bz数组,bz[i]表示i是否确定为 ...

  7. 今天介绍一个渐变的方法,在shell里面自动生成注释简介

    在编辑sh脚本时,我经常在shell中写一些注释.今天我介绍一种渐变方法,它可以在每次vim shell脚本时自动在shell中生成注释和其他信息. 让我们共享一个shell脚本模板文件,将其复制到用 ...

  8. Java 的 JJWT 实现 JWT

    JJWT是一个提供端到端的JWT创建和验证的Java库 依赖 <dependency> <groupId>io.jsonwebtoken</groupId> < ...

  9. 0908NOIP模拟测试赛后总结

    %%%skyh rank1- 奶风神.kx.有钱人 rank2-210 NC锅.RNB.B哥 rank5-200 我 rank32- 9-13upd:无意中点进了某个博客发现我竟然考场上yy出了树上莫 ...

  10. 跳表上线性dp——1150D 好题!

    题目设计的很好,感觉做了这题对dp的状态有了更深的理解 /* 先预处理序列自动机 dp[i][j][k]表示匹配到i,j,k时的最靠前的位置 那么现在A串加入了一个字母,我们要求的就是dp[i+1][ ...