-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathall.cpp
More file actions
50 lines (50 loc) · 1.36 KB
/
all.cpp
File metadata and controls
50 lines (50 loc) · 1.36 KB
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
// 2025-06-15
#include <stack>
#include <stdio.h>
#include <vector>
using namespace std;
void do_testcase(int N1, int N2, int D) {
vector<int> in(N1 + N2);
vector<vector<int>> child(N1 + N2);
for (int i = 0; i < D; i++) {
int x, y; scanf("%d %d", &x, &y); --x; --y;
in[y]++; child[x].push_back(y);
}
int result = 1e9;
for (int sd = 0; sd <= 1; sd++) {
auto incopy = in;
stack<int> S[2];
for (int i = 0; i < N1; i++) {
if (incopy[i] == 0) S[0].push(i);
}
for (int i = N1; i < N1 + N2; i++) {
if (incopy[i] == 0) S[1].push(i);
}
int cur = 2; int d = sd; int rem = N1 + N2;
while (rem) {
if (S[d].empty()) {
++cur;
d = !d;
}
while (!S[d].empty()) {
const int u = S[d].top();
S[d].pop();
--rem;
for (const int v : child[u]) {
if (0 == --incopy[v]) {
if (v < N1) S[0].push(v); else S[1].push(v);
}
}
}
}
result = min(result, cur);
}
printf("%d\n", result);
}
int main() {
for (;;) {
int N1, N2, D; scanf("%d %d %d", &N1, &N2, &D);
if (N1 + N2 + D == 0) break;
do_testcase(N1, N2, D);
}
}