Home BOJ. Passionate Gang-Ho 4 (11378)
Post
Cancel

BOJ. Passionate Gang-Ho 4 (11378)

[Link] https://www.acmicpc.net/problem/11378


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
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <cstring>
#include <stack>
#include <cmath>
// #include <bits/stdc++.h>
using namespace std;
#define FASTIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define command_param int argc, char *argv[]
#define for0(i, n) for(int i = 0; i < n; i++)
#define for1(i, n) for(int i = 1; i < n; i++)
#define fori(s, e) for(int i = s; i < e; i++)
#define ll long long
#define endl "\n"
#define nulls '\0'
#define dkdk " "
const int MAX = 2002;
const int INF = 987654321;

vector<vector<int>> edge;
vector<vector<int>> capacity;
vector<vector<int>> flow;
vector<int> pre;
int n, m, k;
int start, second, end_idx;


int edmonds_karp() {
  queue<int> q;
  int tot_flow = 0;
  while(1) {
    q = {}; q.push(0);
    fill(pre.begin(), pre.end(), -1);
    while(!q.empty()) {
      int cur = q.front(); q.pop();
      if(cur == end_idx) break;
      for(int next: edge[cur]) {
        if(pre[next] == -1 && capacity[cur][next] - flow[cur][next] > 0) {
          pre[next] = cur;
          q.push(next);
        }
      }
    }
    if(pre[end_idx] == -1) break;
    int min_flow = INF;
    for(int i = end_idx; i; i = pre[i]) min_flow = min(min_flow, capacity[pre[i]][i] - flow[pre[i]][i]);
    for(int i = end_idx; i; i = pre[i]) {
      flow[pre[i]][i] += min_flow;
      flow[i][pre[i]] -= min_flow;
    }
    tot_flow += min_flow;
  }
  return tot_flow;
}

int main() {
  FASTIO;
  cin >> n >> m >> k;
  edge.resize(n + m + 3);
  pre.resize(n + m + 3);
  capacity.resize(n + m + 3);
  flow.resize(n + m + 3);
  start = 0, second = n+m+1, end_idx = n+m+2;
  for0(i, n+m+3) { capacity[i].resize(n+m+3); flow[i].resize(n+m+3); }
  edge[start].push_back(second); capacity[start][second] = k;
  edge[second].push_back(start);
  for1(i, n + 1) {
    int work_num; cin >> work_num;
    edge[start].push_back(i);
    edge[i].push_back(start);
    capacity[start][i] = 1;
    edge[second].push_back(i);
    edge[i].push_back(second);
    capacity[second][i] = k;
    for0(j, work_num) {
      int in; cin >> in;
      in += n;
      edge[i].push_back(in);
      capacity[i][in] = 1;
      edge[in].push_back(i);

      edge[in].push_back(end_idx);
      edge[end_idx].push_back(in);
      capacity[in][end_idx] = 1;
    }
  }
  cout << edmonds_karp();
  return 0;
}
This post is licensed under CC BY 4.0 by the author.