# 2179. 圆桌问题()-其他

## 2179. 圆桌问题()

### 数据范围

\(1 \le m \le 150\),
\(1 \le n \le 270\),
\(1 \le r_i,c_i \le 100\)

### 输入样例：

``````4 5
4 5 3 5
3 5 2 6 4
``````

### 输出样例：

``````1
1 2 4 5
1 2 3 4 5
2 4 5
1 2 3 4 5
``````

### 解题思路

• 时间复杂度：\((n^2m)\)

### 代码

``````// Problem: 圆桌问题
// Contest: AcWing
// URL: https://www.acwing.com/problem/content/description/2181/
// Memory Limit: 64 MB
// Time Limit: 1000 ms
//

// %%%Skyqwq
#include <bits/stdc++.h>

//#define int long long
#define help {cin.tie(NULL); cout.tie(NULL);}
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }

template <typename T> void inline read(T &x) {
int f = 1; x = 0; char s = getchar();
while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
x *= f;
}

const int N=425,M=(N+150*270)<<1,inf=0x3f3f3f3f;
int m,n,s,t,res;
int h[N],ne[M],e[M],f[M],idx;
int q[N],hh,tt,d[N],cur[N];
{
e[idx]=b,f[idx]=c,ne[idx]=h[a],h[a]=idx++;
e[idx]=a,f[idx]=0,ne[idx]=h[b],h[b]=idx++;
}
int dfs(int x,int limit)
{
if(x==t)return limit;
int flow=0;
for(int i=cur[x];~i&&flow<limit;i=ne[i])
{
cur[x]=i;
int y=e[i];
if(d[y]==d[x]+1&&f[i])
{
int t=dfs(y,min(f[i],limit-flow));
if(!t)d[y]=-1;
f[i]-=t,f[i^1]+=t,flow+=t;
}
}
return flow;
}
bool bfs()
{
memset(d,-1,sizeof d);
q[0]=s;
d[s]=hh=tt=0;
cur[s]=h[s];
while(hh<=tt)
{
int x=q[hh++];
for(int i=h[x];~i;i=ne[i])
{
int y=e[i];
if(d[y]==-1&&f[i])
{
d[y]=d[x]+1;
cur[y]=h[y];
if(y==t)return true;
q[++tt]=y;
}
}
}
return false;
}

int dinic()
{
int res=0,flow;
while(bfs())while(flow=dfs(s,inf))res+=flow;
return res;
}
int main()
{
scanf("%d%d",&m,&n);
memset(h,-1,sizeof h);
s=0,t=n+m+1;
for(int i=1;i<=m;i++)
{
int r;
scanf("%d",&r);
res+=r;
}
for(int i=m+1;i<=n+m;i++)
{
int c;
scanf("%d",&c);
}
for(int i=1;i<=m;i++)
if(res!=dinic())puts("0");
else
{
puts("1");
for(int i=1;i<=m;i++)
{
for(int j=h[i];~j;j=ne[j])
if(e[j]>=m+1&&e[j]<=n+m&&!f[j])printf("%d ",e[j]-m);
puts("");
}
}
return 0;
}
``````
————————

### 数据范围

\(1 \le m \le 150\),
\(1 \le n \le 270\),
\(1 \le r_i,c_i \le 100\)

### 输入样例：

``````4 5
4 5 3 5
3 5 2 6 4
``````

### 输出样例：

``````1
1 2 4 5
1 2 3 4 5
2 4 5
1 2 3 4 5
``````

### 解题思路

• 时间复杂度：\((n^2m)\)

### 代码

``````// Problem: 圆桌问题
// Contest: AcWing
// URL: https://www.acwing.com/problem/content/description/2181/
// Memory Limit: 64 MB
// Time Limit: 1000 ms
//

// %%%Skyqwq
#include <bits/stdc++.h>

//#define int long long
#define help {cin.tie(NULL); cout.tie(NULL);}
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }

template <typename T> void inline read(T &x) {
int f = 1; x = 0; char s = getchar();
while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
x *= f;
}

const int N=425,M=(N+150*270)<<1,inf=0x3f3f3f3f;
int m,n,s,t,res;
int h[N],ne[M],e[M],f[M],idx;
int q[N],hh,tt,d[N],cur[N];
{
e[idx]=b,f[idx]=c,ne[idx]=h[a],h[a]=idx++;
e[idx]=a,f[idx]=0,ne[idx]=h[b],h[b]=idx++;
}
int dfs(int x,int limit)
{
if(x==t)return limit;
int flow=0;
for(int i=cur[x];~i&&flow<limit;i=ne[i])
{
cur[x]=i;
int y=e[i];
if(d[y]==d[x]+1&&f[i])
{
int t=dfs(y,min(f[i],limit-flow));
if(!t)d[y]=-1;
f[i]-=t,f[i^1]+=t,flow+=t;
}
}
return flow;
}
bool bfs()
{
memset(d,-1,sizeof d);
q[0]=s;
d[s]=hh=tt=0;
cur[s]=h[s];
while(hh<=tt)
{
int x=q[hh++];
for(int i=h[x];~i;i=ne[i])
{
int y=e[i];
if(d[y]==-1&&f[i])
{
d[y]=d[x]+1;
cur[y]=h[y];
if(y==t)return true;
q[++tt]=y;
}
}
}
return false;
}

int dinic()
{
int res=0,flow;
while(bfs())while(flow=dfs(s,inf))res+=flow;
return res;
}
int main()
{
scanf("%d%d",&m,&n);
memset(h,-1,sizeof h);
s=0,t=n+m+1;
for(int i=1;i<=m;i++)
{
int r;
scanf("%d",&r);
res+=r;
}
for(int i=m+1;i<=n+m;i++)
{
int c;
scanf("%d",&c);
}
for(int i=1;i<=m;i++)
if(res!=dinic())puts("0");
else
{
puts("1");
for(int i=1;i<=m;i++)
{
for(int j=h[i];~j;j=ne[j])
if(e[j]>=m+1&&e[j]<=n+m&&!f[j])printf("%d ",e[j]-m);
puts("");
}
}
return 0;
}
``````