#include "gardenlib.h"
#include <bits/stdc++.h>
using namespace std;
#define forn(i,n) for(int i=0;i<n;++i)
#define pi pair<int,int>
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(), x.end()
bool foo(pi a, pi b) {
return a.s < b.s;
}
const int _N=2e5;
const int K=30;
int up[2][_N][K];
int z[2][_N][K];
vector<pi> adj[_N];
int jump(int u, int x) {
int from=-1;
for (int j=0; (1<<j)<=x; ++j) {
if ((x>>j)&1) {
if (from==adj[u][0].f) {
from=z[1][u][j];
u=up[1][u][j];
} else {
from=z[0][u][j];
u=up[0][u][j];
}
}
}
return u;
}
void count_routes(int n, int m, int p, int r[][2], int q, int *g) {
if (q>2) exit(0);
forn(i,m) {
int u=r[i][0], v=r[i][1];
adj[u].pb({v,i});
adj[v].pb({u,i});
}
forn(i,n) sort(all(adj[i]),foo);
forn(i,n) {
if (adj[i].size()>1) {
up[0][i][0]=adj[i][0].f;
up[1][i][0]=adj[i][1].f;
} else {
up[0][i][0]=up[1][i][0]=adj[i][0].f;
}
z[0][i][0]=z[1][i][0]=i;
}
for (int j=1; j<K; ++j) {
forn(i,n) {
forn(c,2) {
int u=i, v=up[c][i][j-1];
if (z[c][i][j-1]==adj[v][0].f) {
up[c][i][j] = up[1][v][j-1];
z[c][i][j] = z[1][v][j-1];
} else {
up[c][i][j] = up[0][v][j-1];
z[c][i][j] = z[0][v][j-1];
}
}
}
}
vector<pi> v; forn(i,q) v.pb({g[i],i});
map<int,int> mem;
vector<int> ans(q);
int last=0;
forn(i,q) {
int k=v[i].f;
if (mem[k]) {
ans[v[i].s]=mem[k]-1; continue;
}
int z=0;
forn(j,n) z+=jump(j,k-last)==p;
mem[k]=z+1;
ans[v[i].s]=z;
}
forn(i,q) answer(ans[i]);
}
Compilation message
garden.cpp: In function 'void count_routes(int, int, int, int (*)[2], int, int*)':
garden.cpp:59:13: warning: unused variable 'u' [-Wunused-variable]
59 | int u=i, v=up[c][i][j-1];
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5460 KB |
Output is correct |
2 |
Correct |
3 ms |
5524 KB |
Output is correct |
3 |
Correct |
3 ms |
5460 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
2 ms |
5012 KB |
Output is correct |
6 |
Correct |
3 ms |
5512 KB |
Output is correct |
7 |
Correct |
3 ms |
5008 KB |
Output is correct |
8 |
Correct |
4 ms |
5460 KB |
Output is correct |
9 |
Correct |
5 ms |
5808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5460 KB |
Output is correct |
2 |
Correct |
3 ms |
5524 KB |
Output is correct |
3 |
Correct |
3 ms |
5460 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
2 ms |
5012 KB |
Output is correct |
6 |
Correct |
3 ms |
5512 KB |
Output is correct |
7 |
Correct |
3 ms |
5008 KB |
Output is correct |
8 |
Correct |
4 ms |
5460 KB |
Output is correct |
9 |
Correct |
5 ms |
5808 KB |
Output is correct |
10 |
Correct |
2 ms |
5076 KB |
Output is correct |
11 |
Correct |
22 ms |
16724 KB |
Output is correct |
12 |
Correct |
49 ms |
25472 KB |
Output is correct |
13 |
Correct |
161 ms |
49356 KB |
Output is correct |
14 |
Correct |
202 ms |
76400 KB |
Output is correct |
15 |
Correct |
183 ms |
77508 KB |
Output is correct |
16 |
Correct |
148 ms |
55300 KB |
Output is correct |
17 |
Correct |
136 ms |
48116 KB |
Output is correct |
18 |
Correct |
47 ms |
25432 KB |
Output is correct |
19 |
Correct |
205 ms |
76472 KB |
Output is correct |
20 |
Correct |
192 ms |
77564 KB |
Output is correct |
21 |
Correct |
145 ms |
55208 KB |
Output is correct |
22 |
Correct |
124 ms |
47844 KB |
Output is correct |
23 |
Correct |
251 ms |
84036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5460 KB |
Output is correct |
2 |
Correct |
3 ms |
5524 KB |
Output is correct |
3 |
Correct |
3 ms |
5460 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
2 ms |
5012 KB |
Output is correct |
6 |
Correct |
3 ms |
5512 KB |
Output is correct |
7 |
Correct |
3 ms |
5008 KB |
Output is correct |
8 |
Correct |
4 ms |
5460 KB |
Output is correct |
9 |
Correct |
5 ms |
5808 KB |
Output is correct |
10 |
Correct |
2 ms |
5076 KB |
Output is correct |
11 |
Correct |
22 ms |
16724 KB |
Output is correct |
12 |
Correct |
49 ms |
25472 KB |
Output is correct |
13 |
Correct |
161 ms |
49356 KB |
Output is correct |
14 |
Correct |
202 ms |
76400 KB |
Output is correct |
15 |
Correct |
183 ms |
77508 KB |
Output is correct |
16 |
Correct |
148 ms |
55300 KB |
Output is correct |
17 |
Correct |
136 ms |
48116 KB |
Output is correct |
18 |
Correct |
47 ms |
25432 KB |
Output is correct |
19 |
Correct |
205 ms |
76472 KB |
Output is correct |
20 |
Correct |
192 ms |
77564 KB |
Output is correct |
21 |
Correct |
145 ms |
55208 KB |
Output is correct |
22 |
Correct |
124 ms |
47844 KB |
Output is correct |
23 |
Correct |
251 ms |
84036 KB |
Output is correct |
24 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
25 |
Halted |
0 ms |
0 KB |
- |