#include "garden.h"
#include "gardenlib.h"
#include <bits/stdc++.h>
using namespace std;
vector <pair <int, int>> dgraf[1000005];
int parent[1000005];
vector <int> graf[1000005];
bool resenje[1000005];
bool cmp(pair <int, int> a, pair <int, int> b){
return a.second < b.second;
}
int n, m, p, q;
int depth[1000005][2];
int cycle_size[1000005];
bool visited[1000005][2];
void dfs(int v, int koji){
visited[v][koji] = 1;
for(auto c : graf[v]){
if(visited[c][koji]){
int x = v;
queue <int> q;
while(x != c){
q.push(x);
x = parent[x];
if(x == c) break;
}
q.push(c);
int g = q.size();
while(!q.empty()){
cycle_size[q.front()] = g;
q.pop();
}
}
else{
depth[c][koji] = depth[v][koji] + 1;
dfs(c, koji);
}
}
}
void count_routes(int N, int M, int P, int R[][2], int Q, int G[]){
n = N, m = M, p = P, q = Q;
for(int i=0; i<m; i++){
dgraf[R[i][0]].push_back({R[i][1], i});
dgraf[R[i][1]].push_back({R[i][0], i});
}
for(int i=0; i<n; i++){
sort(dgraf[i].begin(), dgraf[i].end(), cmp);
}
for(int i=0; i<n; i++){
if(dgraf[i].size() == 1){
if(dgraf[dgraf[i][0].first][0].first == i){
parent[i] = dgraf[i][0].first + n;
parent[i+n] = dgraf[i][0].first + n;
}
else{
parent[i] = dgraf[i][0].first;
parent[i+n] = dgraf[i][0].first;
}
}
else{
if(dgraf[dgraf[i][0].first][0].first == i) parent[i] = dgraf[i][0].first + n;
else parent[i] = dgraf[i][0].first;
if(dgraf[dgraf[i][1].first][0].first == i) parent[i+n] = dgraf[i][1].first + n;
else parent[i+n] = dgraf[i][1].first;
}
}
for(int i=0; i<2*n; i++){
graf[parent[i]].push_back(i);
}
for(int i=0; i<2*n; i++){
depth[i][0] = depth[i][1] = -1;
}
depth[p][0] = 0;
dfs(p, 0);
depth[p+n][1] = 0;
dfs(p+n, 1);
for(int query=0; query<q; query++){
int k = G[query];
int cnt = 0;
for(int i=0; i<n; i++) resenje[i] = 0;
for(int i=0; i<n; i++){
if(depth[i][0] == -1) continue;
if(depth[i][0] == k){
resenje[i] = 1;
}
else if(cycle_size[p]){
if(k > depth[i][0] && (k-depth[i][0])%cycle_size[p] == 0){
resenje[i] = 1;
//cout << i << " e1 " << endl;
}
}
}
for(int i=0; i<n; i++){
if(depth[i][1] == -1) continue;
if(depth[i][1] == k){
resenje[i] = 1;
}
else if(cycle_size[p+n]){
if(k > depth[i][1] && (k-depth[i][1])%cycle_size[p+n] == 0){
resenje[i] = 1;
}
}
}
for(int i=0; i<n; i++){
cnt += resenje[i];
}
answer(cnt);
}
return;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
47644 KB |
Output is correct |
2 |
Correct |
30 ms |
47428 KB |
Output is correct |
3 |
Correct |
29 ms |
47436 KB |
Output is correct |
4 |
Correct |
29 ms |
47324 KB |
Output is correct |
5 |
Correct |
28 ms |
47304 KB |
Output is correct |
6 |
Correct |
29 ms |
47820 KB |
Output is correct |
7 |
Correct |
27 ms |
47308 KB |
Output is correct |
8 |
Correct |
29 ms |
47424 KB |
Output is correct |
9 |
Correct |
31 ms |
47616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
47644 KB |
Output is correct |
2 |
Correct |
30 ms |
47428 KB |
Output is correct |
3 |
Correct |
29 ms |
47436 KB |
Output is correct |
4 |
Correct |
29 ms |
47324 KB |
Output is correct |
5 |
Correct |
28 ms |
47304 KB |
Output is correct |
6 |
Correct |
29 ms |
47820 KB |
Output is correct |
7 |
Correct |
27 ms |
47308 KB |
Output is correct |
8 |
Correct |
29 ms |
47424 KB |
Output is correct |
9 |
Correct |
31 ms |
47616 KB |
Output is correct |
10 |
Correct |
28 ms |
47300 KB |
Output is correct |
11 |
Correct |
41 ms |
50300 KB |
Output is correct |
12 |
Correct |
57 ms |
52388 KB |
Output is correct |
13 |
Correct |
110 ms |
95288 KB |
Output is correct |
14 |
Correct |
135 ms |
63500 KB |
Output is correct |
15 |
Correct |
179 ms |
63912 KB |
Output is correct |
16 |
Correct |
141 ms |
60812 KB |
Output is correct |
17 |
Correct |
125 ms |
59048 KB |
Output is correct |
18 |
Correct |
61 ms |
52288 KB |
Output is correct |
19 |
Correct |
134 ms |
63572 KB |
Output is correct |
20 |
Correct |
178 ms |
63940 KB |
Output is correct |
21 |
Correct |
164 ms |
60348 KB |
Output is correct |
22 |
Correct |
144 ms |
58996 KB |
Output is correct |
23 |
Correct |
145 ms |
65268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
47644 KB |
Output is correct |
2 |
Correct |
30 ms |
47428 KB |
Output is correct |
3 |
Correct |
29 ms |
47436 KB |
Output is correct |
4 |
Correct |
29 ms |
47324 KB |
Output is correct |
5 |
Correct |
28 ms |
47304 KB |
Output is correct |
6 |
Correct |
29 ms |
47820 KB |
Output is correct |
7 |
Correct |
27 ms |
47308 KB |
Output is correct |
8 |
Correct |
29 ms |
47424 KB |
Output is correct |
9 |
Correct |
31 ms |
47616 KB |
Output is correct |
10 |
Correct |
28 ms |
47300 KB |
Output is correct |
11 |
Correct |
41 ms |
50300 KB |
Output is correct |
12 |
Correct |
57 ms |
52388 KB |
Output is correct |
13 |
Correct |
110 ms |
95288 KB |
Output is correct |
14 |
Correct |
135 ms |
63500 KB |
Output is correct |
15 |
Correct |
179 ms |
63912 KB |
Output is correct |
16 |
Correct |
141 ms |
60812 KB |
Output is correct |
17 |
Correct |
125 ms |
59048 KB |
Output is correct |
18 |
Correct |
61 ms |
52288 KB |
Output is correct |
19 |
Correct |
134 ms |
63572 KB |
Output is correct |
20 |
Correct |
178 ms |
63940 KB |
Output is correct |
21 |
Correct |
164 ms |
60348 KB |
Output is correct |
22 |
Correct |
144 ms |
58996 KB |
Output is correct |
23 |
Correct |
145 ms |
65268 KB |
Output is correct |
24 |
Correct |
29 ms |
47308 KB |
Output is correct |
25 |
Correct |
148 ms |
50372 KB |
Output is correct |
26 |
Correct |
202 ms |
52388 KB |
Output is correct |
27 |
Correct |
2663 ms |
95300 KB |
Output is correct |
28 |
Correct |
1178 ms |
63684 KB |
Output is correct |
29 |
Correct |
3267 ms |
64088 KB |
Output is correct |
30 |
Correct |
1831 ms |
60872 KB |
Output is correct |
31 |
Correct |
1893 ms |
59080 KB |
Output is correct |
32 |
Correct |
199 ms |
52420 KB |
Output is correct |
33 |
Correct |
1158 ms |
63684 KB |
Output is correct |
34 |
Correct |
3262 ms |
64120 KB |
Output is correct |
35 |
Correct |
1947 ms |
60484 KB |
Output is correct |
36 |
Correct |
2466 ms |
59192 KB |
Output is correct |
37 |
Correct |
911 ms |
65348 KB |
Output is correct |
38 |
Correct |
3185 ms |
100832 KB |
Output is correct |