Submission #286817

#TimeUsernameProblemLanguageResultExecution timeMemory
286817ChrisTTropical Garden (IOI11_garden)C++17
69 / 100
5058 ms28480 KiB
#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> togo(2*n+1,-1);
  for (int i = 0; i < m; i++) {
    auto [a,b] = r[i];
    if (!~togo[2*a]) togo[2*a] = 2*b+(!~togo[2*b]);
    else if (!~togo[2*a+1]) togo[2*a+1] = 2*b+(!~togo[2*b]);
    if (!~togo[2*b]) togo[2*b] = 2*a+(togo[2*a]/2==b);
    else if (!~togo[2*b+1]) togo[2*b+1] = 2*a+(togo[2*a]/2==b);
  }
  n *= 2;
  vector<vector<int>> adj(n+1);
  for (int i = 0; i < n; i++) {
    if (!~togo[i]) togo[i] = togo[i-1];
    adj[togo[i]].push_back(i);
  }
  vector<int> cmp(n+1), dist(n+1), to(n+1,-1), ind(n+1); vector<vector<int>> cyc(n+1); int cc = 0;
  for (int i = 0; i < n; i++) if (!cmp[i]) {
    int cur = i; ++cc;
    while (!cmp[cur]) {
      cmp[cur] = cc, cur = togo[cur];
    }
    int o = cur; queue<int> qq;
    do {
      ind[cur] = (int)cyc[cc].size(); cyc[cc].push_back(cur); to[cur] = cur; qq.push(cur);
    } while ((cur = togo[cur]) != o);
    while (!qq.empty()) {
      int cur = qq.front(); qq.pop();
      for (int j : adj[cur]) if (!~to[j]) {
        to[j] = to[cur]; cmp[j] = cc;
        dist[j] = dist[cur] + 1;
        qq.push(j);
      }
    }
  }
  array<vector<int>,2> temp;
  for (int i = 2*p; i <= 2*p+1; i++) if (dist[i]) {
    queue<int> qq; qq.push(i); int par = i % 2;
    temp[par].resize(n);
    fill(temp[par].begin(),temp[par].end(),1e9);
    temp[par][i] = 0;
    while (!qq.empty()) {
      int cur = qq.front(); qq.pop();
      for (int j : adj[cur]) if (temp[par][cur] + 1 < temp[par][j]) {
        temp[par][j] = temp[par][cur] + 1;
        qq.push(j);
      }
    }
  }
  auto get = [&] (int x, int k) {
    int ret = 0;
    if (dist[x]) {
      for (int i = 0; i < n; i+=2) ret += temp[x%2][i] == k;
    } else {
      for (int i = 0; i < n; i+=2) if (dist[i] <= k) {
        int go = k - dist[i], id = (ind[to[i]] + go) % ((int)cyc[cmp[i]].size());
        ret += cyc[cmp[i]][id] == x;
      }
    }
    return ret;
  };
  for (int i = 0; i < q; i++) answer(get(2*p,g[i])+get(2*p+1,g[i]));
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...