#include<bits/stdc++.h>
#include "garden.h"
#include "gardenlib.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MAX = 2 * 150000;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
#define pb push_back
#define sz(x) (int) x.size()
#define fr first
#define sc second
#define mp make_pair
#define all(x) x.begin(), x.end()
#define dbg(x) cerr << #x << ": " << "[ " << x << " ]\n"
int dist[MAX][2], cyc[2];
bool seen[MAX];
vector<int> adj[MAX], rev[MAX];
void dfs(int u, int end, int d, int aux){
dist[u][aux] = d;
seen[u] = true;
for(auto v : rev[u]){
if(v == end){
cyc[aux] = dist[u][aux] + 1;
continue;
}
if(!seen[v])
dfs(v, end, d + 1, aux);
}
}
void count_routes(int n, int m, int p, int r[][2], int q, int qrs[]) {
for(int i = 0; i < m; i++){
int u = r[i][0], v = r[i][1];
if(sz(adj[u]) < 2) adj[u].pb(v); //only 2 edges matter
if(sz(adj[v]) < 2) adj[v].pb(u);
}
vector<int> pai(2 * n, -1); //functional graph
for(int i = 0; i < n; i++){
for(int j = 0; j < min(2, sz(adj[i])); j++){
int v = adj[i][j];
int cur = (adj[v][0] == i) * n + v;
pai[j * n + i] = cur;
}
}
for(int i = n; i < 2 * n; i++)
if(pai[i] == -1) pai[i] = pai[i - n];
for(int i = 0; i < 2 * n; i++)
rev[pai[i]].pb(i);
memset(dist, -1, sizeof(dist));
cyc[0] = cyc[1] = INF;
dfs(p, p, 0, 0);
memset(seen, false, sizeof(seen));
dfs(p + n, p + n, 0, 1);
vector<vector<int>> fdist(2, vector<int>(2 * n + 10));
for(int i = 0; i < 2; i++){
for(int j = 0; j < n; j++){
if(dist[j][i] == -1) continue;
fdist[i][dist[j][i]]++;
}
}
for(int i = 0; i < q; i++){
int k = qrs[i], ans = 0;
for(int j = 0; j < 2; j++){
for(int d = k % cyc[j]; d <= min(n, k); d += cyc[j])
ans += fdist[j][d];
}
answer(ans);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
17236 KB |
Output is correct |
2 |
Correct |
9 ms |
17076 KB |
Output is correct |
3 |
Correct |
11 ms |
17108 KB |
Output is correct |
4 |
Correct |
9 ms |
16980 KB |
Output is correct |
5 |
Correct |
9 ms |
16988 KB |
Output is correct |
6 |
Correct |
9 ms |
17368 KB |
Output is correct |
7 |
Correct |
9 ms |
17040 KB |
Output is correct |
8 |
Correct |
9 ms |
17108 KB |
Output is correct |
9 |
Correct |
10 ms |
17108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
17236 KB |
Output is correct |
2 |
Correct |
9 ms |
17076 KB |
Output is correct |
3 |
Correct |
11 ms |
17108 KB |
Output is correct |
4 |
Correct |
9 ms |
16980 KB |
Output is correct |
5 |
Correct |
9 ms |
16988 KB |
Output is correct |
6 |
Correct |
9 ms |
17368 KB |
Output is correct |
7 |
Correct |
9 ms |
17040 KB |
Output is correct |
8 |
Correct |
9 ms |
17108 KB |
Output is correct |
9 |
Correct |
10 ms |
17108 KB |
Output is correct |
10 |
Correct |
9 ms |
16980 KB |
Output is correct |
11 |
Correct |
16 ms |
19688 KB |
Output is correct |
12 |
Correct |
27 ms |
21204 KB |
Output is correct |
13 |
Correct |
52 ms |
44364 KB |
Output is correct |
14 |
Correct |
90 ms |
31156 KB |
Output is correct |
15 |
Correct |
105 ms |
33056 KB |
Output is correct |
16 |
Correct |
72 ms |
28956 KB |
Output is correct |
17 |
Correct |
69 ms |
27224 KB |
Output is correct |
18 |
Correct |
28 ms |
21696 KB |
Output is correct |
19 |
Correct |
87 ms |
32708 KB |
Output is correct |
20 |
Correct |
108 ms |
33108 KB |
Output is correct |
21 |
Correct |
84 ms |
28776 KB |
Output is correct |
22 |
Correct |
75 ms |
27256 KB |
Output is correct |
23 |
Correct |
93 ms |
34428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
17236 KB |
Output is correct |
2 |
Correct |
9 ms |
17076 KB |
Output is correct |
3 |
Correct |
11 ms |
17108 KB |
Output is correct |
4 |
Correct |
9 ms |
16980 KB |
Output is correct |
5 |
Correct |
9 ms |
16988 KB |
Output is correct |
6 |
Correct |
9 ms |
17368 KB |
Output is correct |
7 |
Correct |
9 ms |
17040 KB |
Output is correct |
8 |
Correct |
9 ms |
17108 KB |
Output is correct |
9 |
Correct |
10 ms |
17108 KB |
Output is correct |
10 |
Correct |
9 ms |
16980 KB |
Output is correct |
11 |
Correct |
16 ms |
19688 KB |
Output is correct |
12 |
Correct |
27 ms |
21204 KB |
Output is correct |
13 |
Correct |
52 ms |
44364 KB |
Output is correct |
14 |
Correct |
90 ms |
31156 KB |
Output is correct |
15 |
Correct |
105 ms |
33056 KB |
Output is correct |
16 |
Correct |
72 ms |
28956 KB |
Output is correct |
17 |
Correct |
69 ms |
27224 KB |
Output is correct |
18 |
Correct |
28 ms |
21696 KB |
Output is correct |
19 |
Correct |
87 ms |
32708 KB |
Output is correct |
20 |
Correct |
108 ms |
33108 KB |
Output is correct |
21 |
Correct |
84 ms |
28776 KB |
Output is correct |
22 |
Correct |
75 ms |
27256 KB |
Output is correct |
23 |
Correct |
93 ms |
34428 KB |
Output is correct |
24 |
Correct |
11 ms |
17092 KB |
Output is correct |
25 |
Correct |
16 ms |
19852 KB |
Output is correct |
26 |
Correct |
27 ms |
21768 KB |
Output is correct |
27 |
Correct |
57 ms |
45356 KB |
Output is correct |
28 |
Correct |
117 ms |
32816 KB |
Output is correct |
29 |
Correct |
124 ms |
33200 KB |
Output is correct |
30 |
Correct |
75 ms |
29048 KB |
Output is correct |
31 |
Correct |
73 ms |
27340 KB |
Output is correct |
32 |
Correct |
28 ms |
21748 KB |
Output is correct |
33 |
Correct |
110 ms |
32852 KB |
Output is correct |
34 |
Correct |
139 ms |
33152 KB |
Output is correct |
35 |
Correct |
77 ms |
28748 KB |
Output is correct |
36 |
Correct |
77 ms |
27296 KB |
Output is correct |
37 |
Correct |
89 ms |
34484 KB |
Output is correct |
38 |
Correct |
93 ms |
52864 KB |
Output is correct |