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
206
207
208
209
210
211
212
213
214
215
216
#include<cstdio>                                                                
#include<iostream>                                                              
#include<algorithm>                                                             
#include<string>                                                                
#include<vector>                                                                
#include<set>
#include<cmath>                                                              
                                                                                
using namespace std;                                                            
                                                                                
typedef vector<int> VI;                                                         
typedef long long LL;                                                           
                                                                                
#define FOR(x, b, e) for(int x=b; x<=(e); ++x)                                  
#define FORD(x, b, e) for(int x=b; x>=(e); --x)                                 
#define REP(x, n) for(int x=0; x<(n); ++x)                                      
#define VAR(v, n) __typeof(n) v = (n)                                           
#define ALL(c) (c).begin(), (c).end()                                           
#define SIZE(x) ((int)(x).size())                                               
#define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)             
#define PB push_back                                                            
#define ST first                                                                
#define ND second      

struct Result {
    int longest_path;
    set<int> candidates;

    Result(int l, const set<int> &c): longest_path(l), candidates(c) {}
};
                                                                                
class Solver {                                                                  
    int n, m, k;
    vector<VI > graph, reversed;
    public:

    void readInput() {
        cin >> n >> m >> k;
        graph.resize(n);
        reversed.resize(n);
        REP(i, m) {
            int x,y;
            cin >> x >> y;
            graph[x - 1].PB(y - 1);
            reversed[y - 1].PB(x - 1);
        }
    }

    void printGraph(vector<VI > &g) {
        REP(i, g.size()) {
            printf("%d -> ", i + 1);
            REP(j, g[i].size()) {
                printf("%d ", g[i][j] + 1);
            }
            printf("\n");
        }
    }

    void calculateDist(vector<VI > &g, VI &dist, int s, const set<int> &deleted) {
        if(deleted.find(s) != deleted.end()) return;
        if(g[s].empty()) {
            dist[s] = 0;
        } else {
            int res = -1;
            REP(i, g[s].size()) {
                if(dist[g[s][i]] < 0) calculateDist(g, dist, g[s][i], deleted);
                if(dist[g[s][i]] > res) res = dist[g[s][i]];
            }
            dist[s] = res + 1;
        }
    }     

    void addCandidates(vector<VI > &g, VI &dist, set<int> &cand, int i) {
        cand.insert(i);
        while(!g[i].empty() && dist[i] > 0) {
            int j = 0;
            while(dist[i] != 1 + dist[g[i][j]]) j++;
            cand.insert(g[i][j]);
            i = g[i][j];
        }
    }

    Result getResult(const set<int> &deleted) {
        VI dist_from(n, -1);
        VI dist_to(n, -1);
        REP(i, n) {
            if(dist_from[i] < 0) calculateDist(graph, dist_from, i, deleted);
            if(dist_to[i] < 0) calculateDist(reversed, dist_to, i, deleted);
        }
        int longest = dist_from[0] + dist_to[0];
        int longest_i = 0;
        for(int i = 1; i < n; i++) {
            if(longest < dist_from[i] + dist_to[i]) {
                longest = dist_from[i] + dist_to[i];
                longest_i = i;
            }
        }
        set<int> candidates;
        addCandidates(graph, dist_from, candidates, longest_i);
        addCandidates(reversed, dist_to, candidates, longest_i);
        return Result(longest + 1, candidates);
    }

    int naive() {
        int r = n+1;
        set<int> best;
        for(int i=0;i<n;i++)
            for(int j=i+1;j<n;j++)
                for(int k=j+1;k<n;k++)
                    for(int l=k+1;l<n;l++) {
                        set<int> del;
                        del.insert(i);
                        del.insert(j);
                        del.insert(k);
                        del.insert(l);
                        Result res = getResult(del);
                        if(res.longest_path < r) {
                            r = res.longest_path;
                            best = del;
                        }
                    }
        return r;
    }
                                                                                
    int solve() {
        if(k == n) {
            cout << 0 << '\n';
            return 0;
        }
        Result res = getResult(set<int>());
        Result res2(n+1, set<int>()), res3(n+1, set<int>()), res4(n+1, set<int>()), finalRes(n+1, set<int>());
        set<int> del;
        for(int c:res.candidates) {
            del.insert(c);
            Result tmp = getResult(del);
            if(tmp.longest_path < res2.longest_path) {
                res2 = tmp;
            }
            if(k > 1) {
                for(int d:tmp.candidates) {
                    if(d != c) {
                        del.insert(d);
                        Result tmp2 = getResult(del);
                        if(tmp2.longest_path < res3.longest_path) res3 = tmp2;
                        if(k > 2) {
                            for(int e:tmp2.candidates) {
                                if(e != d && e != c) {
                                    del.insert(e);
                                    Result tmp3 = getResult(del);
                                    if(tmp3.longest_path < res4.longest_path) res4 = tmp3;
                                    if(k > 3) {
                                        for(int f:tmp3.candidates) {
                                            if(f != e && f != d && f != c) {
                                                del.insert(f);
                                                Result tmp4 = getResult(del);
                                                if(tmp4.longest_path < finalRes.longest_path) finalRes = tmp4;
                                                del.erase(f);
                                            }
                                        }
                                    }
                                    del.erase(e);
                                }
                            }
                        }
                        del.erase(d);
                    }
                }
            }
            del.erase(c);
        }
        if(k == 1) finalRes = res2;
        else if(k == 2) finalRes = res3;
        else if(k == 3) finalRes = res4;
        cout << finalRes.longest_path << '\n';
        return finalRes.longest_path;
    }   

    void stressTest() {
        bool found = false;
        while(!found) {
            graph.clear();
            reversed.clear();
            n = 10; m = rand() % (n) + n; k = 4;
            graph.resize(n);
            reversed.resize(n);
            set<pair<int,int> > ed;
            while(ed.size() < m) {
                int x = rand() % n;
                int y = rand() % n;
                while (x == y) y = rand() % n;
                ed.insert(make_pair(min(x,y), max(x,y)));
            }
            for(pair<int,int> p:ed) {
                graph[p.first].PB(p.second);
                reversed[p.second].PB(p.first);
            }
            int a = solve();
            int b = naive();
            if(a != b) {
                printf("%d %d %d\n", n,m,k);
                printGraph(graph);
                printf("res: %d naive: %d\n", a, b);
                found = true;
            }
        }
    }                                                                        
};                                                                              
                                                                                
int main() {                                                                    
    ios_base::sync_with_stdio(0);                                               
    Solver solver;
    //solver.stressTest();                                                      
    solver.readInput();                                                         
    solver.solve();                                                             
    return 0;                                                                   
}