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
217
218
219
//
//  her.cpp
// Heros

#include<iostream>
#include<stack>
#include<vector>
#include<algorithm>
#define INF -1

using namespace std;

void topoSort(int u, bool visited[], stack<int>&stk, short **nodes, int n) {
    visited[u] = true;    //set as the node v is visited
    
    for(int v = 0; v<n; v++) {
        if(nodes[u][v]) {    //for allvertices v adjacent to u
            if(!visited[v])
                topoSort(v, visited, stk, nodes, n);
        }
    }
    
    stk.push(u);    //push starting vertex into the stack
}

size_t getLongestPath(short **nodes, int n, int *dist, bool *vis, bool *startingNodes){
    
    stack<int> stk;
    
    for(int i = 0; i<n;i++){
        startingNodes[i] = true;
    }
    
    for (int i = 0; i<n; i++){
        if(nodes[i][i] == INF){
            startingNodes[i] = false;
        }else{
            for(int j = 0; j<n; j++){
                if(nodes[j][i] > 0){
                    startingNodes[i] = false;
                    break;
                }
            }
        }
    }
    
    int max = 0;
    
    for(int startingNode = 0; startingNode < n; startingNode++){
        
        if(startingNodes[startingNode]){
            
            //cout << "Starting node " << startingNode + 1 << endl;
            
            for(int i = 0; i<n;i++){
                vis[i] = false;    // make all nodes as unvisited at first
            }
            
            for(int i = 0; i<n; i++)    //perform topological sort for vertices
                if(!vis[i])
                    topoSort(i, vis, stk, nodes, n);
            
            for(int i = 0; i<n; i++)
                dist[i] = INF;    //initially all distances are infinity
            dist[startingNode] = 0;    //distance for start vertex is 0
            
            while(!stk.empty()) {    //when stack contains element, process in topological order
                int nextVert = stk.top(); stk.pop();
                
                if(dist[nextVert] != INF) {
                    for(int v = 0; v<n; v++) {
                        if(nodes[nextVert][v] && nodes[nextVert][v] != INF) {
                            if(dist[v] < dist[nextVert] + nodes[nextVert][v])
                                dist[v] = dist[nextVert] + nodes[nextVert][v];
                        }
                    }
                }
            }
            int nodeMax = 0;
            for(int i = 0; i<n; i++){
                if(max < dist[i]){
                    max = dist[i] + 1;
                }
                if(nodeMax < dist[i]){
                    nodeMax = dist[i] + 1;
                }
            }
            
            //cout << "Node max " << nodeMax << endl;
        }
    }
    
    //cout << "Total max " << max << endl;
    
    return max;
}

void printNodes(int n, short ** nodes){
//    for(short i = 0; i < n; i++){
//        for(short j = 0; j < n; j++){
//            cout << nodes[i][j] << ' ';
//        }
//
//        cout << endl;
//    }
//    cout << endl;
}

size_t getLongestPathAfterKNodesRemoved(short **nodes, int n, int k){
    
    size_t min = 1000000000, currentValue {0};
    
    int *dist = new int[n];
    bool *vis = new bool[n];
    bool *startingNodes = new bool[n];
    
    short **nodesCopy {nullptr};
    nodesCopy = new short* [n];
    for(short i = 0; i < n; i++){
        nodesCopy[i] = new short[n];
    }
    
    //kombinacje 4 z 300 bez powtorzen
    std::string bitmask(k, 1); // K leading 1's
    bitmask.resize(n, 0); // N-K trailing 0's
    
    int nodesToRemove[4];
    int nodesToRemoveCounter = 0;
    
    do {
        
        nodesToRemoveCounter = 0;
        
        for (int i = 0; i < n; ++i) // [0..N-1] integers
        {
            if (bitmask[i]) {
                nodesToRemove[nodesToRemoveCounter++] = i;
            }
        }
        
        //skopiuj nodes
        for(short i = 0; i < n; i++){
            for(short j = 0; j < n; j++){
                nodesCopy[i][j] = nodes[i][j];
            }
        }
        
        
        for(short a = 0; a < k; a++){
            
            //cout << nodesToRemove[a] << ' ';
            
            for(short i = 0; i < n; i++){
                nodesCopy[i][nodesToRemove[a]] = INF;
                nodesCopy[nodesToRemove[a]][i] = INF;
            }
            
            //cout << endl;
        }
        
        //cout << endl;
        
        printNodes(n, nodesCopy);
        
        currentValue = getLongestPath(nodesCopy, n, dist, vis, startingNodes);
        
        if(currentValue < min){
            min = currentValue;
        }
        
    } while (std::prev_permutation(bitmask.begin(), bitmask.end()));
    
    for(short i = 0; i < n; i++){
        delete [] nodesCopy[i];
    }
    
    delete [] nodesCopy;
    delete [] dist;
    delete [] vis;
    delete [] startingNodes;

    return min;
}

int main(int argc, const char * argv[]) {
    
    short **nodes {nullptr};
    short n,m,k;
    
    cin >> n >> m >> k;
    
    nodes = new short* [n];
    for(short i = 0; i < n; i++){
        nodes[i] = new short[n];
        for(short j = 0; j < n; j++){
            nodes[i][j] = INF;
        }
        nodes[i][i] = 0;
    }
    
    printNodes(n, nodes);
    
    short a,b;
    for (short i = 0; i < m; i++){
        cin >> a >> b;
        nodes[a - 1][b - 1] = 1;
    }
    
    printNodes(n, nodes);
    
    cout << getLongestPathAfterKNodesRemoved(nodes, n, k) << endl;
    
    for(short i = 0; i < n; i++){
        delete [] nodes[i];
    }
    delete [] nodes;
    
    return 0;
}