LightOJ - 1356 Prime Independence (二分图 最大独立集 素数打表)
题意:
给你一个集合,让你从这个集合中挑选出几个数,使得这几个数中任意两个数相除后的值不能为素数
即挑选出来的这几个数不能互相冲突
最大独立集 = 所有点数 - 最大匹配数
呵。。呵。。。原先用的二维数组来标记 呵。。呵。。。。呵。。呵。。ER。。。MLE
vector 大法好! orz
mmp。。。。。呸。。。吐槽完毕。。。。
因为a = b * k (k为质数) 所以a必然比b多一个质因子 所以把偶数个质因子的数放到左边 把奇数个质因子的数放到右边
代码如下:
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define maxn 500010
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int LL_INF = 0x7fffffffffffffff,INF = 0x3f3f3f3f;
int dx[maxn], dy[maxn], cx[maxn], cy[maxn], used[maxn];
int dot[maxn], vis[maxn], primes[maxn];
int nx, ny, dis, n, m;
int B[maxn], goal[maxn];
vector<int> G[];
void init()
{
int cnt = ;
mem(vis,);
for(int i=; i<maxn; i++)
if(!vis[i]){
primes[cnt++] = i;
for(LL j=(LL)i*i; j<maxn; j+=i)
vis[j] = ;
}
} int bfs()
{
queue<int> Q;
dis = INF;
mem(dx,-);
mem(dy,-);
for(int i=; i<=nx; i++)
{
if(cx[i] == -)
{
Q.push(i);
dx[i] = ;
}
}
while(!Q.empty())
{
int u = Q.front(); Q.pop();
if(dx[u] > dis) break;
for(int v=; v<G[u].size(); v++)
{
int i = G[u][v];
if(dy[i] == -)
{
dy[i] = dx[u] + ;
if(cy[i] == -) dis = dy[i];
else
{
dx[cy[i]] = dy[i] + ;
Q.push(cy[i]);
}
} }
}
return dis != INF;
} int dfs(int u)
{
for(int v=; v<G[u].size(); v++)
{
int i = G[u][v];
if(!used[i] && dy[i] == dx[u] + )
{
used[i] = ;
if(cy[i] != - && dis == dy[i]) continue;
if(cy[i] == - || dfs(cy[i]))
{
cy[i] = u;
cx[u] = i;
return ;
}
}
}
return ;
} int hc()
{
int res = ;
mem(cx,-);
mem(cy,-);
while(bfs())
{
// cout<<22222222222<<endl;
mem(used,);
for(int i=; i<=nx; i++)
{
if(cx[i] == - && dfs(i))
res++;
}
}
return res;
} int main()
{ init();
int T;
int ans = ;
scanf("%d",&T);
while(T--)
{
for(int i=; i<; i++) G[i].clear();
mem(goal,-);
scanf("%d",&n);
for(int i=; i<=n; i++){
scanf("%d",&dot[i]);
}
sort(dot+,dot+n+);
for(int i=; i<=n; i++)
goal[dot[i]] = i;
for(int i=; i<=n; i++)
{
int cnt = , cnt2 = ;
int t = dot[i];
for(int j=; primes[j] * primes[j] <= t; j++)
if(t % primes[j] == )
{
B[cnt++] = primes[j];
while(t % primes[j] == )
{
t /= primes[j];
cnt2++;
}
}
if(t > ) B[cnt++] = t, cnt2++;
for(int j=; j<cnt; j++)
{
t = goal[dot[i]/B[j]];
if(t <= i && t != -)
{
if(cnt2 & ) G[i].push_back(t);
else G[t].push_back(i);
}
}
}
nx = n, ny = n;
printf("Case %d: %d\n",++ans,n - hc()); } return ;
}
LightOJ - 1356 Prime Independence (二分图 最大独立集 素数打表)的更多相关文章
- LightOJ 1356 Prime Independence 二分图最大独立集,HK算法
这个题唯一需要说的就是普通的匈牙利算法是O(nm)的,过不了 然后HK算法可以O(n^0.5m),这个算法可以每次找很多同样长度的最短增广路 分析见:http://www.hardbird.net/l ...
- LightOJ 1356 Prime Independence(质因数分解+最大独立集+Hopcroft-Carp)
http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1356 题意: 给出n个数,问最多能选几个数,使得该集合中的 ...
- LightOJ - 1356 Prime Independence (数论+二分图匹配)
题意:有N个数的集合,其中选出若干个数组成一个子集,要求这个子集中的任意两个数a,b都不能通过a=k*b得到,其中k是一个素数.求这个子集最大的size. 分析:集合中任意两数的关系是二者之间是否之差 ...
- POJ_3126 Prime Path 【BFS+素数打表】
一.题目 http://poj.org/problem?id=3126 二.分析 该题主要是要让我们找到一个$4$位素数到另一个$4$位素数的最少的变换次数,且要求保证每一次变换都满足 1.下一个数必 ...
- HDU 3829 - Cat VS Dog (二分图最大独立集)
题意:动物园有n只猫和m条狗,现在有p个小孩,他们有的喜欢猫,有的喜欢狗,其中喜欢猫的一定不喜欢狗,喜欢狗的一定不喜欢猫.现在管理员要从动物园中移除一些动物,如果一个小孩喜欢的动物留了下来而不喜欢的动 ...
- CodeForces 385C Bear and Prime Numbers 素数打表
第一眼看这道题目的时候觉得可能会很难也看不太懂,但是看了给出的Hint之后思路就十分清晰了 Consider the first sample. Overall, the first sample h ...
- HDU3829(KB10-J 二分图最大独立集)
Cat VS Dog Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/Others)Total ...
- BZOJ3175:[TJOI2013]攻击装置(二分图最大独立集)
Description 给定一个01矩阵,其中你可以在0的位置放置攻击装置.每一个攻击装置(x,y)都可以按照“日”字攻击其周围的 8个位置(x-1,y-2),(x-2,y-1),(x+1,y-2), ...
- Sum of Consecutive Prime Numbers(素数打表+尺取)
Description Some positive integers can be represented by a sum of one or more consecutive prime numb ...
随机推荐
- Oracle ORA-14102: 只能指定一个 LOGGING 或 NOLOGGING 子句
oracle 11g ,在通过命令impdp向一个数据库用户导入数据时,出现错误: ORA-14102: 只能指定一个 LOGGING 或 NOLOGGING 子句 造成此问题的原因是:当导入的表里没 ...
- 『转』统计一个日志文件里,单词出现频率的shell脚本
原文地址:http://blog.csdn.net/taiyang1987912/article/details/39995175 #查找文本中n个出现频率最高的单词 #!/bin/bash coun ...
- NOI.ac #8 小w、小j和小z LIS
传送门 题意:在一个数轴上,给出$N$个人的初始位置与速度(速度有方向),求最大的时间使得存在$N-K$个人在这一段时间内两两没有相遇.$1 \leq K \leq N \leq 10^5$ 显然有二 ...
- DeskMini无传统机械键盘与鼠标接口的情况下使用U盘安装系统经验总结
总结安装纯净版Win7旗舰版系统安装过程所解决的问题要点: 1:UEFI引导启动的实现. 2:使用Dism++实现系统的安装. 3:使用Dism++解决新主板在安装系统过程中不能使用USB键盘和鼠标的 ...
- WPF中的Bitmap与byte
原文:WPF中的Bitmap与byte public MainWindow() { InitializeComponent(); byte[] b = GetPictureData(@"F: ...
- Spring Aop: 关于继承和execution target this @annotation
1.多态 target指通过这个对象调用的方法 (匹配标识对象的所有方法) getMethod() this指调用这个对象的方法 (匹配标识对象实现的方法) getDeclaredMethod( ...
- Luogu P4317 花神的数论题
也是一道不错的数位DP,考虑先转成二进制后再做 转化一下问题,考虑统计出\([1,n]\)中在二进制下有\(i\)个\(1\)的方案数\(cnt_i\),那么答案显然就是\(\prod i^{cnt_ ...
- EZ 2018 07 06 NOIP模拟赛
又是慈溪那边给的题目,这次终于没有像上次那样尴尬了, T1拿到了较高的暴力分,T2没写炸,然后T3写了一个优雅的暴力就203pts,Rank3了. 听说其它学校的分数普遍100+,那我们学校还不是强到 ...
- ML.NET 示例:多类分类之问题分类
写在前面 准备近期将微软的machinelearning-samples翻译成中文,水平有限,如有错漏,请大家多多指正. 如果有朋友对此感兴趣,可以加入我:https://github.com/fei ...
- linux-IO重定向-文本流重定向
输出重定向的追加和覆盖 标准输出就这两种: 覆盖和追加 >> 是重定向操作符 1 是 命令的文件描述符 重定向操作符合文件描述符之间不能存在空白符 否则1会被当做是文件被读取 将正确和错误 ...