답안 #1035202

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1035202 2024-07-26T06:27:28 Z vjudge1 열대 식물원 (Tropical Garden) (IOI11_garden) C
100 / 100
1822 ms 8028 KB
#include "garden.h"
#include "gardenlib.h"
#include <stdbool.h>
#include <assert.h>

void floyds(const int *G, int u, int v, /*out*/ int *cyc, /*out*/ int *rem) {
  assert(rem[u] == 0 && u != v);

  bool seen = false;
  int d = 0;
  int slo = u, fas = u;
  do {
    slo = G[slo];
    fas = G[G[fas]];
    if (!seen) d++;
    if (rem[slo] != 0) {
      if (seen) {
        for (; u != G[v]; u = G[u])
          rem[u] = d--;
        assert(d == -1);
      }
      for (; u != slo; u = G[u]) {
        if (rem[slo] == -1)
          rem[u] = -1;
        else
          rem[u] = rem[slo] + d--;
      }
      assert(seen || rem[slo] == -1 || d == 0);
      return;
    }
    rem[slo] = -1;
    seen = seen || slo == v;
  } while (slo != fas);

  rem[u] = -1;

  int c = 0;
  bool in_cyc = false;
  do {
    slo = G[slo];
    rem[slo] = -1;
    seen = seen || slo == v;
    in_cyc = in_cyc || slo == v;
    c++;
  } while (slo != fas);

  if (in_cyc) {
    *cyc = c;
    slo = G[v];
    for (int i = 1; i <= c; i++) {
      rem[slo] = c - i;
      slo = G[slo];
    }
    assert(slo == G[v]);
  }

  if (seen) {
    // find start of cycle and distance to start of cycle
    d = 0;
    slo = u;
    for (; slo != fas; slo = G[slo], fas = G[fas])
      d++;

    for (; u != slo; u = G[u])
      rem[u] = rem[slo] + d--;
    assert(d == 0);
  }
}

void count_routes(int N, int M, int P, int R[][2], int Q, int G[]) {
  static int most_beautiful[150001][2];
  static int graph[300001];
  static int rem[2][300001];
  int cyc[2];
  cyc[0] = cyc[1] = 1e9 + 1;
  // 1 indexing, graph[u] == 0, means no edges connected to it
  P++;

  // save 2 most_beautiful edges
  for (int i = 0; i < M; i++) {
    for (int j = 0; j < 2; j++) {
      int u = R[i][j] + 1;
      int v = R[i][j^1] + 1;
      if (most_beautiful[u][0] == 0) {
        most_beautiful[u][0] = v;
      } else if (most_beautiful[u][1] == 0) {
        most_beautiful[u][1] = v;
      }
    }
  }

  // build functional graph
  for (int u = 1; u <= N; u++) {
    for (int j = 0; j < 2; j++) {
      int v = most_beautiful[u][j];
      if (most_beautiful[v][0] != u || most_beautiful[v][1] == 0)
        graph[u + j*N] = v;
      else
        graph[u + j*N] = v + N;
    }
  }

  for (int i = 0; i < 2; i++)
    for (int j = 1; j <= 2 * N; j++)
      if (rem[i][j] == 0 && j != P + i*N)
        floyds(graph, j, P + i*N, cyc + i, rem[i]);

  for (int i = 0; i < Q; i++) {
    int res = 0;
    for (int j = 0; j < 2; j++) {
      for (int k = 1; k <= N; k++) {
        if (rem[j][k] == -1) continue;
        res += rem[j][k] <= G[i] && (G[i] - rem[j][k]) % cyc[j] == 0;
      }
    }
    answer(res);
  }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 2 ms 520 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 2 ms 520 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 4 ms 1460 KB Output is correct
12 Correct 9 ms 2396 KB Output is correct
13 Correct 14 ms 4644 KB Output is correct
14 Correct 27 ms 7388 KB Output is correct
15 Correct 27 ms 7524 KB Output is correct
16 Correct 24 ms 5816 KB Output is correct
17 Correct 21 ms 5204 KB Output is correct
18 Correct 11 ms 2432 KB Output is correct
19 Correct 26 ms 7260 KB Output is correct
20 Correct 27 ms 7504 KB Output is correct
21 Correct 27 ms 5720 KB Output is correct
22 Correct 27 ms 5208 KB Output is correct
23 Correct 26 ms 8004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 2 ms 520 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 4 ms 1460 KB Output is correct
12 Correct 9 ms 2396 KB Output is correct
13 Correct 14 ms 4644 KB Output is correct
14 Correct 27 ms 7388 KB Output is correct
15 Correct 27 ms 7524 KB Output is correct
16 Correct 24 ms 5816 KB Output is correct
17 Correct 21 ms 5204 KB Output is correct
18 Correct 11 ms 2432 KB Output is correct
19 Correct 26 ms 7260 KB Output is correct
20 Correct 27 ms 7504 KB Output is correct
21 Correct 27 ms 5720 KB Output is correct
22 Correct 27 ms 5208 KB Output is correct
23 Correct 26 ms 8004 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 71 ms 1372 KB Output is correct
26 Correct 115 ms 2396 KB Output is correct
27 Correct 669 ms 4768 KB Output is correct
28 Correct 677 ms 7248 KB Output is correct
29 Correct 793 ms 7620 KB Output is correct
30 Correct 491 ms 5724 KB Output is correct
31 Correct 442 ms 5464 KB Output is correct
32 Correct 123 ms 2396 KB Output is correct
33 Correct 660 ms 7364 KB Output is correct
34 Correct 717 ms 7624 KB Output is correct
35 Correct 942 ms 5720 KB Output is correct
36 Correct 790 ms 5468 KB Output is correct
37 Correct 593 ms 8028 KB Output is correct
38 Correct 1822 ms 7912 KB Output is correct