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
#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>
#include <set>
#include <map>

class Mistrzostwa {
public:
  Mistrzostwa(int cities) {
    initializeAdjacencyList(cities);
    initializeComponents(cities);
  }

  void addRoad(int cityA, int cityB) {
    adjacencyList[cityA].insert(cityB);
    adjacencyList[cityB].insert(cityA);
  }

  void removeVertex(int city) {
    std::set<int>& neighborsOfCity = adjacencyList[city];
    for (std::set<int>::iterator iter = neighborsOfCity.begin(); iter != neighborsOfCity.end(); ++iter) {
      const int removedCities = adjacencyList[*iter].erase(city);
      assert(removedCities == 1);
    }
    
    neighborsOfCity.clear();
  }

  void removeVerticesWithIncidenceBelowThreshold(unsigned threshold) {
    bool restart = false;
    for (int i = 0; i < adjacencyList.size(); ++i) {
      if (adjacencyList[i].size() < threshold) {
        const int numberOfNeighbors = adjacencyList[i].size();
        removeVertex(i);
        if (numberOfNeighbors > 0) {
          restart = true;
          break;
        }
      }
    }
    if (restart)
      removeVerticesWithIncidenceBelowThreshold(threshold);
  }

  void determineSSC() {
    int currentComponentId = 0;
    for (int i = 1; i < adjacencyList.size(); ++i) {
      const bool modified = traverseNeighbors(i, currentComponentId);
      if (modified)
        ++currentComponentId;
    }
  }

  void printBiggestComponent() {
    std::map<int, std::vector<int> > componentToCities;

    for (int i = 1; i < adjacencyList.size(); ++i) {
      if (components[i] != -1)
        componentToCities[components[i]].push_back(i);
    }

    int componentNo = -1;
    int componentSize = 0;
    for (std::map<int, std::vector<int> >::const_iterator iter = componentToCities.begin(); iter != componentToCities.end(); ++iter) {
      if (iter->second.size() > componentSize) {
        componentNo = iter->first;
        componentSize = iter->second.size();
      }
    }

    std::cout << componentSize << std::endl;
    std::vector<int>& biggestComponent = componentToCities.at(componentNo);
    std::sort(biggestComponent.begin(), biggestComponent.end());
    for (int i = 0; i < biggestComponent.size(); ++i) {
      std::cout << biggestComponent[i];
      if (i != (biggestComponent.size()-1))
        std::cout << " ";
      else
        std::cout << std::endl;
    }
  }

  bool isSolvable(int d) {
    bool result = false;
    for (int i = 1; i < adjacencyList.size(); ++i) {
      result |= (adjacencyList[i].size() >= d);
    }

    return result;
  }

  void print() {
    for (int i = 0; i < adjacencyList.size(); ++i) {
      std::cout << i << ": ";
      for (std::set<int>::const_iterator iter = adjacencyList[i].begin(); iter != adjacencyList[i].end(); ++iter) {
        std::cout << *iter << "(" << components[*iter] << ")" << ",";
      }
      std::cout << std::endl;
    }
  }

private:
  void initializeAdjacencyList(int cities) {
    for (int i = 0; i < (cities + 1); ++i) {
      adjacencyList.push_back(std::set<int>());
    }
  }

  void initializeComponents(int cities) {
    for (int i = 0; i < (cities + 1); ++i) {
      components.push_back(-1);
    }
  }

  bool traverseNeighbors(int cityId, int componentId) {
    if (components[cityId] != -1)
      return false;

    components[cityId] = componentId;
    const std::set<int>& neighbors = adjacencyList[cityId];
    for (std::set<int>::const_iterator iter = neighbors.begin(); iter != neighbors.end(); ++iter) {
      traverseNeighbors(*iter, componentId);
    }

    return true;
  }

  std::vector<std::set<int> > adjacencyList;
  std::vector<int> components;
};

int main(void) {
  std::cin.sync_with_stdio(false);
  std::cout.sync_with_stdio(false);

  int cities;
  int roads;
  int d;

  std::cin >> cities >> roads >> d;
  Mistrzostwa m(cities);

  for (int i = 0; i < roads; ++i) {
    int cityA;
    int cityB;
    std::cin >> cityA >> cityB;

    m.addRoad(cityA, cityB);
  }

  //m.print();
  m.removeVerticesWithIncidenceBelowThreshold(d);

  if (!m.isSolvable(d)) {
    std::cout << "NIE" << std::endl;
    return 0;
  }

  m.determineSSC();
  //m.print();

  m.printBiggestComponent();

  return 0;
}