Submission #14696

# Submission time Handle Problem Language Result Execution time Memory
14696 2015-06-05T04:51:52 Z minsu Tropical Garden (IOI11_garden) C++14
100 / 100
2313 ms 15644 KB
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
#include "garden.h"
#include "gardenlib.h"
using namespace std;
const int MAXN=1e6;
int elast[MAXN], eprev[MAXN], eadj[MAXN], eind;
int bpath[MAXN][2], bpathn[MAXN];
int L[MAXN], C[MAXN];
void add_edge(int u, int v){
  eadj[eind]=v; eprev[eind]=elast[u]; elast[u]=eind++;
}
void count_routes(int N, int M, int P, int R[][2], int Q, int G[])
{
  for(int i=0;i<M;i++){
    int u=R[i][0], v=R[i][1];
    if(bpathn[u]<2) bpath[u][bpathn[u]++]=v;
    if(bpathn[v]<2) bpath[v][bpathn[v]++]=u;
  }
  for(int i=0;i<N;i++) if(bpathn[i]==1) bpath[i][1]=bpath[i][0], bpathn[i]++;
  memset( elast, -1, sizeof elast );
  // Vertex 0 to 2*N-1
  for(int u=0;u<N;u++){
  	if(bpathn[u]==0) continue;
    int F=bpath[u][0], S=bpath[u][1];
    if(bpath[F][0]!=u) add_edge(F*2, u*2); else add_edge(F*2+1, u*2);
    if(bpath[S][0]!=u) add_edge(S*2, u*2+1); else add_edge(S*2+1, u*2+1);
  }
  memset( C, -1, sizeof C );
  queue<int> q; q.push(P*2); q.push(P*2+1);
  L[P*2]=L[P*2+1]=0; C[P*2]=P*2; C[P*2+1]=P*2+1;
  while(!q.empty()){
  	int here=q.front(); q.pop();
  	for(int i=elast[here];i!=-1;i=eprev[i]){
  		int there=eadj[i];
  		L[there]=L[here]+1; C[there]=C[here];
  		if(there/2==P) continue;
  		q.push(there);
  	}
  }
  for(int i=0;i<Q;i++){
    int ans=0;
    for(int b=0;b<2*N;b+=2){
        int g=G[i], bb=b;
        if(C[bb]==-1) continue;
        g-=L[bb]; bb=C[bb];
        if(g>0){ g-=L[bb]; bb=C[bb]; }
        if(g>0){
            if(bb==C[bb] && L[bb]!=0){
                g%=L[bb];
            }else if(bb!=C[bb] && L[bb]!=0 && L[C[bb]]!=0){
                g%=(L[bb]+L[C[bb]]);
            }
        }
        while(g>0 && bb!=-1 && L[bb]!=0){
            g-=L[bb]; bb=C[bb];
        }
        if(g==0) ans++;
    }
    answer(ans);
  }
}
# Verdict Execution time Memory Grader output
1 Correct 9 ms 8300 KB Output is correct
2 Correct 9 ms 8284 KB Output is correct
3 Correct 9 ms 8312 KB Output is correct
4 Correct 9 ms 8156 KB Output is correct
5 Correct 8 ms 8184 KB Output is correct
6 Correct 9 ms 8184 KB Output is correct
7 Correct 9 ms 8168 KB Output is correct
8 Correct 9 ms 8312 KB Output is correct
9 Correct 11 ms 8332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 8300 KB Output is correct
2 Correct 9 ms 8284 KB Output is correct
3 Correct 9 ms 8312 KB Output is correct
4 Correct 9 ms 8156 KB Output is correct
5 Correct 8 ms 8184 KB Output is correct
6 Correct 9 ms 8184 KB Output is correct
7 Correct 9 ms 8168 KB Output is correct
8 Correct 9 ms 8312 KB Output is correct
9 Correct 11 ms 8332 KB Output is correct
10 Correct 9 ms 8184 KB Output is correct
11 Correct 15 ms 9284 KB Output is correct
12 Correct 23 ms 10460 KB Output is correct
13 Correct 32 ms 12652 KB Output is correct
14 Correct 57 ms 15268 KB Output is correct
15 Correct 71 ms 15412 KB Output is correct
16 Correct 58 ms 13608 KB Output is correct
17 Correct 57 ms 13196 KB Output is correct
18 Correct 24 ms 10208 KB Output is correct
19 Correct 56 ms 14764 KB Output is correct
20 Correct 73 ms 14940 KB Output is correct
21 Correct 60 ms 13100 KB Output is correct
22 Correct 58 ms 12636 KB Output is correct
23 Correct 55 ms 15336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 8300 KB Output is correct
2 Correct 9 ms 8284 KB Output is correct
3 Correct 9 ms 8312 KB Output is correct
4 Correct 9 ms 8156 KB Output is correct
5 Correct 8 ms 8184 KB Output is correct
6 Correct 9 ms 8184 KB Output is correct
7 Correct 9 ms 8168 KB Output is correct
8 Correct 9 ms 8312 KB Output is correct
9 Correct 11 ms 8332 KB Output is correct
10 Correct 9 ms 8184 KB Output is correct
11 Correct 15 ms 9284 KB Output is correct
12 Correct 23 ms 10460 KB Output is correct
13 Correct 32 ms 12652 KB Output is correct
14 Correct 57 ms 15268 KB Output is correct
15 Correct 71 ms 15412 KB Output is correct
16 Correct 58 ms 13608 KB Output is correct
17 Correct 57 ms 13196 KB Output is correct
18 Correct 24 ms 10208 KB Output is correct
19 Correct 56 ms 14764 KB Output is correct
20 Correct 73 ms 14940 KB Output is correct
21 Correct 60 ms 13100 KB Output is correct
22 Correct 58 ms 12636 KB Output is correct
23 Correct 55 ms 15336 KB Output is correct
24 Correct 10 ms 8156 KB Output is correct
25 Correct 106 ms 9376 KB Output is correct
26 Correct 125 ms 10400 KB Output is correct
27 Correct 1239 ms 12292 KB Output is correct
28 Correct 1271 ms 14900 KB Output is correct
29 Correct 2297 ms 15052 KB Output is correct
30 Correct 1379 ms 13208 KB Output is correct
31 Correct 1297 ms 12744 KB Output is correct
32 Correct 139 ms 10348 KB Output is correct
33 Correct 1270 ms 14896 KB Output is correct
34 Correct 2313 ms 15052 KB Output is correct
35 Correct 1377 ms 13596 KB Output is correct
36 Correct 1298 ms 13096 KB Output is correct
37 Correct 842 ms 15644 KB Output is correct
38 Correct 2190 ms 14712 KB Output is correct