魔术球问题
https://www.luogu.org/problemnew/show/P2765
题解:网络流+最大流
/*
*@Author: STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
#define endl "\n"
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=10000+10;
const int M=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int s,t,n,m,k,p,l,r,u,v;
int ans,cnt,flag,temp,sum,maxflow;
int dis[N];
int pre[N],head[N];
bool vis[N];
struct node{
int u,v;
ll c;
node(){};
node(int u,int v,ll cap):u(u),v(v),c(cap){}
};
vector<node>edge;
vector<int>G[N];
int find(int x){return pre[x]==x?x:pre[x]=find(pre[x]);}
void Addedge(int u,int v,ll cap){
edge.push_back({u,v,cap});
edge.push_back({v,u,0});
int sz=edge.size();
G[u].push_back(sz-2);
G[v].push_back(sz-1);
}
bool bfs(int u){
memset(dis,-1,sizeof(dis));
dis[u]=0;
queue<int>q;
q.push(u);
while(!q.empty()){
int u=q.front();
q.pop();
for(int i=0,j=G[u].size();i<j;i++){
node e=edge[G[u][i]];
if(dis[e.v]<0&&e.c>0){
dis[e.v]=dis[u]+1;
q.push(e.v);
}
}
}
return dis[t]>0;
}
ll dfs(int u,ll flow){
if(u==t){
return flow;
}
ll now;
for(int i=0,j=G[u].size();i<j;i++){
node e=edge[G[u][i]];
if(e.c>0&&dis[e.v]==dis[u]+1&&(now=dfs(e.v,min(flow,e.c)))){
edge[G[u][i]].c-=now;
edge[G[u][i]^1].c+=now;
pre[u >> 1] = e.v >> 1;
return now;
}
}
return 0;
}
ll dinic(){
ll res;
ll maxflow=0;
while(bfs(s)){
while((res=dfs(s,INF))){
maxflow+=res;
}
}
return maxflow;
}
void init(){
s=0;
t=10000;
edge.clear();
for(int i=s;i<=t;i++)G[i].clear();
ans=0;
maxflow=0;
memset(vis,0,sizeof(vis));
memset(pre,0,sizeof(pre));
}
int main()
{
#ifdef DEBUG
freopen("input.in", "r", stdin);
//freopen("output.out", "w", stdout);
#endif
//ios::sync_with_stdio(false);
//cin.tie(0);
//cout.tie(0);
//scanf("%d",&t);
//while(t--){
while(~scanf("%d",&n)){
init();
int all=0;
int num=0;
while(all<=n){
num++;
Addedge(s,num<<1,1);Addedge((num<<1)|1,t,1);
for(int i=sqrt(num)+1;i*i<2*num;++i)
Addedge((i*i-num)<<1,(num<<1)|1,1);
if(!dinic()) head[++all]=num;
}
printf("%d\n",--num);
for(int i = 1; i <= n; i++) {
if(!vis[head[i]]) {
for(int u = head[i]; u != 0 && u != (t >> 1); u = pre[u]) {
vis[u] = true;
printf("%d%c",u," \n"[pre[u]==0||pre[u] == (t >> 1)]);
}
}
}
}
//}
#ifdef DEBUG
printf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif
//cout << "Hello world!" << endl;
return 0;
}