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
#include <cstdio>
#include <vector>
using namespace std;

const int maxN = 500000;

int state[maxN + 1], banned_vertex;
bool found_cycle, res[maxN + 1];

vector <int> G[maxN + 1];

void dfs(int v)
{
    if (found_cycle)
        return;

    state[v] = 1;

    for (int i=0; i<G[v].size(); i++)
    {
        int u = G[v][i];

        if ((u == banned_vertex) or (state[u] == 2))
            continue;

        if (state[u] == 1)
            found_cycle = 1;

        else
            dfs(u);
    }

    state[v] = 2;
}

int main()
{
    int n, m;
    scanf ("%d%d", &n, &m);

    while (m--)
    {
        int a, b;
        scanf ("%d%d", &a, &b);

        G[a].push_back(b);
    }

    for (banned_vertex = 0; banned_vertex <= n; banned_vertex++)
    {
        for (int i=1; i<=n; i++)
        {
            if ((i == banned_vertex) or (state[i] == 2))
                continue;

            dfs(i);

            if (found_cycle)
                break;
        }

        res[banned_vertex] = !found_cycle;

        found_cycle = 0;

        fill(state, state + n + 1, 0);
    }

    if (res[0])
    {
        printf("NIE\n");
        return 0;
    }

    int res_amount = 0;

    for (int i=1; i<=n; i++)
        res_amount += res[i];

    printf("%d\n", res_amount);

    for (int i=1; i<=n; i++)
    {
        if (res[i])
            printf("%d ", i);
    }

    return 0;
}