Analysis
https://www.cnblogs.com/qixingzhi/p/9419319.html
http://hzwer.com/1878.html
放两个链接,这两篇题解对比着看
理解每个网络流的写法和匹配数的统计方法
更深入地理解这道题
【有时间再来填坑吧···】
Code
#include<bits/stdc++.h>
#define re register
using namespace std;
const int inf=1e9;
const int mN=200000,M=600009;
int head[mN],nxt[M],to[M],cap[M],ecnt=1;
int _head[mN],_nxt[M],_to[M],_cap[M],_ecnt=1;
inline void add(int x,int y,int w){
nxt[++ecnt]=head[x];head[x]=ecnt;to[ecnt]=y;cap[ecnt]=w;
nxt[++ecnt]=head[y];head[y]=ecnt;to[ecnt]=x;cap[ecnt]=0;
}
int S,T,n,m;
int cur[mN],lev[mN];
bool bfs(){
queue<int> q;
for(re int i=S;i<=T;++i) lev[i]=-1,cur[i]=head[i];
lev[S]=0;q.push(S);
while(!q.empty()){
int u=q.front();q.pop();
for(re int e=head[u];e;e=nxt[e]){
int v=to[e];
if(lev[v]!=-1||cap[e]<=0) continue;
lev[v]=lev[u]+1;
if(v==T) return 1;
q.push(v);
}
}
return 0;
}
int dinic(int u,int Flow){
if(u==T||Flow==0) return Flow;
int delta=0,res=0;
for(re int &e=cur[u];e;e=nxt[e]){
int v=to[e];
if(lev[v]>lev[u]&&cap[e]>0){
delta=dinic(v,min(Flow-res,cap[e]));
if(delta){
cap[e]-=delta;cap[e^1]+=delta;
res+=delta;if(res==Flow) return res;
}
}
}
return res;
}
int P,match,N,pre[mN],suf[mN];
void work(){
while(bfs()) match-=dinic(S,inf);
}
void addedge(int u,int v){
add(u*2,v*2+1,1);
}
int main(){
scanf("%d",&P);
S=0;T=12345;
for(N=1;;N++){
match++;
for(re int i=1;i<=N;++i){
if(i*i-N>=N) break;
if(i*i-N>0) addedge(i*i-N,N);
}
add(S,N*2,1);add(N*2+1,T,1);
work();
if(match>P){N--;break;}
for(re int i=1;i<=ecnt;++i){_nxt[i]=nxt[i];_to[i]=to[i];_cap[i]=cap[i];}
for(re int i=S;i<=N*2+4;++i) _head[i]=head[i];
_head[T]=head[T];
}
printf("%d\n",N);
for(re int i=2;i<=N*2+1;i+=2){
for(re int e=_head[i];e;e=_nxt[e]){
int v=_to[e];
if(_cap[e]==0){
pre[v/2]=i/2;
suf[i/2]=v/2;
}
}
}
for(re int i=1;i<=N;++i){
if(pre[i]==0){
int x=i;
while(suf[x]){
printf("%d ",x);
x=suf[x];
}
printf("%d\n",x);
}
}
return 0;
}
//QwQ