//khodaya khodet komak kon
# include "gardenlib.h"
# include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <pii, int> ppi;
typedef pair <int, pii> pip;
typedef pair <pii, pii> ppp;
typedef pair <ll, ll> pll;
# define A first
# define B second
# define endl '\n'
# define sep ' '
# define all(x) x.begin(), x.end()
# define kill(x) return cout << x << endl, 0
# define SZ(x) int(x.size())
# define lc id << 1
# define rc id << 1 | 1
# define fast_io ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
ll power(ll a, ll b, ll md) {return (!b ? 1 : (b & 1 ? a * power(a * a % md, b / 2, md) % md : power(a * a % md, b / 2, md) % md));}
const int xn = 5e5 + 10;
const int xm = - 20 + 10;
const int sq = 320;
const int inf = 1e9 + 10;
const ll INF = 1e18 + 10;
const ld eps = 1e-15;
const int mod = 1e9 + 7;//998244353;
const int base = 257;
struct edge{
int from, to, tabe;
};
int n, ptr, ans[xn], a[xn], q;
edge E[xn];
vector <int> adj[2][xn], g[xn], jad;
vector <pii> que[xn];
bool is_good[xn], mark[xn];
deque <int> dq;
void DFS(int v){
dq.push_back(v);
if (mark[v])
return;
mark[v] = true;
DFS(E[v].tabe);
}
void DFS2(int v, int rt, int h = 0){
jad.push_back(v);
mark[v] = true;
if (v == adj[0][E[v].from][0]){
for (int i = 0; i < q; ++ i){
int k = a[i];
if (k < h)
ans[i] += is_good[jad[h - k]];
else{
k -= h;
k = (k + rt) % SZ(dq);
ans[i] += is_good[dq[k]];
}
}
}
for (int u : g[v]){
if (mark[u])
continue;
DFS2(u, rt, h + 1);
}
jad.pop_back();
}
void count_routes(int N, int M, int P, int R[][2], int Q, int G[]){
//int N, M, P, Q;
//cin >> N >> M >> P;
n = N, q = Q;
for (int i = 0; i < M; ++ i){
int v, u;
//cin >> v >> u;
v = R[i][0], u = R[i][1];
adj[0][v].push_back(ptr);
adj[1][u].push_back(ptr);
E[ptr].from = v;
E[ptr].to = u;
ptr ++;
adj[0][u].push_back(ptr);
adj[1][v].push_back(ptr);
E[ptr].from = u;
E[ptr].to = v;
ptr ++;
}
for (int i = 0; i < n; ++ i)
sort(all(adj[0][i]));
for (int i = 0; i < ptr; ++ i){
int v = E[i].to;
if (adj[0][v][0] != (i ^ 1))
E[i].tabe = adj[0][v][0];
else if (1 < SZ(adj[0][v]))
E[i].tabe = adj[0][v][1];
else
E[i].tabe = adj[0][v][0];
g[E[i].tabe].push_back(i);
}
for (int u : adj[1][P])
is_good[u] = true;
//cin >> Q;
for (int _ = 0; _ < Q; ++ _){
int k = G[_];
a[_] = k - 1;
//cin >> k;
}
for (int i = 0; i < ptr; ++ i){
if (mark[i])
continue;
dq.clear();
DFS(i);
while (dq.front() != dq.back())
mark[dq.front()] = false, dq.pop_front();
dq.pop_back();
for (int j = 0; j < SZ(dq); ++ j){
int v = dq[j];
jad.clear();
DFS2(v, j);
}
}
for (int i = 0; i < Q; ++ i){
//cout << ans[i] << endl;
answer(ans[i]);
}
}
/*
int main(){
fast_io;
count_routes();
return 0;
}
*/
/*
5 5 2
1 0
1 2
3 2
1 3
4 2
2
3 1
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
47480 KB |
Output is correct |
2 |
Correct |
34 ms |
47476 KB |
Output is correct |
3 |
Correct |
27 ms |
47460 KB |
Output is correct |
4 |
Correct |
25 ms |
47276 KB |
Output is correct |
5 |
Correct |
26 ms |
47320 KB |
Output is correct |
6 |
Correct |
28 ms |
47688 KB |
Output is correct |
7 |
Correct |
28 ms |
47344 KB |
Output is correct |
8 |
Correct |
26 ms |
47464 KB |
Output is correct |
9 |
Correct |
34 ms |
47956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
47480 KB |
Output is correct |
2 |
Correct |
34 ms |
47476 KB |
Output is correct |
3 |
Correct |
27 ms |
47460 KB |
Output is correct |
4 |
Correct |
25 ms |
47276 KB |
Output is correct |
5 |
Correct |
26 ms |
47320 KB |
Output is correct |
6 |
Correct |
28 ms |
47688 KB |
Output is correct |
7 |
Correct |
28 ms |
47344 KB |
Output is correct |
8 |
Correct |
26 ms |
47464 KB |
Output is correct |
9 |
Correct |
34 ms |
47956 KB |
Output is correct |
10 |
Correct |
26 ms |
47236 KB |
Output is correct |
11 |
Correct |
49 ms |
50540 KB |
Output is correct |
12 |
Correct |
98 ms |
53732 KB |
Output is correct |
13 |
Correct |
70 ms |
61444 KB |
Output is correct |
14 |
Correct |
271 ms |
67488 KB |
Output is correct |
15 |
Correct |
291 ms |
68128 KB |
Output is correct |
16 |
Correct |
247 ms |
63384 KB |
Output is correct |
17 |
Correct |
252 ms |
62036 KB |
Output is correct |
18 |
Correct |
83 ms |
53728 KB |
Output is correct |
19 |
Correct |
237 ms |
67384 KB |
Output is correct |
20 |
Correct |
251 ms |
67996 KB |
Output is correct |
21 |
Correct |
239 ms |
63732 KB |
Output is correct |
22 |
Correct |
242 ms |
62128 KB |
Output is correct |
23 |
Correct |
232 ms |
69176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
47480 KB |
Output is correct |
2 |
Correct |
34 ms |
47476 KB |
Output is correct |
3 |
Correct |
27 ms |
47460 KB |
Output is correct |
4 |
Correct |
25 ms |
47276 KB |
Output is correct |
5 |
Correct |
26 ms |
47320 KB |
Output is correct |
6 |
Correct |
28 ms |
47688 KB |
Output is correct |
7 |
Correct |
28 ms |
47344 KB |
Output is correct |
8 |
Correct |
26 ms |
47464 KB |
Output is correct |
9 |
Correct |
34 ms |
47956 KB |
Output is correct |
10 |
Correct |
26 ms |
47236 KB |
Output is correct |
11 |
Correct |
49 ms |
50540 KB |
Output is correct |
12 |
Correct |
98 ms |
53732 KB |
Output is correct |
13 |
Correct |
70 ms |
61444 KB |
Output is correct |
14 |
Correct |
271 ms |
67488 KB |
Output is correct |
15 |
Correct |
291 ms |
68128 KB |
Output is correct |
16 |
Correct |
247 ms |
63384 KB |
Output is correct |
17 |
Correct |
252 ms |
62036 KB |
Output is correct |
18 |
Correct |
83 ms |
53728 KB |
Output is correct |
19 |
Correct |
237 ms |
67384 KB |
Output is correct |
20 |
Correct |
251 ms |
67996 KB |
Output is correct |
21 |
Correct |
239 ms |
63732 KB |
Output is correct |
22 |
Correct |
242 ms |
62128 KB |
Output is correct |
23 |
Correct |
232 ms |
69176 KB |
Output is correct |
24 |
Correct |
24 ms |
47316 KB |
Output is correct |
25 |
Correct |
365 ms |
50684 KB |
Output is correct |
26 |
Correct |
627 ms |
53756 KB |
Output is correct |
27 |
Correct |
1594 ms |
61524 KB |
Output is correct |
28 |
Correct |
2751 ms |
67512 KB |
Output is correct |
29 |
Correct |
2801 ms |
68028 KB |
Output is correct |
30 |
Correct |
1842 ms |
63504 KB |
Output is correct |
31 |
Correct |
1635 ms |
62180 KB |
Output is correct |
32 |
Correct |
747 ms |
53764 KB |
Output is correct |
33 |
Correct |
2719 ms |
67492 KB |
Output is correct |
34 |
Correct |
2551 ms |
68232 KB |
Output is correct |
35 |
Correct |
1952 ms |
63536 KB |
Output is correct |
36 |
Correct |
1506 ms |
62268 KB |
Output is correct |
37 |
Correct |
2598 ms |
69400 KB |
Output is correct |
38 |
Correct |
2743 ms |
71820 KB |
Output is correct |