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
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <vector>

// #include <iostream>

int N, M, K;

struct Node {
    int id = 0;
    int in_degree = 0;

    int visited = 0;
    bool deleted = false;

    int distance = 0;
    Node* prev_node_ptr = NULL;

    std::vector<int> edges;
};

std::vector<Node> graph;

void dfs(std::vector<Node*> &topsort, Node &node)
{
    node.visited = true;

    for (int child_id : node.edges)
    {
        Node &child = graph[child_id];
        if (!child.visited && !child.deleted)
            dfs(topsort, child); 
    }

    topsort.push_back(&node);
}

void max_path(std::vector<Node*> &path)
{
    for (auto &node : graph) {
        node.visited = false;
        node.distance = 0;
        node.prev_node_ptr = NULL;
    }

    std::vector<Node*> topsort;
    topsort.reserve(N);
    for (auto &node : graph)
        if (!node.visited && !node.deleted)
            dfs(topsort, node);

    std::reverse(topsort.begin(), topsort.end());

    //std::cerr << "#topsort: " << topsort.size() << std::endl;

    Node *max_node_ptr = &graph[0];
    max_node_ptr->distance = -1;
    for (auto node_ptr : topsort)
      if (!node_ptr->deleted)
        for (int child_id : node_ptr->edges)
        {
            Node &child = graph[child_id];
            if (!child.deleted && child.distance < node_ptr->distance + 1)
            {
                child.distance = node_ptr->distance + 1;
                child.prev_node_ptr = node_ptr;

                if (max_node_ptr->distance < child.distance)
                    max_node_ptr = &child;
            }
        }

    if (max_node_ptr->id != 0)
    {
        while (max_node_ptr != NULL) 
        {
            path.push_back(max_node_ptr);
            max_node_ptr = max_node_ptr->prev_node_ptr;
        }
        // std::cerr << "path[0]->distance " << path[0]->distance << " path.size " << path.size() << std::endl; 
        // assert(path[0]->distance == path.size() - 1);
    }
}

int solve(int k)
{
    std::vector<Node*> path;
    path.reserve(N);
    max_path(path);

    /*
    std::cerr << "k " << k << " path ";
    for (auto ptr : path)
        std::cerr << ptr->id << " -> ";
    std::cerr << std::endl;
    */

    if (k == 0)
        return (path.size() == 1 ? 0 : path.size());

    if (path.empty())
        return 0;

    int min_size = 1e9;
    for (auto node_ptr : path)
    {
        node_ptr->deleted = true;
        // std::cerr << "k " << k << " delete " << node_ptr->id << std::endl;
        min_size = std::min(min_size, solve(k-1));
        node_ptr->deleted = false;
    }

    return min_size;
}

int main() {

    scanf("%d %d %d", &N, &M, &K);

    if (N == 1)
    {
        printf ("0\n");
        return 0;
    }

    graph.resize(N+1);
    for (int i = 0; i < M; ++i)
    {
        int from, to;
        scanf("%d %d", &from, &to);
        graph[from].edges.push_back(to);
        graph[to].in_degree += 1;
    }

    for (int i = 0; i <= N; ++i) 
    {
        graph[i].id = i;
        if (graph[i].in_degree == 0 && graph[i].edges.empty())
            graph[i].deleted = true;
    }
    //assert(graph[0].deleted);

    printf("%d\n", solve(K));

    return 0;
}