Inspection(有下界的最小流)

描述

传送门:UVaLive4597

You are in charge of a team that inspects a new ski resort. A ski resort is situated on several mountains and consists of a number of slopes. Slopes are connected with each other, forking and joining. A map of the ski resort is represented as an acyclic directed graph. Nodes of the graph represent different points in ski resort and edges of the graph represent slopes between the points, with the direction of edges going downwards.
Your team has to inspect each slope of the ski resort. Ski lifts on this resort are not open yet, but you have a helicopter. In one flight the helicopter can drop one person into any point of the resort. From the drop off point the person can ski down the slopes, inspecting each slope as they ski. It is fine to inspect the same slope multiple times, but you have to minimize the usage of the helicopter. So, you have to figure out how to inspect all the slopes with the fewest number of helicopter flights.

Input

Input consists of several datasets. The first line of each dataset contains a single integer number $n$ $(2 ≤ n ≤ 100)$ — the number of points in the ski resort. The following $n$ lines of the input file describe each point of the ski resort numbered from $1$ to $n$. Each line starts with a single integer number $m_i$ ($0 ≤ m_i < n$ for $i$ from $1$ to $n$) and is followed by mi integer numbers aij separated by spaces. All $a_{ij}$ are distinct for each $i$ and each $a_{ij}$ $(1 ≤ a_{ij} ≤ n, a_{ij} ≠ i)$ represents a slope going downwards from point $i$ to point $a_{ij}$ . Each point in the resort has at least one slope connected to it.

Output

On the first line for each dataset write a single integer number $k$ — the minimal number of helicopter flights that are needed to inspect all slopes. Then write $k$ lines that describe inspection routes for each helicopter flight. Each route shall start with single integer number from $1$ to $n$ — the number of the drop off point for the helicopter flight, followed by the numbers of points that will be visited during inspection in the corresponding order as the slopes are inspected going downwards. Numbers on a line shall be separated by spaces. You can write routes in any order.

Sample Input

1
2
3
4
5
6
7
8
9
8
1 3
1 7
2 4 5
1 8
1 8
0
2 6 5
0

Sample Output

1
2
3
4
5
4
1 3 4 8
1 3 5 8
2 7 6
7 5

思路

  • 由于每条边至少走一次,所以可以理解为一个有源汇的有下界(下界为1)的最小流。(源汇自己加,连接每个点,容量无穷大)
  • 对于求解有有源汇的最小流,姿势有很多。这题我是分离必要弧之后,跑附加源汇的最大流,二分答案(即汇点到源点的边的容量上界)。(还有一个更好的姿势可以看)
  • 输出路径的话,只要记录每个边的访问次数,然后去DFS一下就好了,具体要注意的细节看代码。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <bits/stdc++.h>
using namespace std;
#define clr(a, x) memset(a, x, sizeof(a))
#define mp(x, y) make_pair(x, y)
#define pb(x) push_back(x)
#define X first
#define Y second
#define fastin \
ios_base::sync_with_stdio(0); \
cin.tie(0);
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef vector<int> VI;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-6;

const int maxn = 105;
struct Edge
{
int from, to, cap, flow;
Edge(int u, int v, int c, int f) : from(u), to(v), cap(c), flow(f) {}
};
struct Dinic
{
int n, m, s, t; //结点数,边数(包括反向弧),源点编号和汇点编号
vector<Edge> edges; //边表。edge[e]和edge[e^1]互为反向弧
vector<int> G[maxn]; //邻接表,G[i][j]表示节点i的第j条边在e数组中的序号
bool vis[maxn]; //BFS使用
int d[maxn]; //从起点到i的距离
int cur[maxn]; //当前弧下标
void init(int n)
{
this->n = n;
for (int i = 0; i < n; i++) G[i].clear();
edges.clear();
}
void AddEdge(int from, int to, int cap)
{
edges.pb(Edge(from, to, cap, 0));
edges.pb(Edge(to, from, 0, 0));
m = edges.size();
G[from].pb(m - 2);
G[to].pb(m - 1);
}
bool BFS()
{
clr(vis, 0);
clr(d, 0);
queue<int> q;
q.push(s);
d[s] = 0;
vis[s] = 1;
while (!q.empty())
{
int x = q.front();
q.pop();
for (int i = 0; i < G[x].size(); i++)
{
Edge& e = edges[G[x][i]];
if (!vis[e.to] && e.cap > e.flow)
{
vis[e.to] = 1;
d[e.to] = d[x] + 1;
q.push(e.to);
}
}
}
return vis[t];
}
int DFS(int x, int a)
{
if (x == t || a == 0) return a;
int flow = 0, f;
for (int& i = cur[x]; i < G[x].size(); i++)
{
//从上次考虑的弧
Edge& e = edges[G[x][i]];
if (d[x] + 1 == d[e.to] && (f = DFS(e.to, min(a, e.cap - e.flow))) > 0)
{
e.flow += f;
edges[G[x][i] ^ 1].flow -= f;
flow += f;
a -= f;
if (a == 0) break;
}
}
return flow;
}
int Maxflow(int s, int t)
{
this->s = s;
this->t = t;
int flow = 0;
while (BFS())
{
clr(cur, 0);
flow += DFS(s, INF);
}
return flow;
}
} ans;

int in[maxn], out[maxn];
vector<int> G[maxn];
bool check(int flow, const int& n)
{
int s = 0, t = n + 1, S = n + 2, T = n + 3;
ans.init(T + 1);
int sum = 0;
for (int u = 1; u <= n; u++)
{
ans.AddEdge(s, u, INF);
ans.AddEdge(u, t, INF);
for (auto& v : G[u])
ans.AddEdge(u, v, INF);
}
ans.AddEdge(t, s, flow);
for (int u = 1; u <= n; u++)
{
int tmp = in[u] - out[u];
if (tmp > 0)
ans.AddEdge(S, u, tmp), sum += tmp;
else if (tmp < 0)
ans.AddEdge(u, T, -tmp);
}
return sum == ans.Maxflow(S, T);
}

int cap[maxn][maxn];
int ANS[maxn];
bool dfs(int u, int dep, const int& t)
{
ANS[dep] = u;
if (u == t)
{
for (int i = 1; i < dep; i++)
printf("%d%c", ANS[i], i == dep - 1 ? '\n' : ' ');
return true;
}
for (int v = 1; v <= t; v++)
{
if (cap[u][v])
{
cap[u][v]--;
if (dfs(v, dep + 1, t))
return true;
}
}
return false;
}

int main()
{
#ifndef ONLINE_JUDGE
freopen("1.in", "r", stdin);
freopen("1.out", "w", stdout);
#endif
int n, m;
while (~scanf("%d", &n))
{
clr(in, 0), clr(out, 0), clr(cap, 0);
for (int u = 1; u <= n; u++)
{
G[u].clear();
scanf("%d", &m);
while (m--)
{
int v;
scanf("%d", &v);
G[u].pb(v);
cap[u][v] = 1;
out[u]++, in[v]++;
}
}
int L = 0, R = INF, ret;
while (L <= R)
{
int mid = (L + R) >> 1;
if (check(mid, n))
R = mid - 1, ret = mid;
else
L = mid + 1;
}
printf("%d\n", ret);
check(ret, n);
int s = 0, t = n + 1;
for (int u = 0; u <= n; u++)
for (auto& x : ans.G[u])
{
Edge& e = ans.edges[x];
int v = e.to;
if (u != s && v != t && cap[u][v] == 0) continue;
if (v > n + 1) continue;
int tmp = e.flow + 1 - (u == s || v == t);
if (tmp <= 0) continue;
cap[u][v] = tmp;
// printf("%d %d = %d\n", u, v, tmp);
}
for (int u = 1; u <= n; u++)
while (cap[s][u])
{
// printf("u = %d\n", u);
cap[s][u]--;
dfs(u, 1, t);
}
}
return 0;
}
捐助作者