#include <bits/stdc++.h>
using namespace std;
void answer(int ans);
void count_routes(int n, int m, int p, int r[][2], int q, int g[]){
vector<vector<pair<int, int>>> adjin(n);
vector<int> f(n), s(n, -1);
vector<vector<int>> adj(2*n);
for(int i=0; i<m; i++){
adjin[r[i][0]].push_back({r[i][1], i});
adjin[r[i][1]].push_back({r[i][0], i});
}
for(int i=0; i<n; i++){
int c1 = 1e9, v1, c2 = 1e9, v2;
for(auto &[v, c]:adjin[i]){
if(c < c1){
c2 = c1; v2 = v1;
c1 = c; v1 = v;
} else if(c < c2){
c2 = c; v2 = v;
}
}
if(c1 != 1e9) f[i] = v1;
if(c2 != 1e9) s[i] = v2;
}
for(int i=0; i<n; i++){
if(f[f[i]] != i || s[f[i]] == -1) adj[f[i]].push_back(i);
else adj[f[i]+n].push_back(i);
if(s[i] != -1){
if(f[s[i]] != i || s[s[i]] == -1) adj[s[i]].push_back(i+n);
else adj[s[i]+n].push_back(i+n);
}
}
int c1 = -1, c2 = -1;
vector<int> dist1(2*n, -1), dist2(2*n, -1);
vector<bool> vis1(2*n), vis2(2*n);
function<void(int, int)> dfs1 = [&](int s, int d){
vis1[s] = 1;
dist1[s] = d;
for(auto &u:adj[s]){
if(u == p) c1 = d+1;
if(!vis1[u]) dfs1(u, d+1);
}
};
function<void(int, int)> dfs2 = [&](int s, int d){
vis2[s] = 1;
dist2[s] = d;
for(auto &u:adj[s]){
if(u == p+n) c2 = d+1;
if(!vis2[u]) dfs2(u, d+1);
}
};
dfs1(p, 0); dfs2(p+n, 0);
for(int t=0; t<q; t++){
int ans = 0;
for(int i=0; i<n; i++){
bool ok = 0;
if(dist1[i] != -1){
if(c1 == -1 && dist1[i] == g[t]) ok = 1;
if(c1 != -1 && g[t] >= dist1[i] && (g[t]-dist1[i])%c1 == 0) ok = 1;
}
if(dist2[i] != -1){
if(c2 == -1 && dist2[i] == g[t]) ok = 1;
if(c2 != -1 && g[t] >= dist2[i] && (g[t]-dist2[i])%c2 == 0) ok = 1;
}
if(ok) ans++;
}
answer(ans);
}
}
Compilation message
garden.cpp: In function 'void count_routes(int, int, int, int (*)[2], int, int*)':
garden.cpp:27:28: warning: 'v2' may be used uninitialized in this function [-Wmaybe-uninitialized]
27 | if(c2 != 1e9) s[i] = v2;
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
416 KB |
Output is correct |
3 |
Correct |
1 ms |
444 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
2 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
3 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
416 KB |
Output is correct |
3 |
Correct |
1 ms |
444 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
2 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
3 ms |
724 KB |
Output is correct |
10 |
Correct |
1 ms |
312 KB |
Output is correct |
11 |
Correct |
10 ms |
4564 KB |
Output is correct |
12 |
Correct |
25 ms |
7740 KB |
Output is correct |
13 |
Correct |
51 ms |
28624 KB |
Output is correct |
14 |
Correct |
108 ms |
25296 KB |
Output is correct |
15 |
Correct |
139 ms |
25804 KB |
Output is correct |
16 |
Correct |
93 ms |
19220 KB |
Output is correct |
17 |
Correct |
80 ms |
16976 KB |
Output is correct |
18 |
Correct |
24 ms |
7772 KB |
Output is correct |
19 |
Correct |
93 ms |
25380 KB |
Output is correct |
20 |
Correct |
130 ms |
25860 KB |
Output is correct |
21 |
Correct |
92 ms |
19128 KB |
Output is correct |
22 |
Correct |
87 ms |
16900 KB |
Output is correct |
23 |
Correct |
108 ms |
27860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
416 KB |
Output is correct |
3 |
Correct |
1 ms |
444 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
2 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
3 ms |
724 KB |
Output is correct |
10 |
Correct |
1 ms |
312 KB |
Output is correct |
11 |
Correct |
10 ms |
4564 KB |
Output is correct |
12 |
Correct |
25 ms |
7740 KB |
Output is correct |
13 |
Correct |
51 ms |
28624 KB |
Output is correct |
14 |
Correct |
108 ms |
25296 KB |
Output is correct |
15 |
Correct |
139 ms |
25804 KB |
Output is correct |
16 |
Correct |
93 ms |
19220 KB |
Output is correct |
17 |
Correct |
80 ms |
16976 KB |
Output is correct |
18 |
Correct |
24 ms |
7772 KB |
Output is correct |
19 |
Correct |
93 ms |
25380 KB |
Output is correct |
20 |
Correct |
130 ms |
25860 KB |
Output is correct |
21 |
Correct |
92 ms |
19128 KB |
Output is correct |
22 |
Correct |
87 ms |
16900 KB |
Output is correct |
23 |
Correct |
108 ms |
27860 KB |
Output is correct |
24 |
Correct |
2 ms |
340 KB |
Output is correct |
25 |
Correct |
81 ms |
4816 KB |
Output is correct |
26 |
Correct |
125 ms |
7900 KB |
Output is correct |
27 |
Correct |
2178 ms |
28828 KB |
Output is correct |
28 |
Correct |
854 ms |
25452 KB |
Output is correct |
29 |
Correct |
2497 ms |
25948 KB |
Output is correct |
30 |
Correct |
1438 ms |
19432 KB |
Output is correct |
31 |
Correct |
1430 ms |
17168 KB |
Output is correct |
32 |
Correct |
124 ms |
7880 KB |
Output is correct |
33 |
Correct |
849 ms |
25440 KB |
Output is correct |
34 |
Correct |
2487 ms |
25928 KB |
Output is correct |
35 |
Correct |
1513 ms |
19300 KB |
Output is correct |
36 |
Correct |
1432 ms |
17144 KB |
Output is correct |
37 |
Correct |
613 ms |
28076 KB |
Output is correct |
38 |
Correct |
1947 ms |
41008 KB |
Output is correct |