#include <bits/stdc++.h>
#include "gardenlib.h"
#include "garden.h"
using namespace std;
const int N = 3e5 + 10;
int chk[N], d[N], to[N], cyc;
vector<int> vec, g[N];
void dfs( int u ) {
//printf("U %d\n",u);
chk[u] = 1, vec.emplace_back( u );
for( int v : g[u] ) {
if( !chk[v] ) d[v] = d[u] + 1, dfs( v );
else if( chk[v] == 1 ) for( int i = (int)vec.size() - 1 ; i >= 0 ; i-- ) {
cyc++;
if( vec[i] == v ) break;
}
}
chk[u] = 2, vec.pop_back();
}
void count_routes( int n, int m, int p, int r[][2], int q, int G[] ) {
for( int i = 0 ; i < m ; i++ ) {
if( g[r[i][0]].size() < 2 ) g[r[i][0]].emplace_back( r[i][1] );
if( g[r[i][1]].size() < 2 ) g[r[i][1]].emplace_back( r[i][0] );
}
for( int i = 0 ; i < n ; i++ ) for( int j = 0 ; j < g[i].size() ; j++ ) {
int v = g[i][j], now = i + j*n;
if( g[v][0] == i && g[v].size() > 1 ) {
to[now] = v + n;
if( g[i].size() == 1 ) to[i+n] = v + n;
}
else {
to[now] = v;
if( g[i].size() == 1 ) to[i+n] = v + n;
}
//printf("u v : %d %d\n",i,v);
}
/*for( int i = 0 ; i < 2*n ; i++ ) printf("%d ",to[i]);
printf("\n");*/
for( int i = 0 ; i < 2*n ; i++ ) g[i].clear();
for( int i = 0 ; i < 2*n ; i++ ) g[to[i]].emplace_back( i );
/*for( int i = 0 ; i < 2*n ; i++ ) {
printf("%d : ",i);
for( int v : g[i] ) printf("%d ",v);
printf("\n");
}
printf("\n");*/
vector<int> ans( q, 0 );
vec.clear(), memset( d, 127, sizeof d ), d[p] = cyc = 0, memset( chk, 0, sizeof chk );
dfs( p );
//printf("%d\n",cyc);
for( int i = 0 ; i < q ; i++ ) {
for( int j = 0 ; j < n ; j++ ) {
if( d[j] > G[i] ) continue ;
if( cyc ) ans[i] += ( ( G[i] - d[j] ) % cyc == 0 );
else ans[i] += ( d[j] == G[i] );
}
}
vec.clear(), memset( d, 127, sizeof d ), d[p+n] = cyc = 0, memset( chk, 0, sizeof chk );
dfs( p+n );
//printf("%d\n",cyc);
for( int i = 0 ; i < q ; i++ ) {
for( int j = 0 ; j < n ; j++ ) {
if( d[j] > G[i] ) continue ;
if( cyc ) ans[i] += ( ( G[i] - d[j] ) % cyc == 0 );
else ans[i] += ( d[j] == G[i] );
}
answer( ans[i] );
}
//return ans;
}
/*int main()
{
int n, m, p, q, r[100][2], G[100];
scanf("%d %d %d %d",&n,&m,&p,&q);
for( int i = 0 ; i < m ; i++ ) {
scanf("%d %d",&r[i][0],&r[i][1]);
}
for( int i = 0 ; i < q ; i++ ) scanf("%d",&G[i]);
vector<int> ans = count_routes( n, m, p, r, q, G );
for( int i : ans ) printf("%d ",i);
}*/
Compilation message
garden.cpp: In function 'void count_routes(int, int, int, int (*)[2], int, int*)':
garden.cpp:29:55: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for( int i = 0 ; i < n ; i++ ) for( int j = 0 ; j < g[i].size() ; j++ ) {
~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
9856 KB |
Output is correct |
2 |
Correct |
11 ms |
9856 KB |
Output is correct |
3 |
Correct |
11 ms |
9856 KB |
Output is correct |
4 |
Correct |
10 ms |
9728 KB |
Output is correct |
5 |
Correct |
10 ms |
9728 KB |
Output is correct |
6 |
Correct |
11 ms |
9856 KB |
Output is correct |
7 |
Correct |
10 ms |
9728 KB |
Output is correct |
8 |
Correct |
11 ms |
9856 KB |
Output is correct |
9 |
Correct |
12 ms |
9856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
9856 KB |
Output is correct |
2 |
Correct |
11 ms |
9856 KB |
Output is correct |
3 |
Correct |
11 ms |
9856 KB |
Output is correct |
4 |
Correct |
10 ms |
9728 KB |
Output is correct |
5 |
Correct |
10 ms |
9728 KB |
Output is correct |
6 |
Correct |
11 ms |
9856 KB |
Output is correct |
7 |
Correct |
10 ms |
9728 KB |
Output is correct |
8 |
Correct |
11 ms |
9856 KB |
Output is correct |
9 |
Correct |
12 ms |
9856 KB |
Output is correct |
10 |
Correct |
10 ms |
9728 KB |
Output is correct |
11 |
Correct |
20 ms |
11392 KB |
Output is correct |
12 |
Correct |
34 ms |
12280 KB |
Output is correct |
13 |
Correct |
61 ms |
25196 KB |
Output is correct |
14 |
Correct |
108 ms |
18040 KB |
Output is correct |
15 |
Correct |
140 ms |
18296 KB |
Output is correct |
16 |
Correct |
107 ms |
15992 KB |
Output is correct |
17 |
Correct |
92 ms |
15032 KB |
Output is correct |
18 |
Correct |
35 ms |
12280 KB |
Output is correct |
19 |
Correct |
99 ms |
18040 KB |
Output is correct |
20 |
Correct |
149 ms |
18296 KB |
Output is correct |
21 |
Correct |
107 ms |
15992 KB |
Output is correct |
22 |
Correct |
92 ms |
15224 KB |
Output is correct |
23 |
Correct |
112 ms |
19128 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
9856 KB |
Output is correct |
2 |
Correct |
11 ms |
9856 KB |
Output is correct |
3 |
Correct |
11 ms |
9856 KB |
Output is correct |
4 |
Correct |
10 ms |
9728 KB |
Output is correct |
5 |
Correct |
10 ms |
9728 KB |
Output is correct |
6 |
Correct |
11 ms |
9856 KB |
Output is correct |
7 |
Correct |
10 ms |
9728 KB |
Output is correct |
8 |
Correct |
11 ms |
9856 KB |
Output is correct |
9 |
Correct |
12 ms |
9856 KB |
Output is correct |
10 |
Correct |
10 ms |
9728 KB |
Output is correct |
11 |
Correct |
20 ms |
11392 KB |
Output is correct |
12 |
Correct |
34 ms |
12280 KB |
Output is correct |
13 |
Correct |
61 ms |
25196 KB |
Output is correct |
14 |
Correct |
108 ms |
18040 KB |
Output is correct |
15 |
Correct |
140 ms |
18296 KB |
Output is correct |
16 |
Correct |
107 ms |
15992 KB |
Output is correct |
17 |
Correct |
92 ms |
15032 KB |
Output is correct |
18 |
Correct |
35 ms |
12280 KB |
Output is correct |
19 |
Correct |
99 ms |
18040 KB |
Output is correct |
20 |
Correct |
149 ms |
18296 KB |
Output is correct |
21 |
Correct |
107 ms |
15992 KB |
Output is correct |
22 |
Correct |
92 ms |
15224 KB |
Output is correct |
23 |
Correct |
112 ms |
19128 KB |
Output is correct |
24 |
Correct |
11 ms |
9728 KB |
Output is correct |
25 |
Correct |
112 ms |
11264 KB |
Output is correct |
26 |
Correct |
151 ms |
12284 KB |
Output is correct |
27 |
Correct |
2564 ms |
25268 KB |
Output is correct |
28 |
Correct |
992 ms |
18044 KB |
Output is correct |
29 |
Correct |
3017 ms |
18396 KB |
Output is correct |
30 |
Correct |
1735 ms |
16092 KB |
Output is correct |
31 |
Correct |
1821 ms |
15200 KB |
Output is correct |
32 |
Correct |
171 ms |
12284 KB |
Output is correct |
33 |
Correct |
958 ms |
18168 KB |
Output is correct |
34 |
Correct |
2961 ms |
18328 KB |
Output is correct |
35 |
Correct |
1836 ms |
16064 KB |
Output is correct |
36 |
Correct |
2274 ms |
15352 KB |
Output is correct |
37 |
Correct |
735 ms |
19060 KB |
Output is correct |
38 |
Correct |
2794 ms |
29420 KB |
Output is correct |