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

#define DEBUG(a)

int n, m;
std::vector<int> graph[500005];

struct {
  std::vector<int> next;
  std::vector<int> prev;
  std::vector<int> weight;

  void Delete(int x) {
    if (0 <= x and x < (int) cycle.size() and is_on_cycle[cycle[x]]) {
      DEBUG(printf("Delete(%d)\n", x));
      is_on_cycle[cycle[x]] = false;
      next[prev[x]] = next[x];
      prev[next[x]] = prev[x];
    } else {
      DEBUG(printf("NoDelete(%d)\n", x));
    }
  }

  void DeleteRange(int a, int b) {
    /*
    for (int i = a; i != b; i = (i + 1) % cycle.size()) {
      Delete(i);
    }
    */
    auto number_of_inversions = [](int a, int b, int c) -> int {
      return (int) (a >= b) + (int) (a >= c) + (int) (b >= c);
    };
    if (a == b) {
      return;
    }
    Delete(a);
    int pa = a;
    a = next[a];
    while (number_of_inversions(pa, a, b) % 2 == 0) {
      Delete(a);
      pa = a;
      a = next[a];
    }
  }

  std::vector<int> cycle;
  std::vector<int> stack;
  bool is_on_cycle[500005];
  bool is_on_stack[500005];
  bool visited[500005];

  int which_on_cycle[500005];

  // Returns true if a cycle was found.
  bool Dfs(int w) {
    stack.push_back(w);
    is_on_stack[w] = true;
    visited[w] = true;
    for (int neighbour : graph[w]) {
      if (is_on_stack[neighbour]) {
        while (cycle.empty() or cycle.back() != neighbour) {
          cycle.push_back(stack.back());
          stack.pop_back();
        }
        std::reverse(cycle.begin(), cycle.end());
        for (int i = 0; i < (int) cycle.size(); i++) {
          is_on_cycle[cycle[i]] = true;
          which_on_cycle[cycle[i]] = i;
        }
        return true;
      } else if (!visited[neighbour]) {
        if (Dfs(neighbour)) {
          return true;
        }
      }
    }
    is_on_stack[w] = false;
    stack.pop_back();
    return false;
  }

  bool FindCyclePrim() {
    for (int i = 1; i <= n; i++) {
      if (!visited[i] and Dfs(i)) {
        return true;
      }
    }
    return false;
  }

  bool FindCycle() {
    if (FindCyclePrim()) {
      const int cycle_size = (int) cycle.size();
      next.resize(cycle_size);
      prev.resize(cycle_size);
      for (int i = 0; i < cycle_size; i++) {
        next[i] = (i + 1) % cycle_size;
        prev[i] = (i - 1 + cycle_size) % cycle_size;
      }
      return true;
    }
    return false;
  }
} FirstCycle;

struct {
  int time = 73;
  int has_farthest_node[500005];
  int farthest_node[500005];
  std::vector<int> result;

  int FindFarthest(int w) {
    if (has_farthest_node[w] == time) {
      return farthest_node[w];
    }
    has_farthest_node[w] = time;
    farthest_node[w] = -1e9;
    if (FirstCycle.is_on_cycle[w]) {
      farthest_node[w] = FirstCycle.weight[FirstCycle.which_on_cycle[w]];
    } else {
      for (int neighbour : graph[w]) {
        farthest_node[w] = std::max(farthest_node[w], FindFarthest(neighbour));
      }
    }
    return farthest_node[w];
  }

  int FindFarthestFromCycle(int w) {
    int farthest = -1e9;
    for (int neighbour : graph[w]) {
      farthest = std::max(farthest, FindFarthest(neighbour));
    }
    return farthest;
  }

  void Minimize(int repeat = 3) {
    const int cycle_length = (int) FirstCycle.cycle.size();
    if (repeat == 0) {
      for (int i = 0; i < cycle_length; i++) {
        if (FirstCycle.is_on_cycle[FirstCycle.cycle[i]]) {
          result.push_back(FirstCycle.cycle[i]);
        }
      }
      return;
    }
    time++;
    for (int i = cycle_length - 1; i >= 0; i--) {
      if (!FirstCycle.is_on_cycle[FirstCycle.cycle[i]]) {
        continue;
      }
      DEBUG(printf("i = %d (aka %d)\n", i, FirstCycle.cycle[i]));
      const int farthest = FindFarthestFromCycle(FirstCycle.cycle[i]);
      const int id = (farthest + cycle_length * 10) % cycle_length;
      DEBUG(printf("farthest = %d, id = %d\n", farthest, id));
      //FirstCycle.DeleteRange((i + 1) % cycle_length, id);
      FirstCycle.DeleteRange(FirstCycle.next[i], id);
      DEBUG(printf("deleted...\n"));
      FirstCycle.weight[i] -= cycle_length;
    }
    Minimize(repeat - 1);
  }

  void Init() {
    FirstCycle.weight.resize(FirstCycle.cycle.size());
    for (int i = 0; i < (int) FirstCycle.weight.size(); i++) {
      FirstCycle.weight[i] = i;
    }
  }
} Minimizing;

struct {
  bool is_on_stack[500005];
  bool visited[500005];

  bool Dfs(int w) {
    if (Excluded(w)) {
      return false;
    }
    visited[w] = true;
    is_on_stack[w] = true;
    for (int neighbour : graph[w]) {
      if (is_on_stack[neighbour] or (!visited[neighbour] and Dfs(neighbour))) {
        return true;
      }
    }
    is_on_stack[w] = false;
    return false;
  }

  bool Excluded(int w) {
    return FirstCycle.is_on_cycle[w];
  }

  bool FindCycle() {
    for (int i = 1; i <= n; i++) {
      if (!visited[i] and Dfs(i)) {
        return true;
      }
    }
    return false;
  }
} Check;

int main() {
  scanf("%d%d", &n, &m);
  while (m--) {
    int a, b;
    scanf("%d%d", &a, &b);
    graph[a].push_back(b);
  }
  if (!FirstCycle.FindCycle()) {
    printf("NIE\n");
    return 0;
  }
  DEBUG(
    printf("CycleLength = %d\n", (int) FirstCycle.cycle.size());
    for (int i : FirstCycle.cycle) {
      printf(" %d", i);
    }
    printf("\n");
  )
  Minimizing.Init();
  Minimizing.Minimize();
  DEBUG(
    printf("Left on cycle:");
    for (int i : FirstCycle.cycle) {
      if (FirstCycle.is_on_cycle[i]) {
        printf(" %d", i);
      }
    }
    printf("\n");
  )
  if (Check.FindCycle()) {
    printf("0\n\n");
  } else {
    printf("%d\n", (int) Minimizing.result.size());
    std::sort(Minimizing.result.begin(), Minimizing.result.end());
    for (int i = 0; i < (int) Minimizing.result.size(); i++) {
      if (i != 0) {
        printf(" ");
      }
      printf("%d", Minimizing.result[i]);
    }
    printf("\n");
  }
  return 0;
}