Home BOJ. Allocating Cattle Shed (2188)
Post
Cancel

BOJ. Allocating Cattle Shed (2188)

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


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
#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 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 = 5e5 + 1;
const int INF = 987654321;

int n, m;
vector<vector<int>> edge;
vector<bool> visit;
vector<int> from;

bool dfs(int from_idx) {
  if(visit[from_idx]) return false;
  visit[from_idx] = true;
  for(int to: edge[from_idx]) {
    if(from[to] == -1 || dfs(from[to])) {
      from[to] = from_idx;
      return true;
    }
  }
  return false;
}

int main(int argc, char *argv[]) {
  cin >> n >> m;
  from = vector<int>(m, -1);
  edge.resize(n);
  for0(i, n) {
    int in; cin >> in; edge[i].resize(in);
    for0(j, in) { int in; cin >> in; edge[i][j] = in - 1; }
  }
  int cnt = 0;
  for0(i, n) {
    visit = vector<bool>(n, false);
    if(dfs(i)) cnt++;
  }
  cout << cnt;
  return 0;
}
This post is licensed under CC BY 4.0 by the author.