Submission #986902

# Submission time Handle Problem Language Result Execution time Memory
986902 2024-05-21T14:16:47 Z huutuan Tropical Garden (IOI11_garden) C++14
100 / 100
106 ms 55752 KB
#include "garden.h"
#include "gardenlib.h"

#include <bits/stdc++.h>

using namespace std;

const int N=3e5+10;
int n, m, p;
vector<int> g[N], gg[N];
int nxt[N], vis[N], on_cycle[N], dep[N], cnt[N];
vector<int> vv[N];
int ans[N];

void dfs(int u){
   for (int v:gg[u]) if (dep[v]==-1){
      dep[v]=dep[u]+1;
      dfs(v);
   }
}

void count_routes(int _N, int M, int P, int R[][2], int Q, int G[])
{
   n=_N, m=M, p=P;
   for (int i=0; i<m; ++i){
      int u=R[i][0], v=R[i][1];
      g[u].push_back(v);
      g[v].push_back(u);
   }
   for (int u=0; u<n; ++u) if (g[u].size()){
      int v=g[u][0];
      nxt[u<<1]=v<<1|(g[v][0]==u);
      gg[nxt[u<<1]].push_back(u<<1);
      int v2=g[u][0];
      if ((int)g[u].size()>=2) v2=g[u][1];
      nxt[u<<1|1]=v2<<1|(g[v2][0]==u);
      gg[nxt[u<<1|1]].push_back(u<<1|1);
   }
   p<<=1;
   vector<int> cycle;
   {
      int u=p;
      while (1){
         cycle.push_back(u); vis[u]=1;
         u=nxt[u];
         if (vis[u]) break;
      }
      cycle.erase(cycle.begin(), find(cycle.begin(), cycle.end(), u));
      for (int i:cycle) on_cycle[i]=1;
   }
   memset(dep, -1, sizeof dep);
   dep[p]=0;
   dfs(p);
   if (!on_cycle[p]){
      for (int i=0; i<(n<<1); i+=2) if (dep[i]!=-1) ++cnt[dep[i]];
      for (int i=0; i<Q; ++i) ans[i]+=(G[i]<N?cnt[G[i]]:0);
   }else{
      int mod=cycle.size();
      for (int i=0; i<(n<<1); i+=2) if (dep[i]!=-1) vv[dep[i]%mod].push_back(dep[i]);
      for (int i=0; i<mod; ++i) sort(vv[i].begin(), vv[i].end());
      for (int i=0; i<Q; ++i) ans[i]+=(upper_bound(vv[G[i]%mod].begin(), vv[G[i]%mod].end(), G[i])-vv[G[i]%mod].begin());
   }
   memset(dep, -1, sizeof dep);
   memset(cnt, 0, sizeof cnt);
   for (int i=0; i<n; ++i) vv[i].clear();
   p|=1;
   memset(vis, 0, sizeof vis);
   memset(on_cycle, 0, sizeof on_cycle);
   cycle.clear();
   {
      int u=p;
      while (1){
         cycle.push_back(u); vis[u]=1;
         u=nxt[u];
         if (vis[u]) break;
      }
      cycle.erase(cycle.begin(), find(cycle.begin(), cycle.end(), u));
      for (int i:cycle) on_cycle[i]=1;
   }
   dep[p]=0;
   dfs(p);
   if (!on_cycle[p]){
      for (int i=0; i<(n<<1); i+=2) if (dep[i]!=-1) ++cnt[dep[i]];
      for (int i=0; i<Q; ++i) ans[i]+=(G[i]<N?cnt[G[i]]:0);
   }else{
      int mod=cycle.size();
      for (int i=0; i<(n<<1); i+=2) if (dep[i]!=-1) vv[dep[i]%mod].push_back(dep[i]);
      for (int i=0; i<mod; ++i) sort(vv[i].begin(), vv[i].end());
      for (int i=0; i<Q; ++i) ans[i]+=(upper_bound(vv[G[i]%mod].begin(), vv[G[i]%mod].end(), G[i])-vv[G[i]%mod].begin());
   }
   for (int i=0; i<Q; ++i) answer(ans[i]);
}
# Verdict Execution time Memory Grader output
1 Correct 10 ms 28252 KB Output is correct
2 Correct 10 ms 28252 KB Output is correct
3 Correct 9 ms 28248 KB Output is correct
4 Correct 9 ms 27996 KB Output is correct
5 Correct 9 ms 27996 KB Output is correct
6 Correct 10 ms 28368 KB Output is correct
7 Correct 10 ms 27996 KB Output is correct
8 Correct 10 ms 28252 KB Output is correct
9 Correct 11 ms 28252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 28252 KB Output is correct
2 Correct 10 ms 28252 KB Output is correct
3 Correct 9 ms 28248 KB Output is correct
4 Correct 9 ms 27996 KB Output is correct
5 Correct 9 ms 27996 KB Output is correct
6 Correct 10 ms 28368 KB Output is correct
7 Correct 10 ms 27996 KB Output is correct
8 Correct 10 ms 28252 KB Output is correct
9 Correct 11 ms 28252 KB Output is correct
10 Correct 10 ms 27996 KB Output is correct
11 Correct 17 ms 30044 KB Output is correct
12 Correct 28 ms 31360 KB Output is correct
13 Correct 61 ms 48332 KB Output is correct
14 Correct 97 ms 39156 KB Output is correct
15 Correct 94 ms 40124 KB Output is correct
16 Correct 69 ms 37200 KB Output is correct
17 Correct 78 ms 35988 KB Output is correct
18 Correct 29 ms 31316 KB Output is correct
19 Correct 81 ms 39252 KB Output is correct
20 Correct 106 ms 40184 KB Output is correct
21 Correct 79 ms 37012 KB Output is correct
22 Correct 78 ms 35952 KB Output is correct
23 Correct 99 ms 40416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 28252 KB Output is correct
2 Correct 10 ms 28252 KB Output is correct
3 Correct 9 ms 28248 KB Output is correct
4 Correct 9 ms 27996 KB Output is correct
5 Correct 9 ms 27996 KB Output is correct
6 Correct 10 ms 28368 KB Output is correct
7 Correct 10 ms 27996 KB Output is correct
8 Correct 10 ms 28252 KB Output is correct
9 Correct 11 ms 28252 KB Output is correct
10 Correct 10 ms 27996 KB Output is correct
11 Correct 17 ms 30044 KB Output is correct
12 Correct 28 ms 31360 KB Output is correct
13 Correct 61 ms 48332 KB Output is correct
14 Correct 97 ms 39156 KB Output is correct
15 Correct 94 ms 40124 KB Output is correct
16 Correct 69 ms 37200 KB Output is correct
17 Correct 78 ms 35988 KB Output is correct
18 Correct 29 ms 31316 KB Output is correct
19 Correct 81 ms 39252 KB Output is correct
20 Correct 106 ms 40184 KB Output is correct
21 Correct 79 ms 37012 KB Output is correct
22 Correct 78 ms 35952 KB Output is correct
23 Correct 99 ms 40416 KB Output is correct
24 Correct 10 ms 27996 KB Output is correct
25 Correct 19 ms 29968 KB Output is correct
26 Correct 29 ms 31300 KB Output is correct
27 Correct 50 ms 48396 KB Output is correct
28 Correct 90 ms 39216 KB Output is correct
29 Correct 99 ms 40276 KB Output is correct
30 Correct 73 ms 37204 KB Output is correct
31 Correct 69 ms 36180 KB Output is correct
32 Correct 28 ms 31324 KB Output is correct
33 Correct 95 ms 39248 KB Output is correct
34 Correct 106 ms 40016 KB Output is correct
35 Correct 73 ms 37292 KB Output is correct
36 Correct 70 ms 36160 KB Output is correct
37 Correct 103 ms 40256 KB Output is correct
38 Correct 83 ms 55752 KB Output is correct