#include <bits/stdc++.h>
#include "garden.h"
#include "gardenlib.h"
#define maxk 30
#define maxn 150003
#define pb push_back
using namespace std;
int n;
int dp[maxn][2][maxk][2];
vector<int> adj[maxn];
void precalc() {
memset(dp,-1,sizeof(dp));
for( int u = 0 ; u < n ; u++ ) {
int v = adj[u][0];
dp[u][1][0][0] = v;
if(adj[v].size() == 1)
dp[u][1][0][1] = 1;
else
dp[u][1][0][1] = (adj[v][0] != u);
if(adj[u].size() == 2) {
v = adj[u][1];
dp[u][0][0][0] = v;
if(adj[v].size() == 1)
dp[u][0][0][1] = 1;
else
dp[u][0][0][1] = (adj[v][0] != u);
}
}
for( int k = 1 ; k < maxk ; k++ )
for( int u = 0 ; u < n ; u++ )
for( int st = 0 ; st < 2 ; st++ )
if(dp[u][st][k-1][0] != -1) {
int v = dp[u][st][k-1][0];
int st2 = dp[u][st][k-1][1];
dp[u][st][k][0] = dp[v][st2][k-1][0];
dp[u][st][k][1] = dp[v][st2][k-1][1];
}
}
int solve(int u , int d) {
int st = 1;
for( int i = maxk-1 ; i >= 0 ; i-- )
if(d & (1<<i)) {
int v = dp[u][st][i][0];
int stv = dp[u][st][i][1];
u = v;
st = stv;
}
return u;
}
void count_routes(int N, int m, int p, int e[][2], int q, int dist[]) {
n = N;
for( int i = 0 ; i < m ; i++ ) {
int u = e[i][0];
int v = e[i][1];
if(adj[u].size() < 2)
adj[u].pb(v);
if(adj[v].size() < 2)
adj[v].pb(u);
}
precalc();
for( int tc = 0 ; tc < q ; tc++ ) {
int d = dist[tc] , ans = 0;
for( int i = 0 ; i < n ; i++ )
ans += (solve(i,d) == p);
answer(ans);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
74496 KB |
Output is correct |
2 |
Correct |
42 ms |
74368 KB |
Output is correct |
3 |
Correct |
42 ms |
74348 KB |
Output is correct |
4 |
Correct |
42 ms |
74348 KB |
Output is correct |
5 |
Correct |
44 ms |
74348 KB |
Output is correct |
6 |
Correct |
42 ms |
74348 KB |
Output is correct |
7 |
Correct |
41 ms |
74476 KB |
Output is correct |
8 |
Correct |
43 ms |
74348 KB |
Output is correct |
9 |
Correct |
44 ms |
74368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
74496 KB |
Output is correct |
2 |
Correct |
42 ms |
74368 KB |
Output is correct |
3 |
Correct |
42 ms |
74348 KB |
Output is correct |
4 |
Correct |
42 ms |
74348 KB |
Output is correct |
5 |
Correct |
44 ms |
74348 KB |
Output is correct |
6 |
Correct |
42 ms |
74348 KB |
Output is correct |
7 |
Correct |
41 ms |
74476 KB |
Output is correct |
8 |
Correct |
43 ms |
74348 KB |
Output is correct |
9 |
Correct |
44 ms |
74368 KB |
Output is correct |
10 |
Correct |
42 ms |
74348 KB |
Output is correct |
11 |
Correct |
64 ms |
75136 KB |
Output is correct |
12 |
Correct |
92 ms |
76524 KB |
Output is correct |
13 |
Correct |
249 ms |
78700 KB |
Output is correct |
14 |
Correct |
234 ms |
81388 KB |
Output is correct |
15 |
Correct |
233 ms |
81644 KB |
Output is correct |
16 |
Correct |
196 ms |
79852 KB |
Output is correct |
17 |
Correct |
164 ms |
79468 KB |
Output is correct |
18 |
Correct |
91 ms |
76544 KB |
Output is correct |
19 |
Correct |
230 ms |
81388 KB |
Output is correct |
20 |
Correct |
234 ms |
81644 KB |
Output is correct |
21 |
Correct |
189 ms |
79852 KB |
Output is correct |
22 |
Correct |
164 ms |
79488 KB |
Output is correct |
23 |
Correct |
320 ms |
82204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
74496 KB |
Output is correct |
2 |
Correct |
42 ms |
74368 KB |
Output is correct |
3 |
Correct |
42 ms |
74348 KB |
Output is correct |
4 |
Correct |
42 ms |
74348 KB |
Output is correct |
5 |
Correct |
44 ms |
74348 KB |
Output is correct |
6 |
Correct |
42 ms |
74348 KB |
Output is correct |
7 |
Correct |
41 ms |
74476 KB |
Output is correct |
8 |
Correct |
43 ms |
74348 KB |
Output is correct |
9 |
Correct |
44 ms |
74368 KB |
Output is correct |
10 |
Correct |
42 ms |
74348 KB |
Output is correct |
11 |
Correct |
64 ms |
75136 KB |
Output is correct |
12 |
Correct |
92 ms |
76524 KB |
Output is correct |
13 |
Correct |
249 ms |
78700 KB |
Output is correct |
14 |
Correct |
234 ms |
81388 KB |
Output is correct |
15 |
Correct |
233 ms |
81644 KB |
Output is correct |
16 |
Correct |
196 ms |
79852 KB |
Output is correct |
17 |
Correct |
164 ms |
79468 KB |
Output is correct |
18 |
Correct |
91 ms |
76544 KB |
Output is correct |
19 |
Correct |
230 ms |
81388 KB |
Output is correct |
20 |
Correct |
234 ms |
81644 KB |
Output is correct |
21 |
Correct |
189 ms |
79852 KB |
Output is correct |
22 |
Correct |
164 ms |
79488 KB |
Output is correct |
23 |
Correct |
320 ms |
82204 KB |
Output is correct |
24 |
Correct |
53 ms |
74348 KB |
Output is correct |
25 |
Execution timed out |
5056 ms |
75628 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |