#include "garden.h"
#include "gardenlib.h"
#include <bits/stdc++.h>
using namespace std;
const int maxn = 150005, maxm = 150005, INF = 1e9;
int n, m, p;
vector<int> adj[maxn];
int vis[maxn][2];
int dist[maxn][2][2];
vector<pair<int,int>> track;
int cyclesz[2] = {INF, INF};
int dfs(int u) {
int id = (track.size()>0 && track.back().first==adj[u][0] && adj[u].size()>1);
// cout << u << " " << id << " " << vis[u][id] << endl;
if (vis[u][id]==-3) return id;
if (vis[u][id]==-2) {
// cout << u << " " << id << endl;
vector<pair<int,int>> c;
reverse(track.begin(), track.end());
for (auto [i, j]:track) {
c.push_back({i, j});
if (i==u && j==id) break;
}
int sz = c.size(), pos = -1;
for (int i=0;i<sz;i++) if (c[i].first==p && c[i].second==0) pos = i;
if (pos!=-1) {
cyclesz[0] = sz;
for (int i=0;i<sz;i++) {
int cur = (i+pos)%sz;
dist[c[cur].first][c[cur].second][0] = i;
}
}
pos = -1;
for (int i=0;i<sz;i++) if (c[i].first==p && c[i].second==1) pos = i;
if (pos!=-1) {
// cout << pos << endl;
cyclesz[1] = sz;
for (int i=0;i<sz;i++) {
int cur = (i+pos)%sz;
dist[c[cur].first][c[cur].second][1] = i;
// cout << "cost " << c[cur].first << " " << c[cur].second << " " << i << endl;
}
}
return id;
}
vis[u][id] = -2;
track.push_back({u, id});
int v = adj[u][id];
int nxt = dfs(v);
for (int i=0;i<=1;i++) {
if (dist[u][id][i]==-1 && dist[v][nxt][i]!=-1) dist[u][id][i] = dist[v][nxt][i] + 1;
}
if (u==p) dist[u][id][id] = 0;
vis[u][id] = -3;
return id;
}
void count_routes(int N, int M, int P, int R[][2], int Q, int G[]) {
n = N, m = M, p = P;
for (int i=0;i<m;i++) {
int u = R[i][0], v = R[i][1];
if (adj[u].size()<2) adj[u].push_back(v);
if (adj[v].size()<2) adj[v].push_back(u);
}
for (int i=0;i<n;i++) vis[i][0] = vis[i][1] = dist[i][0][0] = dist[i][1][0] = dist[i][0][1] = dist[i][1][1] = -1;
for (int i=0;i<n;i++) if (vis[i][0]==-1) {
track.clear();
dfs(i);
}
// cout << cyclesz[0] << " " << cyclesz[1] << endl;
for (int i=0;i<Q;i++) {
int x = G[i];
int32_t ans = 0;
for (int i=0;i<n;i++) {
bool add = false;
for (int j=0;j<=1;j++) {
if (dist[i][0][j]!=-1 && x>=dist[i][0][j] && (x-dist[i][0][j])%cyclesz[j]==0) add = true;
}
ans += add;
}
answer(ans);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
3 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
3 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6488 KB |
Output is correct |
11 |
Correct |
7 ms |
10328 KB |
Output is correct |
12 |
Correct |
10 ms |
11240 KB |
Output is correct |
13 |
Correct |
34 ms |
38152 KB |
Output is correct |
14 |
Correct |
31 ms |
14684 KB |
Output is correct |
15 |
Correct |
35 ms |
14676 KB |
Output is correct |
16 |
Correct |
26 ms |
13548 KB |
Output is correct |
17 |
Correct |
41 ms |
12716 KB |
Output is correct |
18 |
Correct |
11 ms |
10588 KB |
Output is correct |
19 |
Correct |
26 ms |
14696 KB |
Output is correct |
20 |
Correct |
34 ms |
14672 KB |
Output is correct |
21 |
Correct |
34 ms |
13396 KB |
Output is correct |
22 |
Correct |
26 ms |
12624 KB |
Output is correct |
23 |
Correct |
29 ms |
15956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
3 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6488 KB |
Output is correct |
11 |
Correct |
7 ms |
10328 KB |
Output is correct |
12 |
Correct |
10 ms |
11240 KB |
Output is correct |
13 |
Correct |
34 ms |
38152 KB |
Output is correct |
14 |
Correct |
31 ms |
14684 KB |
Output is correct |
15 |
Correct |
35 ms |
14676 KB |
Output is correct |
16 |
Correct |
26 ms |
13548 KB |
Output is correct |
17 |
Correct |
41 ms |
12716 KB |
Output is correct |
18 |
Correct |
11 ms |
10588 KB |
Output is correct |
19 |
Correct |
26 ms |
14696 KB |
Output is correct |
20 |
Correct |
34 ms |
14672 KB |
Output is correct |
21 |
Correct |
34 ms |
13396 KB |
Output is correct |
22 |
Correct |
26 ms |
12624 KB |
Output is correct |
23 |
Correct |
29 ms |
15956 KB |
Output is correct |
24 |
Correct |
2 ms |
6492 KB |
Output is correct |
25 |
Correct |
65 ms |
10332 KB |
Output is correct |
26 |
Correct |
116 ms |
11096 KB |
Output is correct |
27 |
Correct |
685 ms |
38092 KB |
Output is correct |
28 |
Correct |
675 ms |
14728 KB |
Output is correct |
29 |
Correct |
621 ms |
14632 KB |
Output is correct |
30 |
Correct |
361 ms |
13488 KB |
Output is correct |
31 |
Correct |
410 ms |
12768 KB |
Output is correct |
32 |
Correct |
109 ms |
10584 KB |
Output is correct |
33 |
Correct |
702 ms |
14632 KB |
Output is correct |
34 |
Correct |
561 ms |
14844 KB |
Output is correct |
35 |
Correct |
343 ms |
13352 KB |
Output is correct |
36 |
Correct |
865 ms |
12748 KB |
Output is correct |
37 |
Correct |
554 ms |
15952 KB |
Output is correct |
38 |
Correct |
1406 ms |
38576 KB |
Output is correct |