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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include <bits/stdc++.h>

using namespace std;

#define PB push_back
#define FORE(i, t) for(__typeof(t.begin())i=t.begin();i!=t.end();++i)
#define SZ(x) int((x).size())
#define REP(i, n) for(int i=0,_=(n);i<_;++i)
#define FOR(i, a, b) for(int i=(a),_=(b);i<=_;++i)
#define FORD(i, a, b) for(int i=(a),_=(b);i>=_;--i)

#ifdef DEBUG
#define DEB(x) (cerr << x)
#else
#define DEB(x)
#endif

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;

const int MAX_N = 301;
const int INF = 1e9 + 9;

int n, m, k, n2;
vi edges[MAX_N];
vi rev[MAX_N];

int rights[MAX_N];
int lefts[MAX_N];
int longest[MAX_N];
bool reduced[MAX_N] = {};
int chosen[MAX_N];

vi edges2[MAX_N];
int chosen2[MAX_N];
int length2[MAX_N];
bool removed2[MAX_N] = {};

void compute_rights() {
    FOR (x, 1, n) {
        rights[x] = 1;
    }
    FOR (x, 1, n) {
        FORE(yt, edges[x]) {
            int y = *yt;
            rights[y] = max(rights[y], rights[x] + 1);
        }
    }
    FOR (x, 1, n) {
//        cout << "x=" << x << " rights: " << rights[x] << "\n";
    }
}

void compute_lefts() {
    FOR (x, 1, n) {
        lefts[x] = 1;
    }
    FORD (x, n, 1) {
        FORE(yt, rev[x]) {
            int y = *yt;
            lefts[y] = max(lefts[y], lefts[x] + 1);
        }
    }
    FOR (x, 1, n) {
//        cout << "x=" << x << " lefts: " << lefts[x] << "\n";
    }
}

void compute_longest() {
    FOR (x, 1, n) {
        longest[x] = lefts[x] + rights[x] - 1;
    }
//    FOR (x, 1, n) {
////        cout << "x=" << x << " longest: " << longest[x] << "\n";
////        cout << longest[x] << "\n";
//    }
}

int compute_length() {
    REP(x2, n2) {
        length2[x2] = removed2[x2] ? 0 : 1;
    }
    REP(x2, n2) {
//        DEB("cl x2=" << x2 << "\n");
        if (removed2[x2]) {
            continue;
        }
        FORE(yt2, edges2[x2]) {
            int y2 = *yt2;
//            DEB("cl y2=" << y2 << "\n");
            if (removed2[y2]) {
                continue;
            }
            length2[y2] = max(length2[y2], length2[x2] + 1);
        }
    }
    int max_length = 0;
    REP(x2, n2) {
        max_length = max(max_length, length2[x2]);
    }
    return max_length;
}

void inline one() {
    cin >> n >> m >> k;
    REP(i, m) {
        int x, y;
        cin >> x >> y;
        edges[x].PB(y);
        rev[y].PB(x);
    }
    compute_rights();
    compute_lefts();
    compute_longest();

    // Choose reduced:
    vector <pii> v;
    FOR(i, 1, n) {
        v.PB(pii(-longest[i], i));
    }
    sort(v.begin(), v.end());

    FOR(i, 1, n) {
        reduced[i] = true;
    }

    n2 = min(n, 70);
    REP(i, n2) {
        int x = v[i].second;
        chosen2[i] = x;
        DEB("chosen=" << x << "\n");

    }
    // Renumerate:
    sort(chosen2, chosen2 + n2);
    REP (x2, n2) {
        int x = chosen2[x2];
        chosen[x] = x2;
        reduced[x] = false;
    }
    FOR (x, 1, n) {
        DEB("x=" << x << " longest=" << longest[x] << " reduced=" << reduced[x] << "\n");
    }

    DEB("n2=" << n2 << " chosen2=" << '-' << "\n");
    REP(x2, n2) {
        DEB("x2=" << x2 << " chosen2=" << chosen2[x2] << "\n");
    }

    // Build reduced edges2:
    REP(x2, n2) {
        int x = chosen2[x2];
//        DEB("build x=" << x << " x2=" << x2 << "\n");
        FORE(yt, edges[x]) {
            int y = *yt;
            if (!reduced[y]) {
                int y2 = chosen[y];
//                DEB("build y=" << y << " y2=" << y2 << "\n");
                edges2[x2].PB(y2);
            }
        }
    }


    // Calculate result:
    int start_longest = compute_length();
    int best = start_longest;
    if (k > 0) {
        REP(first, n2) {
            if (longest[chosen2[first]] != start_longest) {
                continue;
            }
            DEB("first2=" << first << " first=" << chosen2[first] << "\n");
            REP(i, n2) {
                removed2[i] = false;
            }
            FORD(i, n2 - 1, n2 - (k - 1)) {
                removed2[i] = true;
            }
            do {
//        REP(x2, n2) {
//            cout << (int) removed2[x2];
//        }
//        cout << "\n";
                if (!removed2[first]) {
                    removed2[first] = true;
                    best = min(best, compute_length());
                    removed2[first] = false;
                }
            } while (next_permutation(removed2, removed2 + n2));
        }
    }

    cout << best << "\n";
}

int main() {
    ios::sync_with_stdio(false);
    //int z; cin >> z; while(z--)
    one();
}