#include "garden.h"
#include "gardenlib.h"
#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<pair<int,int>> graph[150005];
vector<int> adj[300005],cycle;
int best[150005],outgoing[300005],deg[300005],sizes[300005];
int dist0[150005],dist1[150005],peri0[150005],peri1[150005];
bool vis[300005];
void build(int N, int M, int R[][2]){
for(int i = 0;i<M;++i){
best[R[i][0]] = min(best[R[i][0]],i);
best[R[i][1]] = min(best[R[i][1]],i);
graph[R[i][0]].push_back({R[i][1],i});
graph[R[i][1]].push_back({R[i][0],i});
}
for(int i = 0;i<N;++i){
if(best[graph[i][0].first] == graph[i][0].second) outgoing[2*i] = 2*graph[i][0].first+1;
else outgoing[2*i] = 2*graph[i][0].first;
if(graph[i].size() == 1) outgoing[2*i+1] = outgoing[2*i];
else{
if(best[graph[i][1].first] == graph[i][1].second) outgoing[2*i+1] = 2*graph[i][1].first+1;
else outgoing[2*i+1] = 2*graph[i][1].first;
}
}
}
void calcdist(int src, int* dist, int* peri){
memset(vis,false,sizeof(vis));
queue<int> q,dq,pq; q.push(src); vis[src] = true;
dq.push(0); pq.push(-1);
while(!q.empty()){
int u = q.front(); q.pop();
int curdist = dq.front(); dq.pop();
int curper = pq.front(); pq.pop();
curper = max(curper,sizes[u]);
if(u % 2 == 0){
dist[u/2] = curdist;
peri[u/2] = curper;
}
for(auto v : adj[u]){
if(!vis[v]){
vis[v] = true;
q.push(v); dq.push(curdist+1);
pq.push(curper);
}
}
}
}
void count_routes(int N, int M, int P, int R[][2], int Q, int G[]){
memset(best,0x3f,sizeof(best));
memset(sizes,-1,sizeof(sizes));
memset(peri0,-1,sizeof(peri0));
memset(peri1,-1,sizeof(peri1));
memset(dist0,-1,sizeof(dist0));
memset(dist1,-1,sizeof(dist1));
build(N,M,R);
for(int i = 0;i<2*N;++i){
deg[outgoing[i]]++;
deg[i]++;
}
queue<int> q;
for(int i = 0;i<2*N;++i){
if(deg[i] == 1) q.push(i);
}
while(!q.empty()){
int u = q.front(); q.pop();
deg[u]--; deg[outgoing[u]]--;
if(deg[outgoing[u]] == 1){
q.push(outgoing[u]);
}
}
for(int i = 0;i<2*N;++i){
adj[outgoing[i]].push_back(i);
if(deg[i]){
cycle.clear(); int v = i;
while(deg[v]){
deg[v] = 0; cycle.push_back(v);
v = outgoing[v];
}
for(auto c : cycle) sizes[c] = (int)cycle.size();
}
}
calcdist(2*P,dist0,peri0); calcdist(2*P+1,dist1,peri1);
for(int i = 0;i<Q;++i){
int K = G[i], cnt = 0;
for(int j = 0;j<N;++j){
if(dist0[j] != -1){
if(peri0[j] == -1){
if(dist0[j] == K){
cnt++;
continue;
}
}
else{
if((K-dist0[j])%peri0[j] == 0 && dist0[j] <= K){
cnt++;
continue;
}
}
}
if(dist1[j] != -1){
if(peri1[j] == -1){
if(dist1[j] == K){
cnt++;
continue;
}
}
else{
if((K-dist1[j])%peri1[j] == 0 && dist1[j] <= K){
cnt++;
continue;
}
}
}
}
answer(cnt);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
15316 KB |
Output is correct |
2 |
Correct |
10 ms |
15380 KB |
Output is correct |
3 |
Correct |
9 ms |
15372 KB |
Output is correct |
4 |
Correct |
8 ms |
15236 KB |
Output is correct |
5 |
Correct |
8 ms |
15316 KB |
Output is correct |
6 |
Correct |
9 ms |
15444 KB |
Output is correct |
7 |
Correct |
8 ms |
15240 KB |
Output is correct |
8 |
Correct |
8 ms |
15316 KB |
Output is correct |
9 |
Correct |
10 ms |
15700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
15316 KB |
Output is correct |
2 |
Correct |
10 ms |
15380 KB |
Output is correct |
3 |
Correct |
9 ms |
15372 KB |
Output is correct |
4 |
Correct |
8 ms |
15236 KB |
Output is correct |
5 |
Correct |
8 ms |
15316 KB |
Output is correct |
6 |
Correct |
9 ms |
15444 KB |
Output is correct |
7 |
Correct |
8 ms |
15240 KB |
Output is correct |
8 |
Correct |
8 ms |
15316 KB |
Output is correct |
9 |
Correct |
10 ms |
15700 KB |
Output is correct |
10 |
Correct |
8 ms |
15316 KB |
Output is correct |
11 |
Correct |
20 ms |
17792 KB |
Output is correct |
12 |
Correct |
38 ms |
19844 KB |
Output is correct |
13 |
Correct |
46 ms |
26832 KB |
Output is correct |
14 |
Correct |
92 ms |
29812 KB |
Output is correct |
15 |
Correct |
107 ms |
30376 KB |
Output is correct |
16 |
Correct |
67 ms |
26904 KB |
Output is correct |
17 |
Correct |
67 ms |
26224 KB |
Output is correct |
18 |
Correct |
32 ms |
19896 KB |
Output is correct |
19 |
Correct |
96 ms |
29900 KB |
Output is correct |
20 |
Correct |
97 ms |
30440 KB |
Output is correct |
21 |
Correct |
69 ms |
26828 KB |
Output is correct |
22 |
Correct |
64 ms |
26060 KB |
Output is correct |
23 |
Correct |
84 ms |
31188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
15316 KB |
Output is correct |
2 |
Correct |
10 ms |
15380 KB |
Output is correct |
3 |
Correct |
9 ms |
15372 KB |
Output is correct |
4 |
Correct |
8 ms |
15236 KB |
Output is correct |
5 |
Correct |
8 ms |
15316 KB |
Output is correct |
6 |
Correct |
9 ms |
15444 KB |
Output is correct |
7 |
Correct |
8 ms |
15240 KB |
Output is correct |
8 |
Correct |
8 ms |
15316 KB |
Output is correct |
9 |
Correct |
10 ms |
15700 KB |
Output is correct |
10 |
Correct |
8 ms |
15316 KB |
Output is correct |
11 |
Correct |
20 ms |
17792 KB |
Output is correct |
12 |
Correct |
38 ms |
19844 KB |
Output is correct |
13 |
Correct |
46 ms |
26832 KB |
Output is correct |
14 |
Correct |
92 ms |
29812 KB |
Output is correct |
15 |
Correct |
107 ms |
30376 KB |
Output is correct |
16 |
Correct |
67 ms |
26904 KB |
Output is correct |
17 |
Correct |
67 ms |
26224 KB |
Output is correct |
18 |
Correct |
32 ms |
19896 KB |
Output is correct |
19 |
Correct |
96 ms |
29900 KB |
Output is correct |
20 |
Correct |
97 ms |
30440 KB |
Output is correct |
21 |
Correct |
69 ms |
26828 KB |
Output is correct |
22 |
Correct |
64 ms |
26060 KB |
Output is correct |
23 |
Correct |
84 ms |
31188 KB |
Output is correct |
24 |
Correct |
9 ms |
15340 KB |
Output is correct |
25 |
Correct |
71 ms |
17880 KB |
Output is correct |
26 |
Correct |
94 ms |
19944 KB |
Output is correct |
27 |
Correct |
2054 ms |
26980 KB |
Output is correct |
28 |
Correct |
805 ms |
29984 KB |
Output is correct |
29 |
Correct |
2360 ms |
30492 KB |
Output is correct |
30 |
Correct |
1357 ms |
27000 KB |
Output is correct |
31 |
Correct |
1341 ms |
26280 KB |
Output is correct |
32 |
Correct |
126 ms |
19916 KB |
Output is correct |
33 |
Correct |
773 ms |
29980 KB |
Output is correct |
34 |
Correct |
2353 ms |
30488 KB |
Output is correct |
35 |
Correct |
1401 ms |
26948 KB |
Output is correct |
36 |
Correct |
1337 ms |
26160 KB |
Output is correct |
37 |
Correct |
538 ms |
31236 KB |
Output is correct |
38 |
Correct |
1847 ms |
35368 KB |
Output is correct |