[BOJ] 9577 토렌트

토렌트

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
#include <bits/stdc++.h>

#define MAX_N 101
#define ll long long
#define all(a) begin(a), end(a)
using namespace std;

ll n, m;
ll match[MAX_N], seen[MAX_N], a[MAX_N];
unordered_set<ll> d[MAX_N];

ll dfs(ll u, ll s) {
for(auto& v : d[u]) {
if(seen[v] == s) continue;
seen[v] = s;
if(match[v] == -1 or dfs(match[v], s)) {
match[v] = u;
return 1;
}
}
return 0;
}
ll solve() {
for(ll i = 0, ma = 0; i < MAX_N; i++) {
if(dfs(i, i))
ma++;
if(ma == n)
return i + 1;
}

return -1;
}

int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
ll tc;
cin>>tc;
while(tc--) {
for(ll i = 0; i < MAX_N; i++) d[i].clear();
cin>>n>>m;
ll f, e, seed;

while(m--) {
cin>>f>>e>>seed;
for(ll i = 0; i < seed; i++) cin>>a[i];
for(ll i = f; i < e; i++)
for(ll j = 0; j < seed; j++)
d[i].insert(a[j]);
}

memset(match, -1, sizeof(match));
memset(seen, -1, sizeof(seen));
cout<<solve()<<'\n';
}
return 0;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/04/22/PS/BOJ/9577/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.