Submission #304714

# Submission time Handle Problem Language Result Execution time Memory
304714 2020-09-21T18:06:30 Z AlexPop28 Tropical Garden (IOI11_garden) C++11
69 / 100
171 ms 35036 KB
#include <bits/stdc++.h>
#include "garden.h"
#include "gardenlib.h"

#define dbg() cerr <<
#define name(x) (#x) << ": " << (x) << ' ' <<

using namespace std;

void count_routes(int n, int m, int p, int r[][2], int q, int qrs[]) {
  vector<vector<int>> to(n);
  for (int i = 0; i < m; ++i) {
    int a = r[i][0], b = r[i][1];
    if ((int)to[a].size() < 2) to[a].emplace_back(b);
    if ((int)to[b].size() < 2) to[b].emplace_back(a);
  }
  
  vector<int> nxt(2 * n, -1);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < 2 && j < (int)to[i].size(); ++j) {
      int x = to[i][j];
      int node = (to[x][0] == i) * n + x;
      nxt[j * n + i] = node;
    }
  }
  for (int i = n; i < 2 * n; ++i) if (nxt[i] == -1) nxt[i] = nxt[i - n];
  
  vector<vector<int>> adj(2 * n);
  for (int i = 0; i < 2 * n; ++i) {
    adj[nxt[i]].emplace_back(i);
  }
  
  vector<bool> vis(2 * n);
  
  function<void(int, int , int&, vector<int>&)> 
  DFS = [&](int node, int stop, int &cycle_len, vector<int> &dist) {
    if (vis[node]) {
      if (node == stop) {
        cycle_len = dist[node];
      }
      return;
    }
    
    vis[node] = true;
    for (int &x : adj[node]) {
      dist[x] = 1 + dist[node];
      DFS(x, stop, cycle_len, dist);
    }
  };
  
  const int INF = (int)1e9 + 10;
  vector<int> cycle(2, INF);
  vector<vector<int>> dist(2, vector<int>(2 * n, -1));
  
  dist[0][p] = dist[1][p + n] = 0;
  DFS(p, p, cycle[0], dist[0]);
  fill(vis.begin(), vis.end(), false);
  DFS(p + n, p + n, cycle[1], dist[1]);
  
  vector<vector<int>> cnt_dist(2, vector<int>(2 * n));
  for (int i = 0; i < 2; ++i) {
    for (int node = 0; node < n; ++node) { // doar nodurile "bune" conteaza ca doar din ele pot incepe
      if (dist[i][node] == -1) continue;
      ++cnt_dist[i][dist[i][node]];
    }
  }
  
  for (int it = 0; it < q; ++it) {
    int k = qrs[it];
    int ans = 0;
    for (int i = 0; i < 2; ++i) {
      int kk = k % cycle[i];
      for (int d = kk; d <= n && d <= k; d += cycle[i])
        ans += cnt_dist[i][d];
    }
    answer(ans);
  }
}


# Verdict Execution time Memory Grader output
1 Correct 1 ms 640 KB Output is correct
2 Correct 1 ms 512 KB Output is correct
3 Correct 1 ms 640 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 1 ms 768 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 1 ms 512 KB Output is correct
9 Correct 3 ms 640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 640 KB Output is correct
2 Correct 1 ms 512 KB Output is correct
3 Correct 1 ms 640 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 1 ms 768 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 1 ms 512 KB Output is correct
9 Correct 3 ms 640 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 14 ms 4944 KB Output is correct
12 Correct 31 ms 7760 KB Output is correct
13 Correct 70 ms 35036 KB Output is correct
14 Correct 124 ms 26072 KB Output is correct
15 Correct 160 ms 26444 KB Output is correct
16 Correct 114 ms 18732 KB Output is correct
17 Correct 99 ms 15648 KB Output is correct
18 Correct 31 ms 7768 KB Output is correct
19 Correct 130 ms 26176 KB Output is correct
20 Correct 171 ms 26572 KB Output is correct
21 Correct 112 ms 18476 KB Output is correct
22 Correct 100 ms 15648 KB Output is correct
23 Correct 136 ms 28888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 640 KB Output is correct
2 Correct 1 ms 512 KB Output is correct
3 Correct 1 ms 640 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 1 ms 768 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 1 ms 512 KB Output is correct
9 Correct 3 ms 640 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 14 ms 4944 KB Output is correct
12 Correct 31 ms 7760 KB Output is correct
13 Correct 70 ms 35036 KB Output is correct
14 Correct 124 ms 26072 KB Output is correct
15 Correct 160 ms 26444 KB Output is correct
16 Correct 114 ms 18732 KB Output is correct
17 Correct 99 ms 15648 KB Output is correct
18 Correct 31 ms 7768 KB Output is correct
19 Correct 130 ms 26176 KB Output is correct
20 Correct 171 ms 26572 KB Output is correct
21 Correct 112 ms 18476 KB Output is correct
22 Correct 100 ms 15648 KB Output is correct
23 Correct 136 ms 28888 KB Output is correct
24 Incorrect 1 ms 384 KB Output isn't correct
25 Halted 0 ms 0 KB -