Submission #929040

# Submission time Handle Problem Language Result Execution time Memory
929040 2024-02-17T14:28:57 Z OAleksa Tropical Garden (IOI11_garden) C++14
100 / 100
2224 ms 54400 KB
#include "garden.h"
#include "gardenlib.h"
#include <bits/stdc++.h>
using namespace std;
const int N = 300069;
int up[N], vis[N], n, nxt[N], deg[N], dep1[N], sz[N], dep2[N];
vector<int> t[N], rn[N];
void dfs(int v) {
   vis[v] = 1;
   if (t[t[v][0]][0] == v && (int)t[t[v][0]].size() > 1) {
      nxt[v] = n + t[v][0], deg[n + t[v][0]]++;
      rn[n + t[v][0]].push_back(v);
   }
   else {
      nxt[v] = t[v][0], deg[t[v][0]]++;
      rn[t[v][0]].push_back(v);
   }
   if (t[v].size() > 1) {
      if (t[t[v][1]][0] == v && (int)t[t[v][1]].size() > 1) {
         nxt[n + v] = n + t[v][1], deg[n + t[v][1]]++;
         rn[n + t[v][1]].push_back(n + v);
      }
      else {
         nxt[n + v] = t[v][1], deg[t[v][1]]++;
         rn[t[v][1]].push_back(n + v);
      }
   }
   for (auto u : t[v]) {
      if (!vis[u])
         dfs(u);
   }
}

void count_routes(int n1, int m, int p, int r[][2], int q, int k[])
{
   ++p;
   n = n1;
   for (int i = 0;i < m;i++) {
      r[i][0]++, r[i][1]++;
      t[r[i][0]].push_back(r[i][1]);
      t[r[i][1]].push_back(r[i][0]);
   }
   for (int i = 1;i <= n;i++) {
      if (!vis[i])
         dfs(i);
   }
   for (int i = 1;i <= 2 * n;i++) {
      dep1[i] = dep2[i] = 1e9;
   }
   queue<int> que;
   que.push(p);
   dep1[p] = dep2[n + p] = 0;
   //cepamo reverse
   while (!que.empty()) {
      auto v = que.front();
      que.pop();
      for (auto r : rn[v]) {
         if (dep1[r] <= dep1[v] + 1)
            continue;
         dep1[r] = dep1[v] + 1;
         que.push(r);
      }
   }
   que.push(n + p);
   while (!que.empty()) {
      auto v = que.front();
      que.pop();
      for (auto r : rn[v]) {
         if (dep2[r] <= dep2[v] + 1)
            continue;
         dep2[r] = dep2[v] + 1;
         que.push(r);
      }
   }

   for (int i = 1;i <= 2 * n;i++) {
      if (deg[i] == 0)
         que.push(i);
   }
   fill(vis, vis + 2 * n + 1, 0);
   while (!que.empty()) {
      auto v = que.front();
      vis[v] = 1;
      que.pop();
      int r = nxt[v];
      if (r != 0) {
         deg[r]--;
         if (deg[r] == 0)
            que.push(r);
      }
   }
   for (int i = 1;i <= 2 * n;i++) {
      if (!vis[i]) {
         int t = i, s = 0;
         vector<int> o;
         while (t > 0 && !vis[t]) {
            vis[t] = 1;
            up[t] = t;
            o.push_back(t);
            ++s;
            t = nxt[t];
         }
         for (auto u : o)
            sz[u] = s;
      }
   }
   for (int i = 0;i < q;i++) {
      int ans = 0;
      for (int j = 1;j <= n;j++) {
         //up[v] = v // u ciklusu je
         int o = 0;
         if (up[p] == p) {
            if (dep1[j] != 1e9 && dep1[j] <= k[i]) {
               int x = k[i] - dep1[j];
               o |= (x % sz[p] == 0);
            }
         }
         else if (up[p] != p)
            o |= (dep1[j] == k[i]);
         int p1 = n + p;
         if (up[p1] == p1) {
            if (dep2[j] != 1e9 && dep2[j] <= k[i]) {
               int x = k[i] - dep2[j];
               o |= (x % sz[p1] == 0);
            }
         }
         else if (up[p1] != p1)
            o |= (dep2[j] == k[i]);
         ans += o;
      }
      answer(ans);
   }
}

# Verdict Execution time Memory Grader output
1 Correct 4 ms 23132 KB Output is correct
2 Correct 5 ms 23132 KB Output is correct
3 Correct 6 ms 23352 KB Output is correct
4 Correct 5 ms 23132 KB Output is correct
5 Correct 5 ms 23132 KB Output is correct
6 Correct 5 ms 23132 KB Output is correct
7 Correct 4 ms 23384 KB Output is correct
8 Correct 5 ms 23132 KB Output is correct
9 Correct 6 ms 23384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 23132 KB Output is correct
2 Correct 5 ms 23132 KB Output is correct
3 Correct 6 ms 23352 KB Output is correct
4 Correct 5 ms 23132 KB Output is correct
5 Correct 5 ms 23132 KB Output is correct
6 Correct 5 ms 23132 KB Output is correct
7 Correct 4 ms 23384 KB Output is correct
8 Correct 5 ms 23132 KB Output is correct
9 Correct 6 ms 23384 KB Output is correct
10 Correct 5 ms 23132 KB Output is correct
11 Correct 12 ms 25540 KB Output is correct
12 Correct 24 ms 29316 KB Output is correct
13 Correct 38 ms 41796 KB Output is correct
14 Correct 78 ms 37200 KB Output is correct
15 Correct 108 ms 38380 KB Output is correct
16 Correct 64 ms 36436 KB Output is correct
17 Correct 63 ms 35656 KB Output is correct
18 Correct 24 ms 29520 KB Output is correct
19 Correct 89 ms 37228 KB Output is correct
20 Correct 92 ms 37968 KB Output is correct
21 Correct 68 ms 36432 KB Output is correct
22 Correct 61 ms 35524 KB Output is correct
23 Correct 90 ms 37640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 23132 KB Output is correct
2 Correct 5 ms 23132 KB Output is correct
3 Correct 6 ms 23352 KB Output is correct
4 Correct 5 ms 23132 KB Output is correct
5 Correct 5 ms 23132 KB Output is correct
6 Correct 5 ms 23132 KB Output is correct
7 Correct 4 ms 23384 KB Output is correct
8 Correct 5 ms 23132 KB Output is correct
9 Correct 6 ms 23384 KB Output is correct
10 Correct 5 ms 23132 KB Output is correct
11 Correct 12 ms 25540 KB Output is correct
12 Correct 24 ms 29316 KB Output is correct
13 Correct 38 ms 41796 KB Output is correct
14 Correct 78 ms 37200 KB Output is correct
15 Correct 108 ms 38380 KB Output is correct
16 Correct 64 ms 36436 KB Output is correct
17 Correct 63 ms 35656 KB Output is correct
18 Correct 24 ms 29520 KB Output is correct
19 Correct 89 ms 37228 KB Output is correct
20 Correct 92 ms 37968 KB Output is correct
21 Correct 68 ms 36432 KB Output is correct
22 Correct 61 ms 35524 KB Output is correct
23 Correct 90 ms 37640 KB Output is correct
24 Correct 5 ms 23132 KB Output is correct
25 Correct 89 ms 25436 KB Output is correct
26 Correct 139 ms 29568 KB Output is correct
27 Correct 2044 ms 41940 KB Output is correct
28 Correct 806 ms 37200 KB Output is correct
29 Correct 2215 ms 38044 KB Output is correct
30 Correct 1298 ms 36536 KB Output is correct
31 Correct 1266 ms 35780 KB Output is correct
32 Correct 100 ms 29524 KB Output is correct
33 Correct 812 ms 37480 KB Output is correct
34 Correct 2224 ms 37996 KB Output is correct
35 Correct 1393 ms 36568 KB Output is correct
36 Correct 1265 ms 35924 KB Output is correct
37 Correct 666 ms 37460 KB Output is correct
38 Correct 1794 ms 54400 KB Output is correct