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
203
204
205
#include <iostream>
#include "bits/stdc++.h"
using namespace std;

#define MAXN 301

int n, m, k;
vector<int> edge[MAXN];
vector<int> backEdge[MAXN];
bool isTerminal[MAXN];
int depth[MAXN];
int maxDepth = 0;
bool onQueue[MAXN];
bool removed[MAXN];

struct Memento {
    vector<int> nodes;
    vector<pair<int, int> > edges;

    void reset() {
        nodes.clear();
        edges.clear();
    }

    void rollback() {
        for(int i = 1; i <= n; i++) {
            removed[i] = false;
        }
        for(auto it = edges.begin(); it != edges.end(); it++) {
            int x = it->first, y = it->second;
            edge[x].push_back(y);
        }
        reset();
    }
}memento;

long long int calculateComplexity(int n, int k);

void zeroArrays() {
    for(int i = 1; i <= n; ++i) {
        isTerminal[i] = false;
        depth[i] = 0;
        onQueue[i] = false;
    }
}

bool isEdgeTerminal(int a) {
    if(edge[a].empty())
        return true;
    for(auto it = edge[a].begin(); it != edge[a].end(); it++) {
        if(!isTerminal[*it])
            return false;
    }
    return true;
}

int longestPath() {
    queue<int> q;
    for(int i = 1; i <= n; ++i) {
        if(removed[i])
            continue;
        if(edge[i].empty()) {
            depth[i] = 1;
            isTerminal[i] = true;
            for(auto it = backEdge[i].begin(); it != backEdge[i].end(); it++) {
                if(!onQueue[*it]) {
                    q.push(*it);
                    onQueue[*it] = true;
                }
            }
        }
    }

    while(!q.empty()) {
        int a = q.front();
        q.pop();
        if(isEdgeTerminal(a)) {
            int max = 1;
            for(auto it = edge[a].begin(); it != edge[a].end(); it++) {
                if(removed[*it])
                    continue;
                if(depth[*it] > max)
                    max = depth[*it];
            }
            depth[a] = max + 1;
            isTerminal[a] = true;
            for(auto it = backEdge[a].begin(); it != backEdge[a].end(); it++) {
                if(removed[*it])
                    continue;
                if(!onQueue[*it]) {
                    q.push(*it);
                    onQueue[*it] = true;
                }
            }
        } else {
            q.push(a); // back
        }
    }

    int max = 0;
    for(int j = 1; j <= n; ++j) {
        if(removed[j])
            continue;
        if(depth[j] > max)
            max = depth[j];
    }
    return max;
}

void remove(int node) {
    removed[node] = true;
    memento.nodes.push_back(node);
    for(auto it = edge[node].begin(); it != edge[node].end(); it++) {
        auto e = make_pair(node, *it);
        memento.edges.push_back(e);
    }
    edge[node].clear();
    for(auto it = backEdge[node].begin(); it != backEdge[node].end(); it++) {
        auto e = make_pair(*it, node);
        memento.edges.push_back(e);
        for(auto eit = edge[*it].begin(); eit != edge[*it].end(); eit++) {
            if(*eit == node) {
                edge[*it].erase(eit);
                break;
            }
        }
    }
}

int main() {
//    fstream cin;
//    cin.open("test/her2.in", ios_base::in);
//    assert(cin.is_open());

    cin >> n >> m >> k;

    if(n == k) {
        cout << 0 << endl;
        return 0;
    }

    for(int i = 0; i < m; ++i) {
        int a, b;
        cin >> a >> b;
        edge[a].push_back(b);
        backEdge[b].push_back(a);
    }

    int min = n;

    for(int i = 0; i < k; i++) {
        int chosen = -1;
        maxDepth = longestPath();
        int middle = maxDepth / 2;
        if(maxDepth % 2 == 1)
            middle++;
        for(int i = 1; i <= n; i++) {
            if(depth[i] = middle) {
                chosen = i;
                break;
            }
        }
        remove(chosen);
    }

    zeroArrays();
    min = std::min(min, longestPath());

    memento.rollback();

    for(int i = 0; i < k; i++) {
        //choose a node to remove
        int chosen = 1;
        int min = n;
        for(int node1 = 1; node1 <= n; node1++) {
            remove(node1);
            zeroArrays();
            int score = longestPath();
            if(score < min) {
                min = score;
                chosen = node1;
            }
            memento.rollback();
        }

        //remove it
        remove(chosen);
    }

    zeroArrays();
    min = std::min(min, longestPath());

    cout << min << endl;

    return 0;
}

long long int calculateComplexity(int n, int k) {
    long long res = 1;
    while(k != 0) {
        k--;
        res *= n;
    }
    return res;
}