#include "garden.h"
#include "gardenlib.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 3e5;
int N, M, P, Q;
int *G;
vector<int> adj2[MAXN+10];
vector<int> adj[MAXN+10];
int ans[MAXN+10];
int cnt[MAXN+10], cyc=-1;
int dep[MAXN+10];
void dfs(int now, int depth)
{
//printf("%d\n", now);
dep[now]=depth;
for(auto nxt : adj[now])
{
if(dep[nxt]!=-1)
{
cyc=depth+1;
continue;
}
dfs(nxt, depth+1);
}
}
void count_routes(int _N, int _M, int _P, int R[][2], int _Q, int *_G)
{
int i, j;
N=_N; M=_M; P=_P; Q=_Q; G=_G;
for(i=0; i<M; i++)
{
int u=R[i][0], v=R[i][1];
adj2[u].push_back(v);
adj2[v].push_back(u);
}
for(i=0; i<N; i++)
{
int nxt=adj2[i][0];
if(adj2[nxt][0]==i) adj[nxt+N].push_back(i);//, printf("E%d %d\n", nxt+N, i);
else adj[nxt].push_back(i);//, printf("E%d %d\n", nxt, i);
}
for(i=0; i<N; i++)
{
int nxt;
if(adj2[i].size()==1) nxt=adj2[i][0];
else nxt=adj2[i][1];
if(adj2[nxt][0]==i) adj[nxt+N].push_back(i+N);//, printf("E%d %d\n", nxt+N, i+N);
else adj[nxt].push_back(i+N);//, printf("E%d %d\n", nxt, i+N);
}
memset(cnt, 0, sizeof(cnt));
memset(dep, -1, sizeof(dep));
cyc=-1;
dfs(P, 0);
for(i=0; i<N; i++) cnt[dep[i]]++;
for(i=0; i<Q; i++)
{
if(cyc==-1)
{
if(G[i]<2*N) ans[i]+=cnt[G[i]];
}
else
{
for(j=G[i]%cyc; j<2*N && j<=G[i]; j+=cyc) ans[i]+=cnt[j];
}
}
//printf("!%d\n", cyc);
memset(cnt, 0, sizeof(cnt));
memset(dep, -1, sizeof(dep));
cyc=-1;
dfs(P+N, 0);
for(i=0; i<N; i++) cnt[dep[i]]++;
for(i=0; i<Q; i++)
{
if(cyc==-1)
{
if(G[i]<2*N) ans[i]+=cnt[G[i]];
}
else
{
for(j=G[i]%cyc; j<2*N && j<=G[i]; j+=cyc) ans[i]+=cnt[j];
}
}
//printf("!%d\n", cyc);
for(i=0; i<Q; i++) answer(ans[i]);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
16920 KB |
Output is correct |
2 |
Correct |
18 ms |
16888 KB |
Output is correct |
3 |
Correct |
18 ms |
16836 KB |
Output is correct |
4 |
Correct |
16 ms |
16760 KB |
Output is correct |
5 |
Correct |
17 ms |
16760 KB |
Output is correct |
6 |
Correct |
18 ms |
16936 KB |
Output is correct |
7 |
Correct |
17 ms |
16732 KB |
Output is correct |
8 |
Correct |
17 ms |
16888 KB |
Output is correct |
9 |
Correct |
19 ms |
17016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
16920 KB |
Output is correct |
2 |
Correct |
18 ms |
16888 KB |
Output is correct |
3 |
Correct |
18 ms |
16836 KB |
Output is correct |
4 |
Correct |
16 ms |
16760 KB |
Output is correct |
5 |
Correct |
17 ms |
16760 KB |
Output is correct |
6 |
Correct |
18 ms |
16936 KB |
Output is correct |
7 |
Correct |
17 ms |
16732 KB |
Output is correct |
8 |
Correct |
17 ms |
16888 KB |
Output is correct |
9 |
Correct |
19 ms |
17016 KB |
Output is correct |
10 |
Correct |
17 ms |
16760 KB |
Output is correct |
11 |
Correct |
32 ms |
18576 KB |
Output is correct |
12 |
Correct |
52 ms |
19832 KB |
Output is correct |
13 |
Correct |
68 ms |
30856 KB |
Output is correct |
14 |
Correct |
199 ms |
26800 KB |
Output is correct |
15 |
Correct |
227 ms |
27096 KB |
Output is correct |
16 |
Correct |
181 ms |
24476 KB |
Output is correct |
17 |
Correct |
143 ms |
23544 KB |
Output is correct |
18 |
Correct |
52 ms |
19812 KB |
Output is correct |
19 |
Correct |
171 ms |
26788 KB |
Output is correct |
20 |
Correct |
218 ms |
27080 KB |
Output is correct |
21 |
Correct |
177 ms |
24424 KB |
Output is correct |
22 |
Correct |
152 ms |
23440 KB |
Output is correct |
23 |
Correct |
203 ms |
27840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
16920 KB |
Output is correct |
2 |
Correct |
18 ms |
16888 KB |
Output is correct |
3 |
Correct |
18 ms |
16836 KB |
Output is correct |
4 |
Correct |
16 ms |
16760 KB |
Output is correct |
5 |
Correct |
17 ms |
16760 KB |
Output is correct |
6 |
Correct |
18 ms |
16936 KB |
Output is correct |
7 |
Correct |
17 ms |
16732 KB |
Output is correct |
8 |
Correct |
17 ms |
16888 KB |
Output is correct |
9 |
Correct |
19 ms |
17016 KB |
Output is correct |
10 |
Correct |
17 ms |
16760 KB |
Output is correct |
11 |
Correct |
32 ms |
18576 KB |
Output is correct |
12 |
Correct |
52 ms |
19832 KB |
Output is correct |
13 |
Correct |
68 ms |
30856 KB |
Output is correct |
14 |
Correct |
199 ms |
26800 KB |
Output is correct |
15 |
Correct |
227 ms |
27096 KB |
Output is correct |
16 |
Correct |
181 ms |
24476 KB |
Output is correct |
17 |
Correct |
143 ms |
23544 KB |
Output is correct |
18 |
Correct |
52 ms |
19812 KB |
Output is correct |
19 |
Correct |
171 ms |
26788 KB |
Output is correct |
20 |
Correct |
218 ms |
27080 KB |
Output is correct |
21 |
Correct |
177 ms |
24424 KB |
Output is correct |
22 |
Correct |
152 ms |
23440 KB |
Output is correct |
23 |
Correct |
203 ms |
27840 KB |
Output is correct |
24 |
Correct |
17 ms |
16760 KB |
Output is correct |
25 |
Correct |
38 ms |
18764 KB |
Output is correct |
26 |
Correct |
55 ms |
19836 KB |
Output is correct |
27 |
Correct |
67 ms |
30812 KB |
Output is correct |
28 |
Correct |
429 ms |
26872 KB |
Output is correct |
29 |
Correct |
353 ms |
27116 KB |
Output is correct |
30 |
Correct |
160 ms |
24540 KB |
Output is correct |
31 |
Correct |
189 ms |
23568 KB |
Output is correct |
32 |
Correct |
57 ms |
19900 KB |
Output is correct |
33 |
Correct |
341 ms |
26832 KB |
Output is correct |
34 |
Correct |
352 ms |
27168 KB |
Output is correct |
35 |
Correct |
174 ms |
24332 KB |
Output is correct |
36 |
Correct |
197 ms |
23540 KB |
Output is correct |
37 |
Correct |
198 ms |
27768 KB |
Output is correct |
38 |
Correct |
187 ms |
36768 KB |
Output is correct |