传送门

枚举球的个数 num

如果 i < j && (i + j) 是完全平方数,那么 i -> j' 连一条边

再加一个超级源点 s,s -> i

再加一个超级汇点 t,i' -> t

那么当前可以放的柱子的最小数量就是最小不相交路径数

如果当前的最小不相交路径数 > num,break

求最大流的时候别忘了记录方案

——代码

 #include <cmath>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#define N 10001
#define M 200001
#define mid 5000
#define min(x, y) ((x) < (y) ? (x) : (y)) int n, cnt, sum, ans, s, t = mid << ;
int head[N], to[M], next[M], val[M], suc[N], dis[N];
bool vis[N]; inline int read()
{
int x = , f = ;
char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -;
for(; isdigit(ch); ch = getchar()) x = (x << ) + (x << ) + ch - '';
return x * f;
} inline void add(int x, int y, int z)
{
to[cnt] = y;
val[cnt] = z;
next[cnt] = head[x];
head[x] = cnt++;
} inline bool bfs()
{
int i, u, v;
std::queue <int> q;
memset(dis, -, sizeof(dis));
q.push(s);
dis[s] = ;
while(!q.empty())
{
u = q.front(), q.pop();
for(i = head[u]; i ^ -; i = next[i])
{
v = to[i];
if(val[i] && dis[v] == -)
{
dis[v] = dis[u] + ;
if(v == t) return ;
q.push(v);
}
}
}
return ;
} inline int dfs(int u, int maxflow)
{
if(u == t) return maxflow;
int i, v, d, ret = ;
for(i = head[u]; i ^ -; i = next[i])
{
v = to[i];
if(val[i] && dis[v] == dis[u] + )
{
d = dfs(v, min(val[i], maxflow - ret));
ret += d;
val[i] -= d;
val[i ^ ] += d;
if(d) suc[u] = v - mid;
if(ret == maxflow) return ret;
}
}
return ret;
} int main()
{
int i, j, now, num = ;
n = read();
memset(head, -, sizeof(head));
while()
{
num++;
add(s, num, ), add(num, s, );
add(num + mid, t, ), add(t, num + mid, );
for(i = ; i < num; i++)
if(sqrt(i + num) == (int)sqrt(i + num))
add(i, num + mid, ), add(num + mid, i, );
while(bfs()) sum += dfs(s, 1e9);
if(num - sum > n) break;
}
cnt = ;
memset(head, -, sizeof(head));
for(i = ; i < num; i++)
{
add(s, i, ), add(i, s, );
add(i + mid, t, ), add(t, i + mid, );
}
for(i = ; i < num; i++)
for(j = ; j < i; j++)
if(sqrt(i + j) == (int)sqrt(i + j))
add(j, i + mid, ), add(i + mid, j, );
while(bfs()) dfs(s, 1e9);
printf("%d\n", num - );
for(i = ; i < num; i++)
if(!vis[i])
{
now = i;
while(now)
{
printf("%d ", now);
vis[now] = ;
now = suc[now];
}
puts("");
}
return ;
}

[luoguP2765] 魔术球问题(最大流—最小不相交路径覆盖)的更多相关文章

  1. Air Raid POJ - 1422 【有向无环图(DAG)的最小路径覆盖【最小不相交路径覆盖】 模板题】

    Consider a town where all the streets are one-way and each street leads from one intersection to ano ...

  2. P2172 [国家集训队]部落战争 二分图最小不相交路径覆盖

    二分图最小不相交路径覆盖 #include<bits/stdc++.h> using namespace std; ; ; ; ], nxt[MAXM << ], f[MAXM ...

  3. 网络费用流-最小k路径覆盖

    多校联赛第一场(hdu4862) Jump Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  4. POJ Air Raid 【DAG的最小不相交路径覆盖】

    传送门:http://poj.org/problem?id=1422 Air Raid Time Limit: 1000MS   Memory Limit: 10000K Total Submissi ...

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

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

  6. POJ1422Air Raid(二分图,最小不相交路径覆盖)

    Air Raid Consider a town where all the streets are one-way and each street leads from one intersecti ...

  7. 刷题总结——魔术球问题(ssoj最小路径覆盖+网络流)

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

  8. LuoguP2765 魔术球问题

    LuoguP2765 魔术球问题 首先,很难看出来这是一道网络流题.但是因为在网络流24题中,所以还是用网络流的思路 首先考虑完全平方数的限制. 如果\(i,j\)满足\(i < j\) 且 $ ...

  9. HDU 4862 Jump(最小K路径覆盖)

    输入一个n×m网格图,每个结点的值为0-9,可以从任意点出发不超过k次,走完每个点且仅访问每个结点一次,问最终的能量最大值.不可全部走完的情况输出-1. 初始能量为0. 而结点(x,y)可以跳跃到结点 ...

随机推荐

  1. SpringMVC-请求参数的绑定

    绑定的机制 表单提交的数据都是k=v格式的 username=haha&password=123 SpringMVC的参数绑定过程是把表单提交的请求参数,作为控制器中方法的参数进行绑定的 要求 ...

  2. c++调用系统关机命令 c++调用暂停命令

    #include<stdlib.h> int main() { //调用系统dos命令 system("shutdown -s -t 120"); ; } system ...

  3. NSString 使用 copy、strong

    // 首先定义2个属性 @property (nonatomic, strong) NSString *stStr; @property (nonatomic, copy) NSString *coS ...

  4. C# 使用Epplus导出Excel [5]:样式

    C# 使用Epplus导出Excel [1]:导出固定列数据 C# 使用Epplus导出Excel [2]:导出动态列数据 C# 使用Epplus导出Excel [3]:合并列连续相同数据 C# 使用 ...

  5. Title Case a Sentence-freecodecamp算法题目

    Title Case a Sentence(中单词首字母大写) 要求 确保字符串的每个单词首字母都大写,其余部分小写. 像'the'和'of'这样的连接符同理. 思路 将句子小写化后用.split(& ...

  6. tempfs详解

    致因 在平常工作中,我们经常需要查看Linux服务器磁盘挂载使用情况,可以使用df命令,不知大家注意到没有,我们使用此命令除了会查看到系统盘以及数据盘挂载情况,还会看到一个tmpfs也在挂载. [ro ...

  7. 用Python手把手教你搭建一个web框架-flask微框架!

    在之前的文章当中,小编已经教过大家怎么搭建一个Django框架,今天我们来探索另外的一种框架的搭建,这个框架就是web框架-flask微框架啦!首先我们带着以下的几个问题来阅读本文: 1.flask是 ...

  8. UVALive - 3942 Remember the Word (Trie + DP)

    题意: 给定一篇长度为L的小写字母文章, 然后给定n个字母, 问有多少种方法用这些字母组成文章. 思路: 用dp[i]来表达[i , L]的方法数, 那么dp[i] 就可以从dp[len(x) + i ...

  9. C++中重载、覆盖和隐藏的区别,以及适用场景

    一.重载.覆盖和隐藏的区别 二.适用场景 1.重载: 适用于不同的数据类型都需要使用到的功能函数.以数据相加的函数为例,可以在同一个文件内提供以下的重载函数以支持同样的功能: int add(int, ...

  10. 线段树:POJ3468-A Simple Problem with Integers(线段树注意事项)

    A Simple Problem with Integers Time Limit: 10000MS Memory Limit: 65536K Description You have N integ ...