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
#include <iostream>
#include <vector>
#include <limits>

size_t brute(size_t n, size_t k, std::vector<bool> &active, const std::vector<std::vector<size_t>> &deps) {
    std::vector<size_t> counts(n);
    std::vector<size_t> prev(n);
    size_t count_top = 0;
    size_t tail;
    for (size_t i = 1; i < n; ++i) {
        if (!active[i]) {
            continue;
        }

        for (auto dep: deps[i]) {
            if (!active[dep]) {
                continue;
            }

            if (counts[i] < counts[dep] + 1) {
                counts[i] = counts[dep] + 1;
                prev[i] = dep;
            }
        }

        if (count_top <= counts[i]) {
            count_top = counts[i];
            tail = i;
        }
    }

    if (k == 0) {
        return count_top;
    } else {
        count_top = std::numeric_limits<size_t>::max();
        for (;;) {
            active[tail] = false;
            size_t count_new = brute(n, k - 1, active, deps);
            active[tail] = true;

            if (count_top > count_new) {
                count_top = count_new;
            }

            if (counts[tail] == 0) {
                return count_top;
            } else {
                tail = prev[tail];
            }
        }
    }
}

int main() {
    size_t n, m, k;
    std::cin >> n >> m >> k;

    if (k == n) {
        std::cout << "0\n";
    } else {
        std::vector<std::vector<size_t>> deps(n);
        for (size_t i = 0; i < m; ++i) {
            size_t x, y;
            std::cin >> x >> y;
            deps[y - 1].push_back(x - 1);
        }

        std::vector<bool> active(n, true);
        std::cout << brute(n, k, active, deps) + 1 << "\n";
    }
}