CodeFroces New Assignment 二分图匹配
There is a class consisting of n students, in which each one has a number representing his/her personality. The teacher gives the students a new assignment and asks them to solve it in groups so that each group can contain two students at most.
Students cannot create groups as they please because the teacher gives the following rules that must be met in order for a group to be valid:
- The group can be由of one male student, one female student, or male and female students.
- If the number of students in the group is two, these students must share common interests. Two students i and j share interests if and only if their numbers ai and aj share common divisor d > 1.
Since this is a really diverse class, no triple of students share a common interest, therefore all triples ai, aj, ak are co-primes (i.e. gcd(ai, aj, ak) ≡ 1).
Your task is to distribute the students into groups such that each student must join exactly one group, and the number of groups is as minimal as possible. Can you?
Input
The first line contains an integer T (1 ≤ T ≤ 100), in which T is the number of test cases.
The first line of each test case contains an integer n (1 ≤ n ≤ 104), in which n is the number of students in the class.
Then a line follows containing n integers a1, a2, ..., an (1 ≤ ai ≤ 106), in which ai if the personality of the ith student. Then a line follows containing n space-separated characters p1, p2, ..., pn (), in which pi is "M" if the ith student is male, and "F" if she is female.
n 总测试用例的总和不超过 3 × 105.
Output
For each test case, print a single line containing the minimum number of groups that can be formed in the class.
Example
2
2
3 6
M F
5
5 6 7 10 21
F F F M M
1
3
Note
In the second test case, the minimum number of groups is 3, in which the first group consists of the 1st and 4th students, the second group consists of the 2nd student, and the third group consists of the 3rd and 5th students.
思路:第一反应是二分图匹配找到最大匹配数,再看数据n有1e4,而且T有1e2,用匈牙利可能会超时,所以用网络流写法;再思考如何建图连边,暴力n²会超时,只能想如何优化建边了。
想到题目要求两人gcd > 1,所以把每个人对应的数字拆分质因数,我这里先将女生的数的质因数通过df函数(作用就是得出该数的所有质因数且不记录重复的质因数)放入vector数组k[素数(质因数)][i]=女生对应的编号,在通过同样意义的df'数组将男生的数分解质因数,此时不用保存在数组里了,可以直接吧有该质因数的vector数组里的女生连边。
这样就能建图然后利用网络流跑一遍最大匹配,用总数减去最大匹配数得到结果了。
注:vector数组k[素数][i]里的素数并不是直接对应的素数,因为a[i]最大有1e6,所以直接存会很浪费空间,于是首先用素数筛将素数全部找出并标号,k[i][j]的第一维表示的是第i个素数。
贴一发优化llw大佬后的代码
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<stack> using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P; #define bug printf("*********\n");
#define debug(x) cout<<"["<<x<<"]" <<endl;
#define mid (l+r)/2
#define chl 2*k+1
#define chr 2*k+2
#define lson l,mid,chl
#define rson mid,r,chr
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a)); const long long mod=;
const int maxn=1e6+;
const int INF=0x7fffffff;
const int inf=0x3f3f3f3f;
const double eps=1e-;
const double pi=acos(-); struct edge {
int to,cap,rev;
};
vector <edge> G[maxn];
int level[maxn];
int iter[maxn]; void init(int _n) {
for(int i=; i<=_n; i++) {
G[i].clear();
}
} void bfs(int s) {
memset(level,-,sizeof(level));
queue<int> que;
level[s]=;
que.push(s);
while(!que.empty()) {
int v= que.front();
que.pop();
for(int i=; i<G[v].size(); i++) {
edge & e=G[v][i];
if(e.cap>&&level[e.to]<) {
level[e.to]=level[v] + ;
que.push(e.to);
}
}
}
} void add(int from,int to,int cap) {
edge eg;
eg.to=to;
eg.cap=cap;
eg.rev=G[to].size();
G[from].push_back(eg);
eg.to=from;
eg.cap=;
eg.rev=G[from].size()-;
G[to].push_back(eg);
} int dfs(int v,int t,int f) {
if(v == t)return f;
for(int &i = iter[v]; i < G[v].size(); i++) {
edge &e=G[v][i];
if(e.cap> && level[v]<level[e.to]) {
int d=dfs(e.to,t,min(f,e.cap));
if(d>) {
e.cap-=d;
G[e.to][e.rev].cap+=d;
return d;
}
}
}
return ;
}
int maxflow(int s,int t) {
int flow=;
for(;;) {
bfs(s);
if(level[t]<)return flow;
memset(iter,,sizeof(iter));
int f;
while((f = dfs(s,t,INF))>) {
flow +=f;
}
}
} int a[maxn],b[maxn];
int prime[maxn];//存这是第几个素数
vector<int> v;//存素数
void sieve(int _n) {
int tot=;
memset(prime,,sizeof(prime));
prime[]=-;
prime[]=-;
for(int i=; i<=_n; i++) {
if (prime[i]==) {
prime[i]=tot++;
v.push_back(i);
for(int j=i*; j<=_n; j+=i) {
prime[j]=-;
}
}
}
} vector<int> k[maxn/];//大概估计一下区间内素数个数不足总数1/10 void df(int x,int pos) {//x为本身的数,pos为标号
if(x==)return;//1与任何数gcd都是1所以直接不考虑
for(int i=; i<v.size(); i++) {
if(x<v[i]) {//任何数的质因数肯定小于等于本身,大于就return了
return ;
}
if(prime[x]!=-) {//为素数
k[prime[x]].push_back(pos);
return ;
}
if(x%v[i]==)k[i].push_back(pos);//如果是合数就判断当前枚举素数是不是该合数的质因数
while(x%v[i]==) {//去掉重复质因数
x/=v[i];
}
}
}
void df2(int x,int pos) {
if(x==)return;
for(int i=; i<v.size(); i++) {
if(x<v[i]) {
return ;
}
if(prime[x]!=-) {
i=prime[x];
for(int j=; j<k[i].size(); j++) {//与有相同质因数的女生全部连边
add(k[i][j],pos,);
}
return ;
}
if(x%v[i]==) {
for(int j=; j<k[i].size(); j++) {//同上
add(k[i][j],pos,);
}
}
while(x%v[i]==) {
x/=v[i];
}
}
} int main() {
sieve(1e6+);//预处理素数筛
int t,n;
scanf("%d",&t);
while(t--) {
scanf("%d",&n);
for(int i=; i<v.size(); i++) {
k[i].clear();
}
for(int i=; i<=n; i++) {
scanf("%d",&a[i]);
}
for(int i=; i<=n; i++) {
char s[];
scanf("%s",s);
if(s[]=='F') {
b[i]=;//女1男0
} else b[i]=;
}
init(n+);
for(int i=; i<=n; i++) {
if(b[i]==) {
df(a[i],i);
}
}
for(int i=; i<=n; i++) {
if(b[i]==) {
df2(a[i],i);
}
}
for(int i=; i<=n; i++) { if(b[i]==) {
add(,i,);
} else add(i,n+,);
}
printf("%d\n",n-maxflow(,n+));
}
return ;
}
CodeFroces New Assignment 二分图匹配的更多相关文章
- UVA 12549 - 二分图匹配
题意:给定一个Y行X列的网格,网格种有重要位置和障碍物.要求用最少的机器人看守所有重要的位置,每个机器人放在一个格子里,面朝上下左右四个方向之一发出激光直到射到障碍物为止,沿途都是看守范围.机器人不会 ...
- POJ 1274 裸二分图匹配
题意:每头奶牛都只愿意在她们喜欢的那些牛栏中产奶,告诉每头奶牛愿意产奶的牛棚编号,求出最多能分配到的牛栏的数量. 分析:直接二分图匹配: #include<stdio.h> #includ ...
- BZOJ1433 ZJOI2009 假期的宿舍 二分图匹配
1433: [ZJOI2009]假期的宿舍 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2375 Solved: 1005[Submit][Sta ...
- HDU1281-棋盘游戏-二分图匹配
先跑一个二分图匹配,然后一一删去匹配上的边,看能不能达到最大匹配数,不能这条边就是重要边 /*----------------------------------------------------- ...
- HDU 1083 网络流之二分图匹配
http://acm.hdu.edu.cn/showproblem.php?pid=1083 二分图匹配用得很多 这道题只需要简化的二分匹配 #include<iostream> #inc ...
- hdu 5727 Necklace dfs+二分图匹配
Necklace/center> 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5727 Description SJX has 2*N mag ...
- BZOJ 1059 & 二分图匹配
题意: 判断一个黑白染色的棋盘能否通过交换行或列使对角线上都是黑色. SOL: 真是有点醉...这种问题要么很神要么很水...第一眼感觉很水但就是不造怎么做...想了10分钟怎么感觉就是判断个数够不够 ...
- 【POJ 3020】Antenna Placement(二分图匹配)
相当于用1*2的板覆盖给定的h*w的格子里的点,求最少的板.可以把格子相邻的分成两个集合,如下图,0为一个集合,1的为一个,也就是(行数+列数)为奇数的是一个集合,为偶数的为另一个集合.1010101 ...
- BZOJ-1143&&BZOJ-2718 祭祀river&&毕业旅行 最长反链(Floyed传递闭包+二分图匹配)
蛋蛋安利的双倍经验题 1143: [CTSC2008]祭祀river Time Limit: 10 Sec Memory Limit: 162 MB Submit: 1901 Solved: 951 ...
随机推荐
- 14 微服务电商【黑马乐优商城】:day01-springboot(理论篇)
本项目的笔记和资料的Download,请点击这一句话自行获取. day01-springboot(理论篇) :day01-springboot(实践篇) :day01-springboot(Thyme ...
- python语法基础-并发编程-进程-进程池以及回调函数
############### 进程池 ############## """ 进程池的概念 为什么会有进程池? 1,因为每次开启一个进程,都需要创建一个内存空间 ...
- python导入自定义的库
一.导入项目文件夹下的模块 1.导入整个模块 import 模块名 2.导入模块的某个函数 from 模块名 import 函数名 示例 untitled是项目文件夹,文件结构如下 ①在a.py导入c ...
- MAC上的爬虫软件怎么选?看完这篇就够了
在上一篇文章:网络爬虫软件哪个好用? 中,我们介绍了目前市面上比较成熟好用的网络爬虫软件, 但是其中有些不能在MAC上使用,因此今天这篇文章我们单独介绍一下在MAC操作系统中有哪些好用的爬虫软件,给大 ...
- JS 特效三大系列总结
一. offset系列 1. offset系列的5个属性 1. offsetLeft : 用于获取元素到最近的定位父盒子的左侧距离 * 计算方式: 当前元素的左边框的左侧到定位父盒子的左边框右侧 * ...
- Python笔记_第一篇_面向过程_第一部分_5.Python数据类型之列表类型(list)
Python中序列是最基本的数据结构.序列中的每个元素都分配一个数字(他的位置或者索引),第一个索引是0,第二个索引是1,依次类推.Python的列表数据类型类似于C语言中的数组,但是不同之处在于列表 ...
- 借助Nginx搭建反向代理服务器
一.反向代理:Web服务器的"经纪人" 1.1 反向代理初印象 反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网 ...
- 吴裕雄--天生自然TensorFlow高层封装:Keras-多输入输出
# 1. 数据预处理. import keras from keras.models import Model from keras.datasets import mnist from keras. ...
- day50-线程-定时器
#1.定时器: from threading import Timer def func(): print('定时器') t = Timer(1,func) #定时一秒,开启func线程. t.sta ...
- c++ string 的注意事项
string 的reserve 和 resize 区别 reserve 与capacity相关,如果reserve一个比当前capacity大的 reserve 只会找到一块合适大小的内存,并将原始内 ...