Submission #103375

# Submission time Handle Problem Language Result Execution time Memory
103375 2019-03-30T03:51:50 Z wxh010910 Tropical Garden (IOI11_garden) C++17
100 / 100
148 ms 32848 KB
#include <bits/stdc++.h>
#include "garden.h"
#include "gardenlib.h"

using namespace std;

void count_routes(int N, int M, int P, int R[][2], int Q, int G[]) {
  vector<int> deg(N);
  vector<int> most(N, -1);
  for (int i = 0; i < M; ++i) {
    for (int j = 0; j < 2; ++j) {
      ++deg[R[i][j]];
      if (most[R[i][j]] == -1) {
        most[R[i][j]] = i;
      }
    }
  }
  vector<int> to(N * 2, -1);
  for (int i = 0; i < M; ++i) {
    for (int j = 0; j < 2; ++j) {
      if (to[R[i][j]] == -1) {
        to[R[i][j]] = R[i][!j] + N * (most[R[i][!j]] == i && deg[R[i][!j]] > 1);
      } else if (to[R[i][j] + N] == -1) {
        to[R[i][j] + N] = R[i][!j] + N * (most[R[i][!j]] == i && deg[R[i][!j]] > 1);
      }
    }
  }
  vector<vector<int>> adj(N * 2);
  for (int i = 0; i < N; ++i) {
    adj[to[i]].push_back(i);
    if (deg[i] > 1) {
      adj[to[i + N]].push_back(i + N);
    }
  }
  vector<pair<int, int>> queries(Q);
  for (int i = 0; i < Q; ++i) {
    queries[i] = make_pair(G[i], i);
  }
  sort(queries.begin(), queries.end());
  vector<int> ans(Q);
  auto solve = [&](int source) {
    vector<int> depth(N * 2, -1);
    function<void(int)> dfs = [&](int x) {
      for (auto y : adj[x]) {
        if (depth[y] == -1) {
          depth[y] = depth[x] + 1;
          dfs(y);
        }
      }
    };
    depth[source] = 0;
    dfs(source);
    vector<int> dists;
    for (int i = 0; i < N; ++i) {
      if (depth[i] != -1) {
        dists.push_back(depth[i]);
      }
    }
    sort(dists.begin(), dists.end());
    int cycle = 0;
    vector<bool> visit(N * 2);
    int u = source;
    while (!visit[u]) {
      visit[u] = true;
      u = to[u];
      ++cycle;
    }
    if (u != source) {
      cycle = -1;
    }
    vector<int> cnt(N * 2);
    for (int i = 0, j = 0; i < Q; ++i) {
      while (j < (int) dists.size() && dists[j] <= queries[i].first) {
        if (cycle == -1) {
          ++cnt[dists[j]];
        } else {
          ++cnt[dists[j] % cycle];
        }
        ++j;
      }
      if (cycle == -1) {
        if (queries[i].first < N * 2) {
          ans[queries[i].second] += cnt[queries[i].first];
        }
      } else {
        ans[queries[i].second] += cnt[queries[i].first % cycle];
      }
    }
  };
  solve(P);
  if (deg[P] > 1) {
    solve(P + N);
  }
  for (int i = 0; i < Q; ++i) {
    answer(ans[i]);
  }
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 604 KB Output is correct
2 Correct 2 ms 504 KB Output is correct
3 Correct 2 ms 504 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 3 ms 604 KB Output is correct
7 Correct 2 ms 408 KB Output is correct
8 Correct 2 ms 476 KB Output is correct
9 Correct 4 ms 648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 604 KB Output is correct
2 Correct 2 ms 504 KB Output is correct
3 Correct 2 ms 504 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 3 ms 604 KB Output is correct
7 Correct 2 ms 408 KB Output is correct
8 Correct 2 ms 476 KB Output is correct
9 Correct 4 ms 648 KB Output is correct
10 Correct 2 ms 380 KB Output is correct
11 Correct 13 ms 3576 KB Output is correct
12 Correct 30 ms 5508 KB Output is correct
13 Correct 77 ms 25196 KB Output is correct
14 Correct 106 ms 17228 KB Output is correct
15 Correct 148 ms 18092 KB Output is correct
16 Correct 113 ms 13464 KB Output is correct
17 Correct 106 ms 12044 KB Output is correct
18 Correct 29 ms 5456 KB Output is correct
19 Correct 107 ms 17248 KB Output is correct
20 Correct 148 ms 18016 KB Output is correct
21 Correct 111 ms 13112 KB Output is correct
22 Correct 101 ms 11344 KB Output is correct
23 Correct 104 ms 18744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 604 KB Output is correct
2 Correct 2 ms 504 KB Output is correct
3 Correct 2 ms 504 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 3 ms 604 KB Output is correct
7 Correct 2 ms 408 KB Output is correct
8 Correct 2 ms 476 KB Output is correct
9 Correct 4 ms 648 KB Output is correct
10 Correct 2 ms 380 KB Output is correct
11 Correct 13 ms 3576 KB Output is correct
12 Correct 30 ms 5508 KB Output is correct
13 Correct 77 ms 25196 KB Output is correct
14 Correct 106 ms 17228 KB Output is correct
15 Correct 148 ms 18092 KB Output is correct
16 Correct 113 ms 13464 KB Output is correct
17 Correct 106 ms 12044 KB Output is correct
18 Correct 29 ms 5456 KB Output is correct
19 Correct 107 ms 17248 KB Output is correct
20 Correct 148 ms 18016 KB Output is correct
21 Correct 111 ms 13112 KB Output is correct
22 Correct 101 ms 11344 KB Output is correct
23 Correct 104 ms 18744 KB Output is correct
24 Correct 3 ms 348 KB Output is correct
25 Correct 14 ms 3580 KB Output is correct
26 Correct 28 ms 5644 KB Output is correct
27 Correct 76 ms 25448 KB Output is correct
28 Correct 104 ms 17528 KB Output is correct
29 Correct 143 ms 18104 KB Output is correct
30 Correct 111 ms 13248 KB Output is correct
31 Correct 94 ms 11308 KB Output is correct
32 Correct 28 ms 5260 KB Output is correct
33 Correct 102 ms 16876 KB Output is correct
34 Correct 145 ms 17536 KB Output is correct
35 Correct 113 ms 12560 KB Output is correct
36 Correct 122 ms 11312 KB Output is correct
37 Correct 148 ms 18260 KB Output is correct
38 Correct 123 ms 32848 KB Output is correct