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

class Vertex;

int minRoad = 401;

static bool roadComparator(std::vector<int> a, std::vector<int> b) {
    return (a.size() < b.size());
}

std::vector<std::vector<Vertex *> > removedVerticesCache;

class Vertex {
public:
    int index;
    std::vector<int> incomingNeighbors;
    std::vector<int> outgoingNeighbors;
    std::vector<int> longestRoadVertices;

    Vertex(int index) {
        this->index = index;
    }

    void cleanUpReferencesToVertex(Vertex *vertex) {
        this->longestRoadVertices.clear();
        if (this->index == vertex->index) {
            this->incomingNeighbors.clear();
            this->outgoingNeighbors.clear();
        } else {
            std::vector<int>::iterator incomingVertex = std::find(this->incomingNeighbors.begin(), this->incomingNeighbors.end(), vertex->index);
            if (incomingVertex != this->incomingNeighbors.end()) {
                this->incomingNeighbors.erase(incomingVertex);
            } else {
                auto outgoingVertex = std::find(this->outgoingNeighbors.begin(), this->outgoingNeighbors.end(), vertex->index);
                if (outgoingVertex != this->outgoingNeighbors.end()) {
                    this->outgoingNeighbors.erase(outgoingVertex);
                }
            }
        }
    }

    std::vector<int> calculateLongestRoadForVertex(std::vector<Vertex *> vertices) {
        if (this->outgoingNeighbors.size() == 0) {
            return std::vector<int>(1, this->index);
        }
        if (this->longestRoadVertices.size() > 0) {
            return this->longestRoadVertices;
        }
        std::vector<std::vector<int> > neighborLongestRoads;
        for (auto neighborVertex : this->outgoingNeighbors) {
            neighborLongestRoads.push_back(vertices.at(neighborVertex - 1)->calculateLongestRoadForVertex(vertices));
        }
        auto longestNeighborRoadIt = *std::max_element(neighborLongestRoads.begin(), neighborLongestRoads.end(),
                                                       roadComparator);
        int actualRoad = longestNeighborRoadIt.size();
        std::vector<int> longestRoadVertices(longestNeighborRoadIt.begin(), longestNeighborRoadIt.end());
        longestRoadVertices.push_back(this->index);
        this->longestRoadVertices = longestRoadVertices;
        return longestRoadVertices;
    }
};

std::vector<int> getLongestRoadInGraph(std::vector<Vertex *> vertices, int verticesToRemove) {
    int longestRoadSoFar = 0;
    std::vector<int> verticesOfLongestRoad;
    for (auto vertex : vertices) {
        if (vertex->incomingNeighbors.size() == 0) {
            std::vector<int> longestRoadForVertex = vertex->calculateLongestRoadForVertex(vertices);
            if (verticesToRemove == 0 && longestRoadForVertex.size() > minRoad) {
                return longestRoadForVertex;
            }
            if (longestRoadForVertex.size() > longestRoadSoFar) {
                longestRoadSoFar = longestRoadForVertex.size();
                verticesOfLongestRoad = longestRoadForVertex;
            }
        }
    }
    return verticesOfLongestRoad;
}

std::vector<Vertex *> getCopyOfGraph(std::vector<Vertex *> original) {
    std::vector<Vertex *> copy;
    for (auto vertex : original) {
        auto copiedVertex = new Vertex(vertex->index);
        copiedVertex->outgoingNeighbors = std::vector<int>(vertex->outgoingNeighbors.begin(),
                                                           vertex->outgoingNeighbors.end());
        copiedVertex->incomingNeighbors = std::vector<int>(vertex->incomingNeighbors.begin(),
                                                           vertex->incomingNeighbors.end());
        copy.push_back(copiedVertex);
    }

    return copy;
}

std::vector<Vertex *> getGraphWithoutOneVertex(int index, std::vector<Vertex *> original) {
    std::vector<Vertex *> vertices = getCopyOfGraph(original);
    for (auto vertex : vertices) {
        vertex->cleanUpReferencesToVertex(vertices.at(index - 1));
    }
    return vertices;
}

void calculateAllRoadsWithoutVertices(std::vector<Vertex *> vertices, int verticesToRemove,
                                      std::vector<int> removedVertices) {
//    std::sort (removedVertices.begin(), removedVertices.end());
//    if (verticesToRemove == 0 && isResultCached(removedVertices)) {
//        return;
//    }
    auto longestRoad = getLongestRoadInGraph(vertices, verticesToRemove);
//    printf("ON LEVEL %d FOUND LONGEST ROAD %d\n", verticesToRemove, longestRoad.size());
    if (verticesToRemove > 0) {
        for (auto index : longestRoad) {
            auto graph = getGraphWithoutOneVertex(index, vertices);
            std::vector<int> updatedRemoved(removedVertices.begin(), removedVertices.end());
            updatedRemoved.push_back(index);
            calculateAllRoadsWithoutVertices(graph, verticesToRemove - 1, updatedRemoved);
        }
    } else {
        if (longestRoad.size() < minRoad) {
            minRoad = longestRoad.size();
        }
    }
}

int main() {
    int numberOfVertices;
    int verticesToRemove;
    int numberOfEdges;
    scanf("%d %d %d", &numberOfVertices, &numberOfEdges, &verticesToRemove);

    int counter = 1;
    std::vector<Vertex *> vertices;
    std::vector<int> timesVertexWasInLongestPath(numberOfVertices, 0);
    while (counter <= numberOfVertices) {
        vertices.push_back(new Vertex(counter));
        counter++;
    }

    counter = 0;
    while (counter < numberOfEdges) {
        int firstEdge;
        int secondEdge;
        scanf("%d %d", &firstEdge, &secondEdge);
        vertices.at(firstEdge - 1)->outgoingNeighbors.push_back(secondEdge);
        vertices.at(secondEdge - 1)->incomingNeighbors.push_back(firstEdge);
        counter++;
    }
    if (verticesToRemove == numberOfVertices) {
        printf("%d\n", 0);
        return 0;
    }
    std::vector<int> removedVertices;
    calculateAllRoadsWithoutVertices(vertices, verticesToRemove, removedVertices);

    printf("%d\n", minRoad);
    return 0;
}