답안 #1035046

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1035046 2024-07-26T03:29:15 Z vjudge1 열대 식물원 (Tropical Garden) (IOI11_garden) C
100 / 100
1705 ms 9320 KB
#include "garden.h"
#include "gardenlib.h"
#include <stdbool.h>
#include <string.h>
#include <assert.h>

int dsu_find(int *par, int a) {
  return par[a] < 0 ? a : (par[a] = dsu_find(par, par[a]));
}

void dsu_union(int *par, int a, int b) {
  a = dsu_find(par, a);
  b = dsu_find(par, b);
  assert(a != b); // we should only call dsu_union if they belong to different components
  if (-par[b] > -par[a]) {
    int tmp = a;
    a = b;
    b = tmp;
  }
  par[a] += par[b];
  par[b] = a;
}

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 dsu[300001];
  static int rem[2][300001];
  int cyc[2];
  cyc[0] = cyc[1] = 1e9 + 1;
  memset(dsu, -1, sizeof dsu[0] * (2 * (size_t)N + 1));
  assert(dsu[2 * N] == -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;
    }
  }

  // set everything in cycle
  for (int u = 1; u <= 2 * N; u++) {
    if (graph[u] == 0) continue;
    if (dsu_find(dsu, u) != dsu_find(dsu, graph[u])) {
      dsu_union(dsu, u, graph[u]);
      continue;
    }
    bool seen[2] = {false};
    int c = 0;
    int v = u;
    do {
      v = graph[v];
      seen[0] = seen[0] || v == P;
      seen[1] = seen[1] || v == P + N;
      rem[0][v] = rem[1][v] = -1;
      c++;
    } while (v != u);

    for (int i = 0; i < 2; i++) {
      if (!seen[i]) continue;
      cyc[i] = c;
      v = graph[P + i * N];
      for (int j = 1; j <= c; j++) {
        rem[i][v] = c - j;
        v = graph[v];
      }
      assert(v == graph[P + i * N]);
    }
  }
  
  // set everything out of cycle
  for (int i = 0; i < 2; i++) {
    int target = P + i * N;
    for (int u = 1; u <= 2 * N; u++) {
      if (rem[i][u] != 0 || u == target) continue;

      int v = u;
      int d = 0;
      do {
        rem[i][v] = -1;
        d++;
        v = graph[v];
      } while (rem[i][v] == 0 && v != target);

      if (v == target) {
        assert(rem[i][target] == -1 || rem[i][target] == 0);
        for (v = u; v != graph[target]; v = graph[v])
          rem[i][v] = d--;
        assert(d == -1);
        assert(rem[i][target] == 0);
      } else if (rem[i][v] > 0) {
        for (int w = u; w != v; w = graph[w])
          rem[i][w] = rem[i][v] + d--;
        assert(d == 0);
      }
    }
  }
  
  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 0 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 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 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 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 3 ms 1696 KB Output is correct
12 Correct 9 ms 2868 KB Output is correct
13 Correct 13 ms 5212 KB Output is correct
14 Correct 27 ms 8284 KB Output is correct
15 Correct 30 ms 8468 KB Output is correct
16 Correct 24 ms 6480 KB Output is correct
17 Correct 22 ms 5980 KB Output is correct
18 Correct 9 ms 2872 KB Output is correct
19 Correct 26 ms 8284 KB Output is correct
20 Correct 27 ms 8656 KB Output is correct
21 Correct 25 ms 6480 KB Output is correct
22 Correct 27 ms 5892 KB Output is correct
23 Correct 26 ms 9244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 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 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 3 ms 1696 KB Output is correct
12 Correct 9 ms 2868 KB Output is correct
13 Correct 13 ms 5212 KB Output is correct
14 Correct 27 ms 8284 KB Output is correct
15 Correct 30 ms 8468 KB Output is correct
16 Correct 24 ms 6480 KB Output is correct
17 Correct 22 ms 5980 KB Output is correct
18 Correct 9 ms 2872 KB Output is correct
19 Correct 26 ms 8284 KB Output is correct
20 Correct 27 ms 8656 KB Output is correct
21 Correct 25 ms 6480 KB Output is correct
22 Correct 27 ms 5892 KB Output is correct
23 Correct 26 ms 9244 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 71 ms 1628 KB Output is correct
26 Correct 120 ms 2744 KB Output is correct
27 Correct 581 ms 5208 KB Output is correct
28 Correct 619 ms 8276 KB Output is correct
29 Correct 736 ms 8532 KB Output is correct
30 Correct 485 ms 6744 KB Output is correct
31 Correct 430 ms 5920 KB Output is correct
32 Correct 136 ms 2908 KB Output is correct
33 Correct 602 ms 8400 KB Output is correct
34 Correct 701 ms 8528 KB Output is correct
35 Correct 465 ms 6480 KB Output is correct
36 Correct 723 ms 6080 KB Output is correct
37 Correct 499 ms 9052 KB Output is correct
38 Correct 1705 ms 9320 KB Output is correct