#include"garden.h"
#include"gardenlib.h"
#include<iostream>
#include<vector>
#include<unordered_map>
using namespace std;
int n; // num nodes
int m; // num edges
vector<vector<int>>nodes; // lowest node and second lowest node
vector<int>nxt;
vector<int>vis;
vector<int>dist;
int cycle_size;
void add_conn(int node,int dest){
if(nodes[node][0]==-1){
nodes[node]={dest,dest};
}else if(nodes[node][0]==nodes[node][1]){
nodes[node][1]=dest;
}
}
void dfs(int node){
if(vis[nxt[node]]!=-1){
if(vis[nxt[node]]==0)
cycle_size=vis[node]+1;
else
cycle_size=-1;
return;
}
vis[nxt[node]]=vis[node]+1;
dfs(nxt[node]);
}
int get_dist(int node){
if(dist[node]!=-1)
return dist[node];
dist[node]=1e9;
dist[node]=get_dist(nxt[node])+1;
return dist[node];
}
vector<int>get(int Q,int G[],int P){
vis=vector<int>(2*n,-1);
vis[P]=0;
dfs(P);
dist=vector<int>(2*n,-1);
dist[P]=0;
vector<int>res(Q);
if(cycle_size==-1){
unordered_map<int,int>mp;
for(int i=0;i<n;i++)
mp[get_dist(2*i)]++;
for(int i=0;i<Q;i++)
res[i]+=mp[G[i]];
}else{
for(int i=0;i<Q;i++){
for(int j=0;j<n;j++){
int dist=get_dist(2*j);
if(dist<=G[i]&&dist%cycle_size==G[i]%cycle_size){
res[i]++;
}
}
}
}
return res;
}
void count_routes(int N,int M,int P,int R[][2],int Q,int G[]){
n=N;
m=M;
nodes=vector<vector<int>>(n,{-1,-1});
nxt.resize(2*n);
for(int i=0;i<m;i++){
add_conn(R[i][0],R[i][1]);
add_conn(R[i][1],R[i][0]);
}
for(int i=0;i<2*n;i++){
nxt[i]=2*nodes[i/2][i%2];
if(nodes[nxt[i]/2][0]==i/2&&nodes[nxt[i]/2][1]!=-1)
nxt[i]++;
}
vector<int>res1=get(Q,G,2*P);
vector<int>res2=get(Q,G,2*P+1);
for(int i=0;i<Q;i++){
answer(res1[i]+res2[i]);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
448 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
448 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
6 ms |
4780 KB |
Output is correct |
12 |
Correct |
12 ms |
6344 KB |
Output is correct |
13 |
Correct |
24 ms |
13496 KB |
Output is correct |
14 |
Correct |
35 ms |
15940 KB |
Output is correct |
15 |
Correct |
35 ms |
15956 KB |
Output is correct |
16 |
Correct |
29 ms |
12040 KB |
Output is correct |
17 |
Correct |
36 ms |
11000 KB |
Output is correct |
18 |
Correct |
12 ms |
6492 KB |
Output is correct |
19 |
Correct |
38 ms |
15928 KB |
Output is correct |
20 |
Correct |
36 ms |
15940 KB |
Output is correct |
21 |
Correct |
31 ms |
11920 KB |
Output is correct |
22 |
Correct |
30 ms |
10728 KB |
Output is correct |
23 |
Correct |
36 ms |
17340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
448 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
6 ms |
4780 KB |
Output is correct |
12 |
Correct |
12 ms |
6344 KB |
Output is correct |
13 |
Correct |
24 ms |
13496 KB |
Output is correct |
14 |
Correct |
35 ms |
15940 KB |
Output is correct |
15 |
Correct |
35 ms |
15956 KB |
Output is correct |
16 |
Correct |
29 ms |
12040 KB |
Output is correct |
17 |
Correct |
36 ms |
11000 KB |
Output is correct |
18 |
Correct |
12 ms |
6492 KB |
Output is correct |
19 |
Correct |
38 ms |
15928 KB |
Output is correct |
20 |
Correct |
36 ms |
15940 KB |
Output is correct |
21 |
Correct |
31 ms |
11920 KB |
Output is correct |
22 |
Correct |
30 ms |
10728 KB |
Output is correct |
23 |
Correct |
36 ms |
17340 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
137 ms |
4848 KB |
Output is correct |
26 |
Correct |
174 ms |
6536 KB |
Output is correct |
27 |
Correct |
3478 ms |
13752 KB |
Output is correct |
28 |
Correct |
1106 ms |
15948 KB |
Output is correct |
29 |
Correct |
3731 ms |
16140 KB |
Output is correct |
30 |
Correct |
2197 ms |
12008 KB |
Output is correct |
31 |
Correct |
1927 ms |
10836 KB |
Output is correct |
32 |
Correct |
13 ms |
6492 KB |
Output is correct |
33 |
Correct |
1087 ms |
15948 KB |
Output is correct |
34 |
Correct |
3739 ms |
16140 KB |
Output is correct |
35 |
Correct |
2134 ms |
11976 KB |
Output is correct |
36 |
Correct |
1934 ms |
10808 KB |
Output is correct |
37 |
Correct |
904 ms |
17400 KB |
Output is correct |
38 |
Correct |
3117 ms |
19740 KB |
Output is correct |