LuoguP2765 魔术球问题

首先,很难看出来这是一道网络流题.但是因为在网络流24题中,所以还是用网络流的思路

首先考虑完全平方数的限制。

如果\(i,j\)满足\(i < j\) 且 $i + j \(为完全平方数我们就在\)i - j $连一条有向边

练完之后我们会得到这样一个图(图来自luogu题解)

发现这是一个DAG,而且我们将柱子的限制转化为路径条数。问题就转化成了

LuoguP2764 最小路径覆盖问题

然后,我们就一直加球,加到需要的路径条数大于给定的柱子数为止

#include<cstdio>
#include<cctype>
#include<cstring>
#include<queue>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
const int N = 2e5 + 3;
const int M = 2e6 + 3;
const int INF = 2e9;
int n,s,t,tot = 1,top;
vector <int> G[N];
int pre[N],head[N],cur[N],high[N],first[N];
bool vis[N];
struct edge{
int from;
int to;
int nxt;
int flow;
}e[M];
inline void add(int x,int y,int z){
e[++tot].to = y;
e[tot].flow = z;
e[tot].from = x;
e[tot].nxt = head[x];
head[x] = tot;
e[++tot].to = x;
e[tot].flow = 0;
e[tot].from = y;
e[tot].nxt = head[y];
head[y] = tot;
}
inline int read(){
int v = 0,c = 1;char ch = getchar();
while(!isdigit(ch)){
if(ch == '-') c = -1;
ch = getchar();
}
while(isdigit(ch)){
v = v * 10 + ch - 48;
ch = getchar();
}
return v * c;
}
inline bool bfs(){
queue <int> q;
for(int i = 0;i <= t;++i) high[i] = 0;
q.push(s);high[s] = 1;
while(!q.empty()){
int k = q.front();q.pop();
for(int i = head[k];i;i = e[i].nxt){
int y = e[i].to;
if(!high[y] && e[i].flow > 0)
high[y] = high[k] + 1,q.push(y);
}
}
return high[t] != 0;
}
inline int dfs(int x,int dis){
if(x == t) return dis;
for(int &i = cur[x];i;i = e[i].nxt){
int y = e[i].to;
if(high[y] == high[x] + 1 && e[i].flow > 0){
int flow = dfs(y,min(dis,e[i].flow));
if(flow > 0){
e[i].flow -= flow;
e[i ^ 1].flow += flow;
pre[x >> 1] = y >> 1;
return flow;
}
}
}
return 0;
}
inline int dinic(){
int res = 0;
while(bfs()){
for(int i = 0;i <= t;++i) cur[i] = head[i];
while(int now = dfs(s,INF)) res += now;
}
return res;
}
int main(){
n = read();
int sum = 0,now = 0;
s = 100000 + 2,t = s + 1;
while(now <= n){
sum++;
add(s,sum << 1,1);
add(sum << 1 | 1,t,1);
for(int i = sqrt(sum) + 1;i * i < sum * 2;++i)
add((i * i - sum) << 1,sum << 1 | 1,1);
int f = dinic();
if(!f) first[++now] = sum;
}
printf("%d\n",sum - 1);
for(int i = 1;i <= n;++i){
if(vis[first[i]]) continue;
for(int j = first[i];j != 0 && j != (t >> 1);j = pre[j])
vis[j] = 1,printf("%d ",j);
puts("");
}
return 0;
}

LuoguP2765 魔术球问题的更多相关文章

  1. [luoguP2765] 魔术球问题(最大流—最小不相交路径覆盖)

    传送门 枚举球的个数 num 如果 i < j && (i + j) 是完全平方数,那么 i -> j' 连一条边 再加一个超级源点 s,s -> i 再加一个超级汇 ...

  2. LuoguP2765 魔术球问题(最大流)

    题目描述 «问题描述: 假设有n根柱子,现要按下述规则在这n根柱子中依次放入编号为1,2,3,...的球. (1)每次只能在某根柱子的最上面放球. (2)在同一根柱子中,任何2个相邻球的编号之和为完全 ...

  3. cogs_396_魔术球问题_(最小路径覆盖+二分图匹配,网络流24题#4)

    描述 http://cojs.tk/cogs/problem/problem.php?pid=396 连续从1开始编号的球,按照顺寻一个个放在n个柱子上,\(i\)放在\(j\)上面的必要条件是\(i ...

  4. LOJ6003 - 「网络流 24 题」魔术球

    原题链接 Description 假设有根柱子,现要按下述规则在这根柱子中依次放入编号为的球. 每次只能在某根柱子的最上面放球. 在同一根柱子中,任何2个相邻球的编号之和为完全平方数. 试设计一个算法 ...

  5. P2765 魔术球问题

    P2765 魔术球问题 贪心模拟就可以过.........好像和dinic没啥关系   找找规律发现可以贪心放.n又灰常小. 设答案=m 你可以$O(mn)$直接模拟过去 闲的慌得话可以像我用个$se ...

  6. LibreOJ 6003. 「网络流 24 题」魔术球 贪心或者最小路径覆盖

    6003. 「网络流 24 题」魔术球 内存限制:256 MiB时间限制:1000 ms标准输入输出 题目类型:传统评测方式:Special Judge 上传者: 匿名 提交提交记录统计讨论测试数据 ...

  7. 洛谷 P2765 魔术球问题 解题报告

    P2765 魔术球问题 题目描述 问题描述: 假设有\(n\)根柱子,现要按下述规则在这\(n\)根柱子中依次放入编号为\(1,2,3,\dots\)的球. \((1)\) 每次只能在某根柱子的最上面 ...

  8. Libre 6003 「网络流 24 题」魔术球 (网络流,最大流)

    Libre 6003 「网络流 24 题」魔术球 (网络流,最大流) Description 假设有n根柱子,现要按下述规则在这n根柱子中依次放入编号为 1,2,3,4......的球. (1)每次只 ...

  9. [loj #6003]「网络流 24 题」魔术球 二分图最小路径覆盖,网络流

    #6003. 「网络流 24 题」魔术球 内存限制:256 MiB时间限制:1000 ms标准输入输出 题目类型:传统评测方式:Special Judge 上传者: 匿名 提交提交记录统计讨论测试数据 ...

随机推荐

  1. oracle怎么捕获表上的DML语句(不包括select)语句)

    可以采用dml触发器,如 CREATE OR REPLACE TRIGGER tr_capt_sql BEFORE DELETE OR INSERT OR UPDATE ON manager.test ...

  2. 使用PHP类TCPDF生成PDF文档

    转自:http://www.blhere.com/1180.html 这两天遇到一个项目中,需要php自动处理生成pdf文档.在网上找了好几个类,最后决定使用TCPDF,使用的时候真是发现这个类真是强 ...

  3. Maven学习总结--maven入门(一)

    一.Maven的基本概念 Maven(翻译为"专家","内行")是跨平台的项目管理工具.主要服务于基于Java平台的项目构建,依赖管理和项目信息管理.

  4. Ubuntu18.10创建软件图标

    解压下载包都/opt目录 创建并编辑/usr/share/applications/xxx.desktop [Desktop Entry] Encoding=UTF-8 Name=Pycharm Co ...

  5. du,df区别

    1.记住命令 du:disk Usage -h, --human-readable print sizes in human readable format df:disk free 2.区别 du ...

  6. 【算法】BSGS算法

    BSGS算法 BSGS算法用于求解关于x的模方程\(A^x\equiv B\mod P\)(P为质数),相当于求模意义下的对数. 思想: 由费马小定理,\(A^{p-1}\equiv 1\mod P\ ...

  7. 【批量添加】-拼接sql字符串 标签: 批量添加 2015-12-13 17:49 2070人阅读 评论(33)

    现在做的一个项目需要用到批量添加,但是封装的底层没有这个方法,所以自食其力,自己来写.我们用的是拼接sql字符串的方法来实现功能. 具体实现流程:首先将需要的数据存储到实体的list中,然后将这个li ...

  8. SecureCRT 7.1.1和SecureFx key 亲测可用

    CRT:Name: ygeRCompany: TEAM ZWTSerialNumber: 03-77-119256License Key: ABH2MJ 9YVAC5 Z17QF7 4ZAS7Z AB ...

  9. oracle函数 BFILENAME(dir,file)

    [功能]函数返回一个空的BFILE位置值指示符,函数用于初始化BFILE变量或者是BFILE列. [参数]dir是一个directory类型的对象,file为一文件名. insert into lob ...

  10. Mysql统计信息处理及binlog解释

    TODO use db_name; -- 分析表 ANALYZE TABLE table_name; -- 查看表信息 ; -- 查看索引 SHOW INDEX FROM table_name; ht ...