#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include "garden.h"
#include "gardenlib.h"
using namespace std;
#define INF 1145141919
#define rep(i, n) for (int i=0; i<(n); i++)
#define _1 first
#define _2 second
#define all(x) x.begin(), x.end()
#define pb push_back
typedef pair<int, int> P;
int N, M, T, Q;
vector<P> G[150000];
vector<int> G2[150000*2];
int best[150000];
int F[30][150000*2];
int A[2000];
int D[150000*2];
int C[2][150000*2];
inline int fg(P p) {
int x = p._2, e = p._1;
if (best[x] == e) return x+N;
else return x;
}
inline int go(int x, int s) {
for (int k=29; k>=0; k--) if ((s>>k)&1) x = F[k][x];
return x;
}
int bfs(int s) {
queue<int> q;
rep(i, 2*N) D[i] = INF;
D[s] = 0;
q.push(s);
int ret = INF;
while (!q.empty()) {
int x = q.front(); q.pop();
for (int t : G2[x]) {
if (t == s) ret = min(ret, D[x]+1);
if (D[t] > D[x]+1) {
D[t] = D[x]+1;
q.push(t);
}
}
}
return ret;
}
void count_routes(int n, int m, int p, int edges[][2], int q, int g[]) {
N = n, M = m, T = p, Q = q;
rep(i, N) G[i].clear();
rep(i, M) {
G[edges[i][0]].pb(P(i, edges[i][1]));
G[edges[i][1]].pb(P(i, edges[i][0]));
}
rep(i, N) best[i] = G[i][0]._1;
rep(i, N) {
F[0][i] = fg(G[i][0]);
F[0][i+N] = fg((G[i].size()>=2)? G[i][1] : G[i][0]);
}
rep(i, 2*N) G2[i].clear();
rep(i, 2*N) G2[F[0][i]].pb(i);
rep(k, 29) rep(i, 2*N) F[k+1][i] = F[k][F[k][i]];
vector<int> S[2];
int r[2];
r[0] = bfs(T ); rep(i, N) S[0].pb(D[i]); sort(all(S[0]), greater<int>());
r[1] = bfs(T+N); rep(i, N) S[1].pb(D[i]); sort(all(S[1]), greater<int>());
rep(i, 2) rep(t, 2*N) C[i][t] = 0;
vector<P> qs;
rep(i, Q) qs.pb(P(g[i], i));
sort(all(qs));
rep(_, Q) {
int k = qs[_]._1, q = qs[_]._2;
int ctr = 0;
rep(i, 2) {
while (!S[i].empty() && S[i].back() <= k) {
int t = S[i].back();
if (r[i] != INF) C[i][t%r[i]]++;
else if (t == k) ctr++;
S[i].pop_back();
}
}
rep(i, 2) if (r[i] != INF) ctr += C[i][k%r[i]];
A[q] = ctr;
}
rep(i, Q) answer(A[i]);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
11452 KB |
Output is correct |
2 |
Correct |
19 ms |
11424 KB |
Output is correct |
3 |
Correct |
16 ms |
11464 KB |
Output is correct |
4 |
Correct |
28 ms |
11168 KB |
Output is correct |
5 |
Correct |
15 ms |
11128 KB |
Output is correct |
6 |
Correct |
14 ms |
11600 KB |
Output is correct |
7 |
Correct |
12 ms |
11128 KB |
Output is correct |
8 |
Correct |
13 ms |
11512 KB |
Output is correct |
9 |
Correct |
19 ms |
11740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
11452 KB |
Output is correct |
2 |
Correct |
19 ms |
11424 KB |
Output is correct |
3 |
Correct |
16 ms |
11464 KB |
Output is correct |
4 |
Correct |
28 ms |
11168 KB |
Output is correct |
5 |
Correct |
15 ms |
11128 KB |
Output is correct |
6 |
Correct |
14 ms |
11600 KB |
Output is correct |
7 |
Correct |
12 ms |
11128 KB |
Output is correct |
8 |
Correct |
13 ms |
11512 KB |
Output is correct |
9 |
Correct |
19 ms |
11740 KB |
Output is correct |
10 |
Correct |
13 ms |
11232 KB |
Output is correct |
11 |
Correct |
34 ms |
19548 KB |
Output is correct |
12 |
Correct |
78 ms |
25324 KB |
Output is correct |
13 |
Correct |
94 ms |
43896 KB |
Output is correct |
14 |
Correct |
188 ms |
60084 KB |
Output is correct |
15 |
Correct |
234 ms |
60852 KB |
Output is correct |
16 |
Correct |
176 ms |
46152 KB |
Output is correct |
17 |
Correct |
162 ms |
41380 KB |
Output is correct |
18 |
Correct |
57 ms |
25308 KB |
Output is correct |
19 |
Correct |
186 ms |
60092 KB |
Output is correct |
20 |
Correct |
228 ms |
60684 KB |
Output is correct |
21 |
Correct |
174 ms |
46148 KB |
Output is correct |
22 |
Correct |
155 ms |
40004 KB |
Output is correct |
23 |
Correct |
194 ms |
63240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
11452 KB |
Output is correct |
2 |
Correct |
19 ms |
11424 KB |
Output is correct |
3 |
Correct |
16 ms |
11464 KB |
Output is correct |
4 |
Correct |
28 ms |
11168 KB |
Output is correct |
5 |
Correct |
15 ms |
11128 KB |
Output is correct |
6 |
Correct |
14 ms |
11600 KB |
Output is correct |
7 |
Correct |
12 ms |
11128 KB |
Output is correct |
8 |
Correct |
13 ms |
11512 KB |
Output is correct |
9 |
Correct |
19 ms |
11740 KB |
Output is correct |
10 |
Correct |
13 ms |
11232 KB |
Output is correct |
11 |
Correct |
34 ms |
19548 KB |
Output is correct |
12 |
Correct |
78 ms |
25324 KB |
Output is correct |
13 |
Correct |
94 ms |
43896 KB |
Output is correct |
14 |
Correct |
188 ms |
60084 KB |
Output is correct |
15 |
Correct |
234 ms |
60852 KB |
Output is correct |
16 |
Correct |
176 ms |
46152 KB |
Output is correct |
17 |
Correct |
162 ms |
41380 KB |
Output is correct |
18 |
Correct |
57 ms |
25308 KB |
Output is correct |
19 |
Correct |
186 ms |
60092 KB |
Output is correct |
20 |
Correct |
228 ms |
60684 KB |
Output is correct |
21 |
Correct |
174 ms |
46148 KB |
Output is correct |
22 |
Correct |
155 ms |
40004 KB |
Output is correct |
23 |
Correct |
194 ms |
63240 KB |
Output is correct |
24 |
Correct |
13 ms |
11128 KB |
Output is correct |
25 |
Correct |
33 ms |
19420 KB |
Output is correct |
26 |
Correct |
64 ms |
25336 KB |
Output is correct |
27 |
Correct |
97 ms |
42928 KB |
Output is correct |
28 |
Correct |
194 ms |
58528 KB |
Output is correct |
29 |
Correct |
221 ms |
59096 KB |
Output is correct |
30 |
Correct |
177 ms |
44652 KB |
Output is correct |
31 |
Correct |
159 ms |
39888 KB |
Output is correct |
32 |
Correct |
60 ms |
24748 KB |
Output is correct |
33 |
Correct |
197 ms |
58508 KB |
Output is correct |
34 |
Correct |
229 ms |
59940 KB |
Output is correct |
35 |
Correct |
183 ms |
45244 KB |
Output is correct |
36 |
Correct |
170 ms |
40532 KB |
Output is correct |
37 |
Correct |
193 ms |
63416 KB |
Output is correct |
38 |
Correct |
196 ms |
66792 KB |
Output is correct |