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

using namespace std;

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

#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
#define MP make_pair

#define INF 1000000001


/**
 * Reprezentacja grafu oraz algorytmy grafowe na podstawie książki:
 * Piotr Stańczyk
 * "Algorytmika praktyczna Nie tylko dla mistrzów"
 * PWN 2009
 */
template<class V, class E> struct Graph {

    // Represents Edges
    struct Ed : E {
        int v;
        Ed(E p, int w) : E(p), v(w) { }
    };

    // Represents Vertices
    struct Ve : V, vector<Ed> { };

    // vertices in graph
    vector<Ve> g;

    int d;  // Paramert d z zadania

    // n - number of vertices in graph
    Graph(int dd, int n = 0) : g(n), d(dd) { }

    void EdgeD(int b, int e, E ed = E()) {
        g[b].PB(Ed(ed, e));
    }

    // requires:
    //      int E::rev;
    void EdgeU(int b, int e, E ed = E()) {
        Ed eg(ed, e);

        eg.rev = SIZE(g[e]) + (b == e);
        g[b].PB(eg);

        eg.rev = SIZE(g[eg.v = b]) - 1;
        g[e].PB(eg);
    }

    // time of entrance into vertex in recursive DFS algorithm
    int t;

    // DFS helper function: search sub tree of vertex v
    // requires:
    //      int Graph<V, E>::t
    //      int V::d
    //      int V::f
    //      int V::s
    void Dfs(int v, vector<int>& verts) {
        g[v].d = ++t;
        verts.PB(v);
        FOREACH(it, g[v]) if (g[it->v].d == -1 && !g[it->v].disabled) {
            g[it->v].s = v;
            Dfs(it->v, verts);
        }
        g[v].f = ++t;
    }

    void DisableVerts() {
        vector<int> qu(SIZE(g));
        int b = 0, e = 0;

        REP(i, SIZE(g)) {
            g[i].deg = SIZE(g[i]);
            g[i].disabled = false;
            if (g[i].deg < d) {
                qu[e++] = i;
                g[i].disabled = true;
            }
        }

        while (b < e) {
            int i = qu[b++];
            for (auto& ed : g[i]) {
                --(g[ed.v].deg);
                if (g[ed.v].deg < d && !g[ed.v].disabled) {
                    qu[e++] = ed.v;
                    g[ed.v].disabled = true;
                }
            }
        }
    }

    vector<int> FindBest() {
        t = -1;
        vector<int> ret, tmp;

        for (auto& v : g) v.d = v.f = v.s = -1;
        REP(x, SIZE(g)) if (g[x].s == -1 && !g[x].disabled) {
            tmp.clear();
            Dfs(x, tmp);
            if (tmp.size() > ret.size()) swap(tmp, ret);
        }

        return ret;
    }
};

struct Edge {
    int rev;
};

struct Vert {
    int d, f, s;

    int cc;
    int deg;
    bool disabled;
};

int main()
{
    ios_base::sync_with_stdio(0);
    int n, m, d, a, b;
    cin >> n >> m >> d;

    Graph<Vert, Edge> g(d, n);
    REP(i, m) {
        cin >> a >> b;
        g.EdgeU(--a, --b);
    }

    g.DisableVerts();
    vector<int> verts = g.FindBest();
    if (verts.empty()) {
        cout << "NIE\n";
    } else {
        sort(ALL(verts));
        cout << verts.size() << "\n";
        int i = 0;
        for (auto v : verts) {
            if (i > 0) cout << " ";
            cout << (v + 1);
            ++i;
        }
        cout << "\n";
    }

    return 0;
}