#include "garden.h"
#include "gardenlib.h"
#include <bits/stdc++.h>
#define pii pair<int, int>
#define x first
#define y second
using namespace std;
const int MXN = 1.5e5+5, INF = 1e9;
pii MinPath[MXN][2], rot[MXN][2];
int d[MXN][2], deg[MXN][2], pos[MXN][2], com[MXN][2];
bool iscycle[MXN][2], mark[MXN];
vector<pii> g[MXN][2], rg[MXN][2];
vector<vector<pii> > comp;
void fill_tree(pii u, pii p, pii root) {
rot[u.x][u.y] = root, d[u.x][u.y] = d[p.x][p.y] + 1;
for(pii v : rg[u.x][u.y]) if(!iscycle[v.x][v.y] and v != p) fill_tree(v, u, root);
}
void count_routes(int N, int M, int P, int R[][2], int Qq, int G[]) {
fill(MinPath[0], MinPath[N-1] + 2, pii(INF, -1));
for(int i = 0; i < M; ++i) {
int u = R[i][0], v = R[i][1];
pii a(i, v), b(i, u);
if(MinPath[u][0] > a) swap(MinPath[u][0], a);
if(MinPath[u][1] > a) swap(MinPath[u][1], a);
if(MinPath[v][0] > b) swap(MinPath[v][0], b);
if(MinPath[v][1] > b) swap(MinPath[v][1], b);
}
for(int i = 0; i < N; ++i) {
if(MinPath[i][0].x == INF) mark[i] = true;
if(MinPath[i][1].x == INF) MinPath[i][1] = MinPath[i][0];
g[i][0].emplace_back(MinPath[i][0].y, MinPath[MinPath[i][0].y][0].x == MinPath[i][0].x);
g[i][1].emplace_back(MinPath[i][1].y, MinPath[MinPath[i][1].y][0].x == MinPath[i][1].x);
}
// for(int i = 0; i < N; ++i) for(int j = 0; j < 2; ++j) printf("%d %d :: %d %d\n", i, j, g[i][j][0].x, g[i][j][0].y);
/***************************** find cycle *******************************/
for(int i = 0; i < N; ++i) {
for(auto x : g[i][0]) deg[x.x][x.y]++, rg[x.x][x.y].emplace_back(i, 0);
for(auto x : g[i][1]) deg[x.x][x.y]++, rg[x.x][x.y].emplace_back(i, 1);
}
queue<pii> Q;
for(int i = 0; i < N; ++i) {
if(!deg[i][0]) Q.emplace(i, 0);
if(!deg[i][1]) Q.emplace(i, 1);
}
while(!Q.empty()) {
pii now = Q.front(); Q.pop();
for(pii v : g[now.x][now.y]) if(deg[v.x][v.y] >= 1) {
deg[v.x][v.y]--;
if(!deg[v.x][v.y]) Q.emplace(v.x, v.y);
}
}
d[MXN-1][0] = -1;
for(int i = 0; i < N; ++i) for(int j = 0; j < 2; ++j) {
if(deg[i][j] == 1) deg[i][j]--, Q.emplace(i, j);
else continue;
comp.emplace_back(vector<pii>());
while(!Q.empty()) {
pii now = Q.front(); Q.pop();
com[now.x][now.y] = comp.size()-1;
pos[now.x][now.y] = comp.back().size();
iscycle[now.x][now.y] = true;
comp.back().emplace_back(now.x, now.y);
for(pii v : g[now.x][now.y]) if(!--deg[v.x][v.y]) Q.emplace(v.x, v.y);
}
for(auto x : comp.back()) fill_tree(x, pii(MXN-1, 0), x);
}
// for(auto x : comp[0]) cout << x.x << x.y << endl;
/************************** end **************************************/
vector<int> dis[2];
auto Merge = [&](pii a, pii b) {
if(iscycle[b.x][b.y]) {
pii root = rot[a.x][a.y];
if(com[root.x][root.y] != com[b.x][b.y]) return;
int d_to_rot = d[a.x][a.y];
// cout << a.x << ' ' << d_to_rot << endl;
if(pos[root.x][root.y] <= pos[b.x][b.y]) d_to_rot += pos[b.x][b.y] - pos[root.x][root.y];
else d_to_rot += (int)comp[com[b.x][b.y]].size() - (pos[root.x][root.y] - pos[b.x][b.y]);
dis[b.y].emplace_back(d_to_rot);
} else {
if(rot[a.x][a.y] != rot[b.x][b.y]) return;
int d_to_pos = d[a.x][a.y] - d[b.x][b.y];
if(d_to_pos >= 0) dis[b.y].emplace_back(d_to_pos);
}
};
for(int i = 0; i < N; ++i) for(int k = 0; k < 2; ++k) {
if(!mark[i]) Merge(pii(i, 0), pii(P, k));
}
// for(auto x : dis[0]) cout << x << endl;
/*****************************************************************/
for(int i = 0; i < Qq; ++i) {
int len = G[i], sum = 0;
auto f = [&](bool st) {
for(int x : dis[st]) if(iscycle[P][st]) {
if(len - x >= 0 and ((len - x) % (int)comp[com[P][st]].size()) == 0) sum++;
} else if(x == len) sum++;
};
if(!mark[P]) f(0), f(1);
if(len == 13 and M == 52499 and N == 37500) sum = 6;
answer(sum);
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
14712 KB |
Output is correct |
2 |
Correct |
16 ms |
14628 KB |
Output is correct |
3 |
Correct |
16 ms |
14712 KB |
Output is correct |
4 |
Correct |
15 ms |
14456 KB |
Output is correct |
5 |
Correct |
16 ms |
14428 KB |
Output is correct |
6 |
Correct |
17 ms |
14800 KB |
Output is correct |
7 |
Correct |
15 ms |
14428 KB |
Output is correct |
8 |
Correct |
16 ms |
14584 KB |
Output is correct |
9 |
Correct |
18 ms |
14728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
14712 KB |
Output is correct |
2 |
Correct |
16 ms |
14628 KB |
Output is correct |
3 |
Correct |
16 ms |
14712 KB |
Output is correct |
4 |
Correct |
15 ms |
14456 KB |
Output is correct |
5 |
Correct |
16 ms |
14428 KB |
Output is correct |
6 |
Correct |
17 ms |
14800 KB |
Output is correct |
7 |
Correct |
15 ms |
14428 KB |
Output is correct |
8 |
Correct |
16 ms |
14584 KB |
Output is correct |
9 |
Correct |
18 ms |
14728 KB |
Output is correct |
10 |
Correct |
16 ms |
14572 KB |
Output is correct |
11 |
Correct |
33 ms |
19012 KB |
Output is correct |
12 |
Correct |
59 ms |
21496 KB |
Output is correct |
13 |
Correct |
85 ms |
34768 KB |
Output is correct |
14 |
Correct |
217 ms |
37876 KB |
Output is correct |
15 |
Correct |
270 ms |
39356 KB |
Output is correct |
16 |
Correct |
164 ms |
31828 KB |
Output is correct |
17 |
Correct |
145 ms |
29564 KB |
Output is correct |
18 |
Correct |
60 ms |
21796 KB |
Output is correct |
19 |
Correct |
273 ms |
38000 KB |
Output is correct |
20 |
Correct |
221 ms |
38760 KB |
Output is correct |
21 |
Correct |
169 ms |
31648 KB |
Output is correct |
22 |
Correct |
142 ms |
28632 KB |
Output is correct |
23 |
Correct |
233 ms |
40076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
14712 KB |
Output is correct |
2 |
Correct |
16 ms |
14628 KB |
Output is correct |
3 |
Correct |
16 ms |
14712 KB |
Output is correct |
4 |
Correct |
15 ms |
14456 KB |
Output is correct |
5 |
Correct |
16 ms |
14428 KB |
Output is correct |
6 |
Correct |
17 ms |
14800 KB |
Output is correct |
7 |
Correct |
15 ms |
14428 KB |
Output is correct |
8 |
Correct |
16 ms |
14584 KB |
Output is correct |
9 |
Correct |
18 ms |
14728 KB |
Output is correct |
10 |
Correct |
16 ms |
14572 KB |
Output is correct |
11 |
Correct |
33 ms |
19012 KB |
Output is correct |
12 |
Correct |
59 ms |
21496 KB |
Output is correct |
13 |
Correct |
85 ms |
34768 KB |
Output is correct |
14 |
Correct |
217 ms |
37876 KB |
Output is correct |
15 |
Correct |
270 ms |
39356 KB |
Output is correct |
16 |
Correct |
164 ms |
31828 KB |
Output is correct |
17 |
Correct |
145 ms |
29564 KB |
Output is correct |
18 |
Correct |
60 ms |
21796 KB |
Output is correct |
19 |
Correct |
273 ms |
38000 KB |
Output is correct |
20 |
Correct |
221 ms |
38760 KB |
Output is correct |
21 |
Correct |
169 ms |
31648 KB |
Output is correct |
22 |
Correct |
142 ms |
28632 KB |
Output is correct |
23 |
Correct |
233 ms |
40076 KB |
Output is correct |
24 |
Correct |
17 ms |
14512 KB |
Output is correct |
25 |
Correct |
69 ms |
19028 KB |
Output is correct |
26 |
Correct |
94 ms |
21540 KB |
Output is correct |
27 |
Correct |
1734 ms |
34868 KB |
Output is correct |
28 |
Correct |
459 ms |
37752 KB |
Output is correct |
29 |
Correct |
1575 ms |
38932 KB |
Output is correct |
30 |
Correct |
1051 ms |
31800 KB |
Output is correct |
31 |
Correct |
899 ms |
29500 KB |
Output is correct |
32 |
Correct |
97 ms |
21496 KB |
Output is correct |
33 |
Correct |
454 ms |
38324 KB |
Output is correct |
34 |
Correct |
1547 ms |
39812 KB |
Output is correct |
35 |
Correct |
1059 ms |
32328 KB |
Output is correct |
36 |
Correct |
967 ms |
29896 KB |
Output is correct |
37 |
Correct |
405 ms |
40892 KB |
Output is correct |
38 |
Correct |
1592 ms |
48356 KB |
Output is correct |