#include "garden.h"
#include "gardenlib.h"
#include<vector>
#include<cstdio>
#include<cstring>
constexpr int maxn = 3e5+10;
std::vector<int> g[maxn]; // functional graph reversed
int f[maxn], s[maxn], dist[maxn][2], ans[maxn][2], last[maxn][2], n, sz[2];
bool mark[maxn];
void dfs(int u, int d, int k, int p) {
if(u < n/2) ++dist[d][k];
for(int v : g[u])
if(v != p) dfs(v, d+1, k, p);
}
void dfs2(int u, int k, int p) {
mark[u] = 1; ++sz[k];
if(f[u] == p) return;
if(mark[f[u]]) return (void)(sz[k] = maxn);
dfs2(f[u], k, p);
}
void count_routes(int N, int M, int P, int R[][2], int Q, int G[]) {
n = 2*N;
for(int i = 0; i < n; i++)
f[i] = s[i] = -1;
for(int i = 0; i < M; i++) {
int a = R[i][0], b = R[i][1];
if(f[a] == -1) f[a] = b;
else if(s[a] == -1) s[a] = b;
if(f[b] == -1) f[b] = a;
else if(s[b] == -1) s[b] = a;
}
for(int a = 0; a < N; a++) {
if(s[a] == -1) s[a] = f[a];
if(f[f[a]] == a) g[f[a]+N].push_back(a);
else g[f[a]].push_back(a);
if(f[s[a]] == a) g[s[a]+N].push_back(a+N);
else g[s[a]].push_back(a+N);
}
for(int i = 0; i < n; i++)
for(int v : g[i]) f[v] = i;
dfs(P, 0, 0, P);
dfs(P+N, 0, 1, P+N);
dfs2(P, 0, P);
memset(mark, 0, sizeof mark);
dfs2(P+N, 1, P+N);
for(int b = 0; b <= 1; b++) {
for(int i = 0; i < n; i++) {
ans[i][b] = (i>=sz[b]?ans[i-sz[b]][b]:0) + dist[i][b];
last[i%sz[b]][b] = i;
}
}
for(int i = 0; i < Q; i++) {
int here = 0;
for(int b = 0; b <= 1; b++)
if(sz[b] < maxn) here += ans[std::min(G[i], last[G[i]%sz[b]][b])][b];
else if(G[i] < n) here += dist[G[i]][b];
answer(here);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
7788 KB |
Output is correct |
2 |
Correct |
6 ms |
7788 KB |
Output is correct |
3 |
Correct |
6 ms |
7788 KB |
Output is correct |
4 |
Correct |
6 ms |
7660 KB |
Output is correct |
5 |
Correct |
5 ms |
7660 KB |
Output is correct |
6 |
Correct |
8 ms |
7916 KB |
Output is correct |
7 |
Correct |
6 ms |
7660 KB |
Output is correct |
8 |
Correct |
6 ms |
7788 KB |
Output is correct |
9 |
Correct |
7 ms |
7788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
7788 KB |
Output is correct |
2 |
Correct |
6 ms |
7788 KB |
Output is correct |
3 |
Correct |
6 ms |
7788 KB |
Output is correct |
4 |
Correct |
6 ms |
7660 KB |
Output is correct |
5 |
Correct |
5 ms |
7660 KB |
Output is correct |
6 |
Correct |
8 ms |
7916 KB |
Output is correct |
7 |
Correct |
6 ms |
7660 KB |
Output is correct |
8 |
Correct |
6 ms |
7788 KB |
Output is correct |
9 |
Correct |
7 ms |
7788 KB |
Output is correct |
10 |
Correct |
6 ms |
7660 KB |
Output is correct |
11 |
Correct |
14 ms |
9580 KB |
Output is correct |
12 |
Correct |
28 ms |
10604 KB |
Output is correct |
13 |
Correct |
53 ms |
26348 KB |
Output is correct |
14 |
Correct |
91 ms |
17516 KB |
Output is correct |
15 |
Correct |
125 ms |
17772 KB |
Output is correct |
16 |
Correct |
83 ms |
15212 KB |
Output is correct |
17 |
Correct |
74 ms |
15212 KB |
Output is correct |
18 |
Correct |
27 ms |
11264 KB |
Output is correct |
19 |
Correct |
97 ms |
17688 KB |
Output is correct |
20 |
Correct |
118 ms |
17900 KB |
Output is correct |
21 |
Correct |
91 ms |
16504 KB |
Output is correct |
22 |
Correct |
88 ms |
14060 KB |
Output is correct |
23 |
Correct |
101 ms |
18540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
7788 KB |
Output is correct |
2 |
Correct |
6 ms |
7788 KB |
Output is correct |
3 |
Correct |
6 ms |
7788 KB |
Output is correct |
4 |
Correct |
6 ms |
7660 KB |
Output is correct |
5 |
Correct |
5 ms |
7660 KB |
Output is correct |
6 |
Correct |
8 ms |
7916 KB |
Output is correct |
7 |
Correct |
6 ms |
7660 KB |
Output is correct |
8 |
Correct |
6 ms |
7788 KB |
Output is correct |
9 |
Correct |
7 ms |
7788 KB |
Output is correct |
10 |
Correct |
6 ms |
7660 KB |
Output is correct |
11 |
Correct |
14 ms |
9580 KB |
Output is correct |
12 |
Correct |
28 ms |
10604 KB |
Output is correct |
13 |
Correct |
53 ms |
26348 KB |
Output is correct |
14 |
Correct |
91 ms |
17516 KB |
Output is correct |
15 |
Correct |
125 ms |
17772 KB |
Output is correct |
16 |
Correct |
83 ms |
15212 KB |
Output is correct |
17 |
Correct |
74 ms |
15212 KB |
Output is correct |
18 |
Correct |
27 ms |
11264 KB |
Output is correct |
19 |
Correct |
97 ms |
17688 KB |
Output is correct |
20 |
Correct |
118 ms |
17900 KB |
Output is correct |
21 |
Correct |
91 ms |
16504 KB |
Output is correct |
22 |
Correct |
88 ms |
14060 KB |
Output is correct |
23 |
Correct |
101 ms |
18540 KB |
Output is correct |
24 |
Correct |
7 ms |
7804 KB |
Output is correct |
25 |
Correct |
15 ms |
9580 KB |
Output is correct |
26 |
Correct |
27 ms |
10604 KB |
Output is correct |
27 |
Correct |
54 ms |
26476 KB |
Output is correct |
28 |
Correct |
99 ms |
17644 KB |
Output is correct |
29 |
Correct |
125 ms |
17772 KB |
Output is correct |
30 |
Correct |
88 ms |
15212 KB |
Output is correct |
31 |
Correct |
75 ms |
15212 KB |
Output is correct |
32 |
Correct |
28 ms |
11244 KB |
Output is correct |
33 |
Correct |
91 ms |
17644 KB |
Output is correct |
34 |
Correct |
117 ms |
17772 KB |
Output is correct |
35 |
Correct |
98 ms |
16492 KB |
Output is correct |
36 |
Correct |
82 ms |
15212 KB |
Output is correct |
37 |
Correct |
93 ms |
18540 KB |
Output is correct |
38 |
Correct |
88 ms |
32364 KB |
Output is correct |