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
#include <iostream>
#include <string>
#include <vector>
#include <unordered_set>
#include <algorithm>
#include <queue>

using namespace std;

vector<int> calcpaths(const vector<vector<int>>& children, int n, unordered_set<int>& removed){

  vector<int> ret(n,1);
  for(int i = n - 1; i >= 0; i--){
    if(removed.find(i) != removed.end()){
      continue;
    }
    for(auto child: children[i]){
      if(removed.find(child) == removed.end()){
        ret[i] = max (ret[i], ret[child] + 1);
      }
    }
  }
  return ret;
}

int solve(const vector<vector<int>>& children, int n, int k, unordered_set<int> removed = {}, unordered_set<int> seen = {}){

  if(k == n){
    return 0;
  }
  vector<int> pathsc = calcpaths(children, n, removed);
  int maxpath = *(max_element(pathsc.begin(), pathsc.end()));
  int ret = maxpath;
  if(k == 0){
    return maxpath;
  } else {
    {
      vector<int> width(maxpath, 0);
      priority_queue<int> toexplore = {};
      for (int i = 0; i < n; i ++){
        if(pathsc[i] == maxpath){
          toexplore.push(i);
        }
      }
      while(!toexplore.empty()){
        int cur = toexplore.top();
        toexplore.pop();
        width[pathsc[cur] - 1] ++;
        for(auto child : children[cur]){
          if(pathsc[child] == pathsc[cur] - 1){
            toexplore.push(child);
          }
        }
      }
      int diameter = *(min_element(pathsc.begin(), pathsc.end()));
      if(diameter > k){
        return maxpath;
      }
    }
    {
      priority_queue<int> toexplore = {};
      for (int i = 0; i < n; i ++){
        if(pathsc[i] == maxpath){
          toexplore.push(i);
        }
      }
      while(!toexplore.empty()){
        int cur = toexplore.top();
        toexplore.pop();
        for(auto child : children[cur]){
          if(pathsc[child] == pathsc[cur] - 1){
            toexplore.push(child);
          }
        }
        if(seen.find(cur) != seen.end()){
          continue;
        }
        auto removedcp = removed;
        removedcp.insert(cur);
        seen.insert(cur);
        ret = min(ret, solve(children, n, k - 1, removedcp, seen));
      }
    }

  }
  return ret;

}

int main(){

  int n, m, k;

  cin >> n >> m >> k;

  vector<vector<int>> children;
  for(int i = 0; i < n ; i++){
    children.emplace_back();
  }

  for(int i = 0; i < m; i++){

    int a, b;
    cin >> a >> b;
    children[a-1].emplace_back(b - 1);
  }
  cout << solve(children, n, k) << endl;

}