#include "garden.h"
#include "gardenlib.h"
#include <vector>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
typedef pair<int,int> pi;
queue<int> q, p, d;
vector<int> ls;
vector<pi> graph[150005];
vector<int> graph1[300005];
int nxt[300005];
int low[150005];
int deg[300005];
int ped[300005];
bool vis[300005];
bool ans[150005];
void make_graph(int N, int M, int R[][2]){
for (int i=0; i<M; i++) {
for (int j=0; j<2; j++) {
low[R[i][j]] = min(low[R[i][j]],i);
}
graph[R[i][0]].push_back(pi(R[i][1],i));
graph[R[i][1]].push_back(pi(R[i][0],i));
}
for (int i=0; i<N; i++) {
if(low[graph[i][0].first] == graph[i][0].second){
nxt[2*i] = graph[i][0].first * 2 + 1;
}
else{
nxt[2*i] = graph[i][0].first * 2;
}
if(graph[i].size() == 1){
nxt[2*i+1] = nxt[2*i];
}
else{
if(low[graph[i][1].first] == graph[i][1].second){
nxt[2*i+1] = graph[i][1].first * 2 + 1;
}
else{
nxt[2*i+1] = graph[i][1].first * 2;
}
}
}
}
int dist0[150005], dist1[150005], peri0[150005], peri1[150005];
void solve(int x, int *dist, int *peri){
memset(vis,0,sizeof(vis));
q.push(x);
p.push(-1);
d.push(0);
vis[x] = 1;
while (!q.empty()) {
int qf = q.front();
int pf = p.front();
int df = d.front();
q.pop(), p.pop(), d.pop();
pf = max(pf, ped[qf]);
if(qf % 2 == 0){
dist[qf/2] = df;
peri[qf/2] = pf;
}
for (auto &i : graph1[qf]){
if(!vis[i]){
vis[i] = 1;
q.push(i);
p.push(pf);
d.push(df + 1);
}
}
}
}
void count_routes(int N, int M, int P, int R[][2], int Q, int G[])
{
memset(ped,-1,sizeof(ped));
memset(dist0,-1,sizeof(dist0));
memset(dist1,-1,sizeof(dist1));
memset(peri0,-1,sizeof(peri0));
memset(peri1,-1,sizeof(peri1));
memset(low,0x3f,sizeof(low));
make_graph(N,M,R);
for (int i=0; i<2*N; i++) {
deg[i]++;
deg[nxt[i]]++;
}
for (int i=0; i<2*N; i++) {
if(deg[i] == 1){
q.push(i);
}
}
while (!q.empty()) {
int qf = q.front();
q.pop();
deg[qf]--;
deg[nxt[qf]]--;
if(deg[nxt[qf]] == 1){
q.push(nxt[qf]);
}
}
for (int i=0; i<2*N; i++) {
graph1[nxt[i]].push_back(i);
if(deg[i]){
int pos = i;
ls.clear();
while (deg[pos]) {
deg[pos] = 0;
ls.push_back(pos);
pos = nxt[pos];
}
for (auto &i : ls){
ped[i] = (int)ls.size();
}
}
}
solve(2*P,dist0,peri0);
solve(2*P+1,dist1,peri1);
for (int i=0; i<Q; i++) {
int cnt = 0;
for (int j=0; j<N; j++) {
if(dist0[j] != -1){
if(peri0[j] == -1){
if(dist0[j] == G[i]){
cnt++; continue;
}
}
else{
if(dist0[j] <= G[i] && (G[i] - dist0[j]) % peri0[j] == 0){
cnt++; continue;
}
}
}
if(dist1[j] != -1){
if(peri1[j] == -1){
if(dist1[j] == G[i]){
cnt++; continue;
}
}
else{
if(dist1[j] <= G[i] && (G[i] - dist1[j]) %peri1[j] == 0){
cnt++; continue;
}
}
}
}
answer(cnt);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
15440 KB |
Output is correct |
2 |
Correct |
10 ms |
15312 KB |
Output is correct |
3 |
Correct |
10 ms |
15440 KB |
Output is correct |
4 |
Correct |
10 ms |
15608 KB |
Output is correct |
5 |
Correct |
11 ms |
15412 KB |
Output is correct |
6 |
Correct |
12 ms |
15452 KB |
Output is correct |
7 |
Correct |
11 ms |
15440 KB |
Output is correct |
8 |
Correct |
12 ms |
15492 KB |
Output is correct |
9 |
Correct |
14 ms |
15708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
15440 KB |
Output is correct |
2 |
Correct |
10 ms |
15312 KB |
Output is correct |
3 |
Correct |
10 ms |
15440 KB |
Output is correct |
4 |
Correct |
10 ms |
15608 KB |
Output is correct |
5 |
Correct |
11 ms |
15412 KB |
Output is correct |
6 |
Correct |
12 ms |
15452 KB |
Output is correct |
7 |
Correct |
11 ms |
15440 KB |
Output is correct |
8 |
Correct |
12 ms |
15492 KB |
Output is correct |
9 |
Correct |
14 ms |
15708 KB |
Output is correct |
10 |
Correct |
12 ms |
15440 KB |
Output is correct |
11 |
Correct |
22 ms |
17944 KB |
Output is correct |
12 |
Correct |
35 ms |
20048 KB |
Output is correct |
13 |
Correct |
43 ms |
26852 KB |
Output is correct |
14 |
Correct |
100 ms |
30060 KB |
Output is correct |
15 |
Correct |
114 ms |
30536 KB |
Output is correct |
16 |
Correct |
87 ms |
26908 KB |
Output is correct |
17 |
Correct |
78 ms |
26224 KB |
Output is correct |
18 |
Correct |
34 ms |
19916 KB |
Output is correct |
19 |
Correct |
85 ms |
29956 KB |
Output is correct |
20 |
Correct |
105 ms |
30476 KB |
Output is correct |
21 |
Correct |
83 ms |
26952 KB |
Output is correct |
22 |
Correct |
75 ms |
26148 KB |
Output is correct |
23 |
Correct |
92 ms |
31136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
15440 KB |
Output is correct |
2 |
Correct |
10 ms |
15312 KB |
Output is correct |
3 |
Correct |
10 ms |
15440 KB |
Output is correct |
4 |
Correct |
10 ms |
15608 KB |
Output is correct |
5 |
Correct |
11 ms |
15412 KB |
Output is correct |
6 |
Correct |
12 ms |
15452 KB |
Output is correct |
7 |
Correct |
11 ms |
15440 KB |
Output is correct |
8 |
Correct |
12 ms |
15492 KB |
Output is correct |
9 |
Correct |
14 ms |
15708 KB |
Output is correct |
10 |
Correct |
12 ms |
15440 KB |
Output is correct |
11 |
Correct |
22 ms |
17944 KB |
Output is correct |
12 |
Correct |
35 ms |
20048 KB |
Output is correct |
13 |
Correct |
43 ms |
26852 KB |
Output is correct |
14 |
Correct |
100 ms |
30060 KB |
Output is correct |
15 |
Correct |
114 ms |
30536 KB |
Output is correct |
16 |
Correct |
87 ms |
26908 KB |
Output is correct |
17 |
Correct |
78 ms |
26224 KB |
Output is correct |
18 |
Correct |
34 ms |
19916 KB |
Output is correct |
19 |
Correct |
85 ms |
29956 KB |
Output is correct |
20 |
Correct |
105 ms |
30476 KB |
Output is correct |
21 |
Correct |
83 ms |
26952 KB |
Output is correct |
22 |
Correct |
75 ms |
26148 KB |
Output is correct |
23 |
Correct |
92 ms |
31136 KB |
Output is correct |
24 |
Correct |
11 ms |
15440 KB |
Output is correct |
25 |
Correct |
66 ms |
17936 KB |
Output is correct |
26 |
Correct |
94 ms |
20040 KB |
Output is correct |
27 |
Correct |
725 ms |
27108 KB |
Output is correct |
28 |
Correct |
686 ms |
30024 KB |
Output is correct |
29 |
Correct |
814 ms |
30536 KB |
Output is correct |
30 |
Correct |
431 ms |
27044 KB |
Output is correct |
31 |
Correct |
384 ms |
26184 KB |
Output is correct |
32 |
Correct |
102 ms |
20032 KB |
Output is correct |
33 |
Correct |
665 ms |
30108 KB |
Output is correct |
34 |
Correct |
814 ms |
30628 KB |
Output is correct |
35 |
Correct |
483 ms |
26952 KB |
Output is correct |
36 |
Correct |
694 ms |
26444 KB |
Output is correct |
37 |
Correct |
475 ms |
31280 KB |
Output is correct |
38 |
Correct |
1234 ms |
35500 KB |
Output is correct |