Submission #116556

# Submission time Handle Problem Language Result Execution time Memory
116556 2019-06-12T23:00:01 Z dragonslayerit Tropical Garden (IOI11_garden) C++14
100 / 100
2607 ms 29788 KB
#include "garden.h"
#include "gardenlib.h"
#include <vector>
#include <queue>
#include <cstdio>

const int INF=1e9+7;

static std::vector<int> adj[150005];
static int to[300005];
static std::vector<int> from[300005];

static int dist[2][300005];

static void bfs(int start,int* dist){
  std::queue<int> q;
  q.push(start);
  while(!q.empty()){
    int node=q.front();
    q.pop();
    for(int child:from[node]){
      if(!dist[child]){
	dist[child]=dist[node]+1;
	q.push(child);
      }
    }
  }
}

void count_routes(int N, int M, int P, int R[][2], int Q, int G[])
{
  for(int i=0;i<M;i++){
    adj[R[i][0]].push_back(R[i][1]);
    adj[R[i][1]].push_back(R[i][0]);
  }
  /*
  for(int i=0;i<N;i++){
    printf("adj[%d][0]=%d\n",i,adj[i][0]);
    if(adj[i].size()>1){
      printf("adj[%d][1]=%d\n",i,adj[i][1]);
    }
  }
  */
  for(int i=0;i<N;i++){
    to[i<<1]=(adj[i][0]<<1)|(i==adj[adj[i][0]][0]);
    if(adj[i].size()>1){
      to[i<<1|1]=(adj[i][1]<<1)|(i==adj[adj[i][1]][0]);
    }else{
      to[i<<1|1]=to[i<<1];
    }
  }
  /*
  for(int i=0;i<N*2;i++){
    fprintf(stderr,"to[%d,%d]=%d,%d\n",i>>1,i&1,to[i]>>1,to[i]&1);
  }
  */
  for(int i=0;i<N*2;i++){
    from[to[i]].push_back(i);
  }
  bfs(P<<1,dist[0]);
  bfs(P<<1|1,dist[1]);
  int cycle[2];
  cycle[0]=dist[0][P<<1]?dist[0][P<<1]:INF;
  cycle[1]=dist[1][P<<1|1]?dist[1][P<<1|1]:INF;
  /*
  for(int i=0;i<N*2;i++){
    fprintf(stderr,"dist[%d,%d]=%d\n",i>>1,i&1,dist[0][i]);
  }
  */
  for(int q=0; q<Q; q++){
    int K=G[q];
    int ans=0;
    for(int i=0;i<N;i++){
      /*
      int x=i<<1;
      for(int k=0;k<K;k++){
	x=to[x];
      }
      if((x>>1)==P){
	ans++;
      }
      */
      if(dist[0][i<<1]) ans+=(K>=dist[0][i<<1]&&(K-dist[0][i<<1])%cycle[0]==0);
      if(dist[1][i<<1]) ans+=(K>=dist[1][i<<1]&&(K-dist[1][i<<1])%cycle[1]==0);
    }
    answer(ans);
  }
}


# Verdict Execution time Memory Grader output
1 Correct 12 ms 10972 KB Output is correct
2 Correct 12 ms 11000 KB Output is correct
3 Correct 12 ms 11040 KB Output is correct
4 Correct 11 ms 10872 KB Output is correct
5 Correct 12 ms 11000 KB Output is correct
6 Correct 12 ms 11000 KB Output is correct
7 Correct 11 ms 10872 KB Output is correct
8 Correct 12 ms 11000 KB Output is correct
9 Correct 14 ms 11228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 10972 KB Output is correct
2 Correct 12 ms 11000 KB Output is correct
3 Correct 12 ms 11040 KB Output is correct
4 Correct 11 ms 10872 KB Output is correct
5 Correct 12 ms 11000 KB Output is correct
6 Correct 12 ms 11000 KB Output is correct
7 Correct 11 ms 10872 KB Output is correct
8 Correct 12 ms 11000 KB Output is correct
9 Correct 14 ms 11228 KB Output is correct
10 Correct 11 ms 10872 KB Output is correct
11 Correct 24 ms 13048 KB Output is correct
12 Correct 43 ms 14456 KB Output is correct
13 Correct 58 ms 21596 KB Output is correct
14 Correct 136 ms 23288 KB Output is correct
15 Correct 168 ms 24368 KB Output is correct
16 Correct 126 ms 19988 KB Output is correct
17 Correct 187 ms 19532 KB Output is correct
18 Correct 55 ms 14584 KB Output is correct
19 Correct 134 ms 24056 KB Output is correct
20 Correct 167 ms 24420 KB Output is correct
21 Correct 141 ms 19952 KB Output is correct
22 Correct 130 ms 19448 KB Output is correct
23 Correct 148 ms 24264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 10972 KB Output is correct
2 Correct 12 ms 11000 KB Output is correct
3 Correct 12 ms 11040 KB Output is correct
4 Correct 11 ms 10872 KB Output is correct
5 Correct 12 ms 11000 KB Output is correct
6 Correct 12 ms 11000 KB Output is correct
7 Correct 11 ms 10872 KB Output is correct
8 Correct 12 ms 11000 KB Output is correct
9 Correct 14 ms 11228 KB Output is correct
10 Correct 11 ms 10872 KB Output is correct
11 Correct 24 ms 13048 KB Output is correct
12 Correct 43 ms 14456 KB Output is correct
13 Correct 58 ms 21596 KB Output is correct
14 Correct 136 ms 23288 KB Output is correct
15 Correct 168 ms 24368 KB Output is correct
16 Correct 126 ms 19988 KB Output is correct
17 Correct 187 ms 19532 KB Output is correct
18 Correct 55 ms 14584 KB Output is correct
19 Correct 134 ms 24056 KB Output is correct
20 Correct 167 ms 24420 KB Output is correct
21 Correct 141 ms 19952 KB Output is correct
22 Correct 130 ms 19448 KB Output is correct
23 Correct 148 ms 24264 KB Output is correct
24 Correct 13 ms 10904 KB Output is correct
25 Correct 121 ms 13072 KB Output is correct
26 Correct 166 ms 14588 KB Output is correct
27 Correct 1412 ms 21632 KB Output is correct
28 Correct 1313 ms 24208 KB Output is correct
29 Correct 1584 ms 24496 KB Output is correct
30 Correct 951 ms 20128 KB Output is correct
31 Correct 896 ms 19564 KB Output is correct
32 Correct 187 ms 14456 KB Output is correct
33 Correct 1365 ms 23840 KB Output is correct
34 Correct 1527 ms 24488 KB Output is correct
35 Correct 943 ms 20216 KB Output is correct
36 Correct 1635 ms 19532 KB Output is correct
37 Correct 996 ms 24244 KB Output is correct
38 Correct 2607 ms 29788 KB Output is correct