#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 ) {
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;
}
}
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 );
vector<int> ans( q, 0 );
vec.clear(), memset( d, 127, sizeof d ), d[p] = cyc = 0, memset( chk, 0, sizeof chk );
dfs( p );
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 );
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] );
}
}
Compilation message
garden.cpp: In function 'void count_routes(int, int, int, int (*)[2], int, int*)':
garden.cpp:28: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++ ) {
~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 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 |
10 ms |
9856 KB |
Output is correct |
7 |
Correct |
12 ms |
9728 KB |
Output is correct |
8 |
Correct |
10 ms |
9856 KB |
Output is correct |
9 |
Correct |
12 ms |
9856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 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 |
10 ms |
9856 KB |
Output is correct |
7 |
Correct |
12 ms |
9728 KB |
Output is correct |
8 |
Correct |
10 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 |
35 ms |
12236 KB |
Output is correct |
13 |
Correct |
60 ms |
25196 KB |
Output is correct |
14 |
Correct |
100 ms |
18168 KB |
Output is correct |
15 |
Correct |
140 ms |
18296 KB |
Output is correct |
16 |
Correct |
98 ms |
15992 KB |
Output is correct |
17 |
Correct |
98 ms |
15096 KB |
Output is correct |
18 |
Correct |
34 ms |
12288 KB |
Output is correct |
19 |
Correct |
101 ms |
18040 KB |
Output is correct |
20 |
Correct |
152 ms |
18296 KB |
Output is correct |
21 |
Correct |
116 ms |
15992 KB |
Output is correct |
22 |
Correct |
95 ms |
15224 KB |
Output is correct |
23 |
Correct |
112 ms |
18936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 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 |
10 ms |
9856 KB |
Output is correct |
7 |
Correct |
12 ms |
9728 KB |
Output is correct |
8 |
Correct |
10 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 |
35 ms |
12236 KB |
Output is correct |
13 |
Correct |
60 ms |
25196 KB |
Output is correct |
14 |
Correct |
100 ms |
18168 KB |
Output is correct |
15 |
Correct |
140 ms |
18296 KB |
Output is correct |
16 |
Correct |
98 ms |
15992 KB |
Output is correct |
17 |
Correct |
98 ms |
15096 KB |
Output is correct |
18 |
Correct |
34 ms |
12288 KB |
Output is correct |
19 |
Correct |
101 ms |
18040 KB |
Output is correct |
20 |
Correct |
152 ms |
18296 KB |
Output is correct |
21 |
Correct |
116 ms |
15992 KB |
Output is correct |
22 |
Correct |
95 ms |
15224 KB |
Output is correct |
23 |
Correct |
112 ms |
18936 KB |
Output is correct |
24 |
Correct |
11 ms |
9728 KB |
Output is correct |
25 |
Correct |
105 ms |
11264 KB |
Output is correct |
26 |
Correct |
152 ms |
12280 KB |
Output is correct |
27 |
Correct |
2545 ms |
25268 KB |
Output is correct |
28 |
Correct |
911 ms |
18296 KB |
Output is correct |
29 |
Correct |
2941 ms |
18452 KB |
Output is correct |
30 |
Correct |
1702 ms |
15992 KB |
Output is correct |
31 |
Correct |
1783 ms |
15224 KB |
Output is correct |
32 |
Correct |
159 ms |
12408 KB |
Output is correct |
33 |
Correct |
920 ms |
18156 KB |
Output is correct |
34 |
Correct |
2949 ms |
18324 KB |
Output is correct |
35 |
Correct |
1821 ms |
16096 KB |
Output is correct |
36 |
Correct |
2284 ms |
15376 KB |
Output is correct |
37 |
Correct |
738 ms |
19064 KB |
Output is correct |
38 |
Correct |
2766 ms |
29404 KB |
Output is correct |