Submission #494068

# Submission time Handle Problem Language Result Execution time Memory
494068 2021-12-14T07:57:55 Z ntabc05101 Tropical Garden (IOI11_garden) C++14
100 / 100
117 ms 41216 KB
#include<bits/stdc++.h>
#include "garden.h"
#include "gardenlib.h"
using namespace std;

#define taskname ""

/*void answer(int res) {
  cout << res << "\n";
}*/

void count_routes(int N, int M, int P, int R[][2], int Q, int G[]) {
  vector<int> nxt(N << 1, -1);
  for (int i = 0; i < M; i++) {
    bool f[] = {!(~nxt[R[i][0] << 1]), !(~nxt[R[i][1] << 1])};
    for (bool e: {0, 1}) {
      int &u = R[i][e], &v = R[i][e ^ 1];
      if (!(~nxt[u << 1])) {
        nxt[u << 1] = v << 1 | f[e ^ 1];
      }
      else {
        if (!(~nxt[u << 1 | 1])) {
          nxt[u << 1 | 1] = v << 1 | f[e ^ 1];
        }
      }
    }
  }

  for (int i = 0; i < N; i++) {
    if (!(~nxt[i << 1])) {
      continue;
    }
    if (~nxt[i << 1 | 1]) {
      continue;
    }
    nxt[i << 1 | 1] = nxt[i << 1] & ~1;
    if ((nxt[nxt[i << 1 | 1]] >> 1) == i) {
      nxt[i << 1 | 1] |= 1;
    }
  }
  //return ;

  vector<int> adj[N << 1];
  for (int i = 0; i < (N << 1); i++) {
    if (~nxt[i]) {
      adj[nxt[i]].push_back(i);
    }
  }

  auto bfs = [&](int s) -> vector<int> {
    vector<int> dst(N << 1, -1);
    queue<int> q; q.push(s);
    dst[s] = 0;
    while (!q.empty()) {
      auto u = q.front(); q.pop();
      for (auto &to: adj[u]) {
        if (!(~dst[to])) {
          dst[to] = dst[u] + 1;
          q.push(to);
        }
      }
    }
    return dst;
  };

  vector<int> dst[2];
  const int inf = (N << 1) + 10;
  int cyc_len[] = {inf, inf};
  for (bool e: {0, 1}) {
    int u = P << 1 | e;
    dst[e] = bfs(u);
    if (!(~nxt[u])) {
      continue;
    }
    if (!(~dst[e][nxt[u]])) {
      continue;
    }

    cyc_len[e] = dst[e][nxt[u]] - dst[e][u] + 1;
  }
  //return ;

  vector< vector<int> > gr[2];
  for (bool e: {0, 1}) {
    gr[e].resize(N << 1);
    for (int i = 0; i < (N << 1); i += 2) {
      auto &dst_ = dst[e][i];
      if (~dst_) {
        gr[e][dst_ % cyc_len[e]].push_back(dst_);
      }
    }
    for (auto &l: gr[e]) {
      sort(l.begin(), l.end());
    }
  }
  //return ;

  for (int i = 0; i < Q; i++) {
    int res = 0;
    for (bool e: {0, 1}) {
      if (cyc_len[e] >= inf) {
        if (G[i] < inf) {
          res += gr[e][G[i]].size();
        }
      }
      else {
        auto &L = gr[e][G[i] % cyc_len[e]];
        res += upper_bound(L.begin(), L.end(), G[i]) - L.begin();
      }
    }
    answer(res);
  }
}

/*int main() {
  if (fopen(taskname".inp", "r")) {
    freopen(taskname".inp", "r", stdin);
    freopen(taskname".out", "w", stdout);
  }
  else {
    if (fopen(taskname".in", "r")) {
      freopen(taskname".in", "r", stdin);
      freopen(taskname".out", "w", stdout);
    }
  }

  cin.tie(0)->sync_with_stdio(0);

  int N, M, P; cin >> N >> M >> P;
  int R[M][2];
  for (int i = 0; i < M; i++) {
    cin >> R[i][0] >> R[i][1];
  }
  int Q; cin >> Q;
  int G[Q];
  for (int i = 0; i < Q; i++) {
    cin >> G[i];
  }

  //return 0;
  count_routes(N, M, P, R, Q, G);

  return 0;
}*/

/*
6 6 0
1 2
0 1
0 3
3 4
4 5
1 5
1
3
 */

/*
5 5 2
1 0
1 2
3 2
1 3
4 2
2
3 1
 */
# Verdict Execution time Memory Grader output
1 Correct 1 ms 460 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 2 ms 632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 460 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 2 ms 632 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 11 ms 5316 KB Output is correct
12 Correct 20 ms 8776 KB Output is correct
13 Correct 49 ms 26568 KB Output is correct
14 Correct 86 ms 29320 KB Output is correct
15 Correct 113 ms 30328 KB Output is correct
16 Correct 98 ms 21364 KB Output is correct
17 Correct 67 ms 18272 KB Output is correct
18 Correct 23 ms 8780 KB Output is correct
19 Correct 76 ms 29336 KB Output is correct
20 Correct 102 ms 30284 KB Output is correct
21 Correct 63 ms 21316 KB Output is correct
22 Correct 66 ms 18244 KB Output is correct
23 Correct 117 ms 32172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 460 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 2 ms 632 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 11 ms 5316 KB Output is correct
12 Correct 20 ms 8776 KB Output is correct
13 Correct 49 ms 26568 KB Output is correct
14 Correct 86 ms 29320 KB Output is correct
15 Correct 113 ms 30328 KB Output is correct
16 Correct 98 ms 21364 KB Output is correct
17 Correct 67 ms 18272 KB Output is correct
18 Correct 23 ms 8780 KB Output is correct
19 Correct 76 ms 29336 KB Output is correct
20 Correct 102 ms 30284 KB Output is correct
21 Correct 63 ms 21316 KB Output is correct
22 Correct 66 ms 18244 KB Output is correct
23 Correct 117 ms 32172 KB Output is correct
24 Correct 1 ms 312 KB Output is correct
25 Correct 10 ms 5452 KB Output is correct
26 Correct 23 ms 8780 KB Output is correct
27 Correct 47 ms 26564 KB Output is correct
28 Correct 113 ms 29276 KB Output is correct
29 Correct 115 ms 30344 KB Output is correct
30 Correct 101 ms 21444 KB Output is correct
31 Correct 68 ms 18256 KB Output is correct
32 Correct 21 ms 8776 KB Output is correct
33 Correct 76 ms 29256 KB Output is correct
34 Correct 113 ms 30320 KB Output is correct
35 Correct 87 ms 21420 KB Output is correct
36 Correct 78 ms 18448 KB Output is correct
37 Correct 94 ms 32196 KB Output is correct
38 Correct 82 ms 41216 KB Output is correct