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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Łukasz Proksa, Silesian Univeristy of Technology, Poland

// Potyczki Algorytmiczne 2015
// Task "Mistrzostwa"
// Solved! O((n+m)*log_n) ~ O(7.6 mln)

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;

typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VLL;
typedef pair<int, int> PII;
typedef vector<PII> VPII;

#define LET(k, val) __typeof(val) k = (val)
#define FOR(i, b, e) for(LET(i,b); i <= (e); ++i) 
#define FORD(i, b, e) for(LET(i,b); i >= (e); --i)
#define REP(i, n) for(int i = 0; i < (n); ++i)
#define SIZE(c) ((int)(c).size())
#define ALL(c) (c).begin(), (c).end()
#define FOREACH(i, c) for(LET(i,(c).begin()); i != (c).end(); ++i)
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define ST first
#define ND second
#define PB push_back
#define MP make_pair
#define COUT(c) {cout<<SIZE(c)<<":{";FOREACH(i,c){cout<<*i<<",";}cout<<"}";} 

template <class V, class E> struct Graph;

template <class T, class U>
ostream& operator<<(ostream& os, const pair<T,U>& p)
{
  return os<<"("<<p.ST<<","<<p.ND<<")";
}

template<class V, class E>
ostream& operator<<(ostream& os, const Graph<V,E>& g)
{
  cout<<"graph: "<<SIZE(g.g)<<endl;
  REP(i, SIZE(g.g)) {
    cout<<" "<<i<<":";
    FOREACH(it, g.g[i]) {
      cout<<" "<<it->v;
    }
    cout<<endl;
  }
  return os;
}

static const int    INF = 1e9 + 1; // mld + 1
static const double EPS = 1e-9;

static const int    INF_C = 200001; // 200 tys +1

template<class T>
T min_pow_2(T x)
{
  T tmp = 1;
  while (tmp < x) {
    tmp *= 2;
  }
  return tmp;
}

template <class V, class E>
struct Graph
{
  struct Ed : E
  {
    int v;
    Ed(E e, int vv) : E(e), v(vv)
    {
    };
  };
  struct Ve : V, vector<Ed>
  {
  };

  vector<Ve> g;
  vector<VI> trees;
  int d;
  static Graph<V, E>* ice;

  Graph(int n = 0) : g(n)
  {
  }
  void edge_u(int a, int b, E e = E())
  {
    Ed ed(e, b);
    ed.rev = SIZE(g[b]) + (a == b);
    g[a].PB(ed);
    ed.v = a;
    ed.rev = SIZE(g[a]) - 1;
    g[b].PB(ed);
  }
  int min_c(int a, int b) // for interval tree
  {
    return (g[a].c < g[b].c ? a : b);
  }
  VI vis; // empty for every root
  int size_2;
  void init_tree() 
  {
    // make interval tree, min
    int size = SIZE(vis);
    size_2 = min_pow_2(size);
    vis.resize(2*size_2, -1); 
    REP(i, size) { // rewrite el
      vis[size_2 + i] = vis[i];
      vis[i] = -1; 
    }
    int pos = size_2 / 2;
    while (pos > 0) { // pos is first el in level
      REP(i, pos) { // level has 'pos' el
        int l = 2*(pos+i);
        int r = l + 1;
        if (vis[r] != -1) { // only right can be -1
          vis[pos+i] = min_c(vis[l], vis[r]);
        } else {
          vis[pos+i] = vis[l]; // maybe -1
        }
      }
      pos /= 2;
    }
  }
  void up_tree(int i)
  {
    int pos = (size_2 + i) / 2;
    while (pos > 0) {
      int l = 2*pos;
      int r = l + 1;
      if (vis[r] != -1) { // only right can be -1
        vis[pos] = min_c(vis[l], vis[r]);
      } else {
        vis[pos] = vis[l]; // maybe -1
      }
      pos /= 2;
    }
  }
  void get_tree(VI& tree)
  {
    int v, pos = size_2;
    int i = 0;
    int last = 2*size_2;
    while ((pos < last) && (vis[pos] != -1)) {
      int v = vis[pos];
      if (g[v].c < INF_C) {
        tree.PB(v);
      }
      pos++;
    }
  }
  VI dfs()
  {
    FOREACH(it, g) {
      it->c = 0;
      it->tr = -1;
    }
    int tr_cnt = 0;
    VI max;
    REP(i, SIZE(g)) { 
      if (g[i].tr == -1 && is_c(i)) {
        vis.resize(0);
        dfs_r(i, vis, tr_cnt++);
        int c_cnt = SIZE(vis);
        init_tree();
        // remove vertexes with g[].c < d
        int v;
        while (g[v = vis[1]].c < d) { // if exists vertex 'v' that can't be in S
          FOREACH(it, g[v]) { // g[].c-- for neightbours
            int u = it->v;
            if (g[u].tr != -1) { // candidate going to be checked
              g[u].c--;
              up_tree(g[u].vis_rev);
            }
          }
          // remove 'v', update tree(v)
          g[v].tr = -1;
          g[v].c = INF_C; 
          up_tree(g[v].vis_rev);
          c_cnt--;
        }
        if (c_cnt > SIZE(max)) { // at least 1 vertex has g[].c != INF
          max.resize(0);
          get_tree(max);
        }
      }
    }
    return max;
  }
  void dfs_r(int v, VI& vis, int tr)
  {
    g[v].tr = tr;
    vis.PB(v);
    g[v].vis_rev = SIZE(vis) - 1;
    FOREACH(it, g[v]) { // for each neighbour 'u'
      int u = it->v;
      if (is_c(u)) {
        g[v].c++;
        if (g[u].tr == -1) {
          dfs_r(u, vis, tr);
        }
      }
    }
  }
  bool is_c(int v)
  {
    return SIZE(g[v]) >= d;
  }
};

// template<>
// Graph<V, Empty>* Graph<V, Empty>::ice = NULL;

struct Empty {};

struct V
{
  int c, tr;
  int vis_rev;
};

struct E
{
  int rev;
};

bool cmp(int a, int b)
{
  return a < b;
}

int main()
{
  ios_base::sync_with_stdio(false);
  int n, m, d;
  cin>>n>>m>>d;
  Graph<V, E> g(n);
  g.d = d;
  REP(i, m) {
    int a, b;
    cin>>a>>b; a--; b--;
    g.edge_u(a, b);
  }
  VI result = g.dfs();
  // dfs(vertex), wypisz wierzcholki nalezace do rdzrzewa rosnaca
  if (SIZE(result) == 0) {
    cout<<"NIE"<<endl;
  } else {
    sort(ALL(result), cmp);
    cout<<SIZE(result)<<endl; // = result.ST
    REP(i, SIZE(result)) {
      cout<<result[i]+1<<" ";
    }
    cout<<endl;
  }
  
  return 0;
}