#include<bits/stdc++.h>
#include "garden.h"
#include "gardenlib.h"
#define cyc1 cyc
using namespace std;
const int N = 150010;
vector <array <int, 2>> g[N], gg[N];
int dist[N][2];
int low[N][2];
int nx[N][2];
int back[N][2];
void count_routes(int n, int m, int p, int r[][2], int q, int G[]){
for(int i = 0;i < m;i++){
gg[r[i][0]].push_back({i, r[i][1]});
gg[r[i][1]].push_back({i, r[i][0]});
}
memset(low, -1, sizeof low);
memset(nx, -1, sizeof nx);
for(int i = 0;i < n;i++){
sort(gg[i].begin(), gg[i].end());
for(int j = 0;j < min((int)gg[i].size(), 2);j++){
g[gg[i][j][1]].push_back({gg[i][j][0], i});
low[i][j] = gg[i][j][0];
nx[i][j] = gg[i][j][1];
}
}
for(int i = 0;i < N;i++){
dist[i][0] = dist[i][1] = 1e9+20;
}
dist[p][0] = dist[p][1] = 0;
priority_queue <array <int, 3>> pq;
pq.push({0, p, 0});
pq.push({0, p, 1});
while(!pq.empty()){
auto [d, v, flag] = pq.top();
pq.pop();
d *= -1;
if(d != dist[v][flag]) continue;
for(auto [w, x] : g[v]){
if(w == low[x][0]){
int fg;
if(w == low[v][0]) fg = 1;
else fg = 0;
if(fg != flag) continue;
if(dist[v][flag]+1 < dist[x][0]){
dist[x][0] = dist[v][flag]+1;
if(v == p){
if(w == low[v][0]) back[x][0] = 1;
else back[x][0] = 0;
}
else{
back[x][0] = back[v][flag];
}
pq.push({-dist[x][0], x, 0});
if(low[x][1] == -1){
if(dist[v][flag]+1 < dist[x][1]){
dist[x][1] = dist[v][flag]+1;
if(v == p){
if(w == low[v][0]) back[x][1] = 1;
else back[x][1] = 0;
}
else{
back[x][1] = back[v][flag];
}
pq.push({-dist[x][1], x, 1});
}
}
}
}
else if(w == low[x][1]){
int fg;
if(w == low[v][0]) fg = 1;
else fg = 0;
if(fg != flag) continue;
if(dist[v][flag]+1 < dist[x][1]){
dist[x][1] = dist[v][flag]+1;
if(v == p){
if(w == low[v][0]) back[x][1] = 1;
else back[x][1] = 0;
}
else{
back[x][1] = back[v][flag];
}
pq.push({-dist[x][1], x, 1});
}
}
}
}
for(int i = 0;i < q;i++){
int k = G[i];
int res = 0;
for(int v = 0;v < n;v++){
//cout << res << ' ';
if(dist[v][0] == k) {
res++;
continue;
}
if(k < dist[v][0]) continue;
int t = dist[v][0], b = back[v][0];
if(b == 1){
if(nx[p][1] == -1){
b = 0;
}
}
int cyc=0;
if(b == 0){
int x = nx[p][0];
cyc++;
if(low[p][0] == low[x][0]) cyc += dist[x][1];
else cyc += dist[x][0];
int bb = back[x][(low[p][0] == low[x][0] ? 1 : 0)];
if(bb == 1){
if(nx[p][1] == -1) bb = 0;
}
if(bb == 0){
if(k % cyc == (dist[v][0]) % cyc){
res++;
continue;
}
else continue;
}
else{
int cyc2 = 0;
int xx = nx[p][1];
cyc2++;
if(low[p][1] == low[xx][0]) cyc2 += dist[xx][1];
else cyc2 += dist[xx][0];
int bbb = back[xx][(low[p][1] == low[xx][0] ? 1 : 0)];
if(bbb == 1){
if(k%cyc2 == (dist[v][0] + cyc1)%cyc2){
res++;
continue;
}
}
else{
if(k%(cyc1+cyc2) == (dist[v][0])%(cyc1+cyc2) or k%(cyc1+cyc2) == (dist[v][0]+cyc1)%(cyc1+cyc2)){
res++;
continue;
}
}
}
}
else{
int x = nx[p][1];
cyc++;
if(low[p][1] == low[x][0]) cyc += dist[x][1];
else cyc += dist[x][0];
int bb = back[x][(low[p][1] == low[x][0] ? 1 : 0)];
if(bb == 1){
if(k % cyc == (dist[v][0]) % cyc){
res++;
continue;
}
else continue;
}
else{
int cyc2 = 0;
int xx = nx[p][0];
cyc2++;
if(low[p][0] == low[xx][0]) cyc2 += dist[xx][1];
else cyc2 += dist[xx][0];
int bbb = back[xx][(low[p][0] == low[xx][0] ? 1 : 0)];
if(bbb == 0){
if(k%cyc2 == (dist[v][0] + cyc1)%cyc2){
res++;
continue;
}
}
else{
if(k%(cyc1+cyc2) == (dist[v][0])%(cyc1+cyc2) or k%(cyc1+cyc2) == (dist[v][0]+cyc1)%(cyc1+cyc2)){
res++;
continue;
}
}
}
}
}
answer(res);
}
}
Compilation message
garden.cpp: In function 'void count_routes(int, int, int, int (*)[2], int, int*)':
garden.cpp:102:13: warning: unused variable 't' [-Wunused-variable]
102 | int t = dist[v][0], b = back[v][0];
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
11088 KB |
Output is correct |
2 |
Correct |
8 ms |
11088 KB |
Output is correct |
3 |
Correct |
8 ms |
11088 KB |
Output is correct |
4 |
Correct |
8 ms |
10844 KB |
Output is correct |
5 |
Correct |
9 ms |
10832 KB |
Output is correct |
6 |
Correct |
10 ms |
11088 KB |
Output is correct |
7 |
Correct |
9 ms |
10832 KB |
Output is correct |
8 |
Correct |
9 ms |
11088 KB |
Output is correct |
9 |
Correct |
11 ms |
11344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
11088 KB |
Output is correct |
2 |
Correct |
8 ms |
11088 KB |
Output is correct |
3 |
Correct |
8 ms |
11088 KB |
Output is correct |
4 |
Correct |
8 ms |
10844 KB |
Output is correct |
5 |
Correct |
9 ms |
10832 KB |
Output is correct |
6 |
Correct |
10 ms |
11088 KB |
Output is correct |
7 |
Correct |
9 ms |
10832 KB |
Output is correct |
8 |
Correct |
9 ms |
11088 KB |
Output is correct |
9 |
Correct |
11 ms |
11344 KB |
Output is correct |
10 |
Correct |
10 ms |
10832 KB |
Output is correct |
11 |
Correct |
15 ms |
12736 KB |
Output is correct |
12 |
Correct |
27 ms |
14416 KB |
Output is correct |
13 |
Correct |
35 ms |
18340 KB |
Output is correct |
14 |
Correct |
74 ms |
23112 KB |
Output is correct |
15 |
Correct |
144 ms |
23680 KB |
Output is correct |
16 |
Correct |
82 ms |
19800 KB |
Output is correct |
17 |
Correct |
85 ms |
19528 KB |
Output is correct |
18 |
Correct |
32 ms |
14928 KB |
Output is correct |
19 |
Correct |
83 ms |
23112 KB |
Output is correct |
20 |
Correct |
139 ms |
23644 KB |
Output is correct |
21 |
Correct |
95 ms |
19812 KB |
Output is correct |
22 |
Correct |
81 ms |
19272 KB |
Output is correct |
23 |
Correct |
75 ms |
23880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
11088 KB |
Output is correct |
2 |
Correct |
8 ms |
11088 KB |
Output is correct |
3 |
Correct |
8 ms |
11088 KB |
Output is correct |
4 |
Correct |
8 ms |
10844 KB |
Output is correct |
5 |
Correct |
9 ms |
10832 KB |
Output is correct |
6 |
Correct |
10 ms |
11088 KB |
Output is correct |
7 |
Correct |
9 ms |
10832 KB |
Output is correct |
8 |
Correct |
9 ms |
11088 KB |
Output is correct |
9 |
Correct |
11 ms |
11344 KB |
Output is correct |
10 |
Correct |
10 ms |
10832 KB |
Output is correct |
11 |
Correct |
15 ms |
12736 KB |
Output is correct |
12 |
Correct |
27 ms |
14416 KB |
Output is correct |
13 |
Correct |
35 ms |
18340 KB |
Output is correct |
14 |
Correct |
74 ms |
23112 KB |
Output is correct |
15 |
Correct |
144 ms |
23680 KB |
Output is correct |
16 |
Correct |
82 ms |
19800 KB |
Output is correct |
17 |
Correct |
85 ms |
19528 KB |
Output is correct |
18 |
Correct |
32 ms |
14928 KB |
Output is correct |
19 |
Correct |
83 ms |
23112 KB |
Output is correct |
20 |
Correct |
139 ms |
23644 KB |
Output is correct |
21 |
Correct |
95 ms |
19812 KB |
Output is correct |
22 |
Correct |
81 ms |
19272 KB |
Output is correct |
23 |
Correct |
75 ms |
23880 KB |
Output is correct |
24 |
Correct |
8 ms |
10832 KB |
Output is correct |
25 |
Correct |
77 ms |
13020 KB |
Output is correct |
26 |
Correct |
120 ms |
15056 KB |
Output is correct |
27 |
Correct |
1092 ms |
18524 KB |
Output is correct |
28 |
Correct |
850 ms |
23408 KB |
Output is correct |
29 |
Correct |
1271 ms |
24568 KB |
Output is correct |
30 |
Correct |
844 ms |
21000 KB |
Output is correct |
31 |
Correct |
740 ms |
20752 KB |
Output is correct |
32 |
Correct |
141 ms |
14928 KB |
Output is correct |
33 |
Correct |
837 ms |
22088 KB |
Output is correct |
34 |
Correct |
1304 ms |
23820 KB |
Output is correct |
35 |
Correct |
848 ms |
20928 KB |
Output is correct |
36 |
Correct |
1118 ms |
20468 KB |
Output is correct |
37 |
Correct |
654 ms |
23112 KB |
Output is correct |
38 |
Correct |
1567 ms |
22764 KB |
Output is correct |