#include "garden.h"
#include "gardenlib.h"
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define REP(n) FOR(O, 1, (n))
#define f first
#define s second
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vii;
const int MAXN = 300100, MAXK = 31;
int n, m, p;
vii adj[MAXN];
int par[2*MAXN][MAXK];
int getId (int v, int b) {
return (b*n + v);
}
/*int lift (int v, ll x) {
FOR(i, 0, MAXK-1)
if (x & (1ll<<i))
v = par[v][i];
return v;
}
int getAns (ll k){
int ret = 0;
//k--;
FOR(i, 0, n-1) {
int lifted = lift(i,k);
// cout << " k = " << k << " i = " << i << " lifted = " << lifted << endl;
if (lifted == getId(p, 0) || lifted == getId(p, 1)) {
ret++;
}
}
return ret;
}*/
bool vis[MAXN];
stack<int> cycle;
int toP[MAXN][2];
bool inCycle[MAXN][2];
int cycleLen[2];
void checkCycle (int v, int st, bool b) {
vis[v] = true;
int u = par[v][0];
cycle.push(v);
if (!vis[u])
checkCycle(u, st, b);
else {
if (u == st) {
inCycle[st][b] = true;
cycleLen[b] = (int)cycle.size();
int cur = 0;
while (!cycle.empty()) {
int x = cycle.top();
inCycle[x][b] = true;
cycle.pop();
cur++;
if (x != st) toP[x][b] = cur;
}
} else {
while (!cycle.empty()) {
cycle.pop();
}
}
}
}
void dfs (int v, bool b) {
if (inCycle[v][b]) return;
toP[v][b] = -1;
vis[v] = true;
int u = par[v][0];
if (!vis[u])
dfs(u, b);
if (toP[u][b] == -1) toP[v][b] = -1;
else toP[v][b] = toP[u][b] + 1;
if(v==getId(p,b)) toP[v][b] = 0;
}
int getAns (ll k) {
int ret = 0;
FOR(i, 0, n-1) {
bool ok = false;
FOR(b,0,1) {
// cout << " i = " << i << " b = " << b << " toP = " << toP[i][b] << " incycle= " << inCycle[i][b] << endl;
if (toP[i][b] == -1) continue;
ll rem = k - toP[i][b];
if (rem > 0 && cycleLen[b] > 0 && ((rem % cycleLen[b]) == 0))
ok = true;
else if (rem == 0) ok = true;
}
if (ok) ret++;
}
return ret;
}
void count_routes(int N, int M, int P, int R[][2], int Q, int G[])
{
n = N;
m = M;
p = P;
FOR(i, 0, m-1) {
int u = R[i][0];
int v = R[i][1];
adj[u].pb({i, v});
adj[v].pb({i, u});
}
FOR(v, 0, n-1) FOR(b, 0, 1) {
int fr = getId(v, b);
int u = (b < (int)adj[v].size() ? adj[v][b].s : adj[v][0].s);
int b2 = (adj[u][0].s == v);
int to = getId(u, b2);
par[fr][0] = to;
// cout << " fr = " << fr << " to = " << to << endl;
}
/*FOR(j, 1, MAXK-1) FOR(i, 0, getId(n-1, 1))
par[i][j] = par[par[i][j-1]][j-1];*/
int mxn = getId(n-1, 1);
//cout << " a" << endl;
FOR(b, 0, 1) {
FOR(i, 0, mxn) vis[i] = false;
checkCycle(getId(p, b), getId(p, b), b);
}
//cout << " b " << endl;
FOR(b, 0, 1) {
FOR(i, 0, mxn) vis[i] = false;
FOR(i, 0, mxn) {
if (!vis[i])
dfs(i, b);
}
}
//cout << " c" << endl;
for(int i=0; i<Q; i++) {
ll k = G[i];
int ans = getAns (k);
answer(ans);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7660 KB |
Output is correct |
2 |
Correct |
5 ms |
7660 KB |
Output is correct |
3 |
Correct |
7 ms |
7788 KB |
Output is correct |
4 |
Correct |
5 ms |
7404 KB |
Output is correct |
5 |
Correct |
5 ms |
7404 KB |
Output is correct |
6 |
Correct |
8 ms |
7788 KB |
Output is correct |
7 |
Correct |
5 ms |
7404 KB |
Output is correct |
8 |
Correct |
6 ms |
7660 KB |
Output is correct |
9 |
Correct |
9 ms |
8044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7660 KB |
Output is correct |
2 |
Correct |
5 ms |
7660 KB |
Output is correct |
3 |
Correct |
7 ms |
7788 KB |
Output is correct |
4 |
Correct |
5 ms |
7404 KB |
Output is correct |
5 |
Correct |
5 ms |
7404 KB |
Output is correct |
6 |
Correct |
8 ms |
7788 KB |
Output is correct |
7 |
Correct |
5 ms |
7404 KB |
Output is correct |
8 |
Correct |
6 ms |
7660 KB |
Output is correct |
9 |
Correct |
9 ms |
8044 KB |
Output is correct |
10 |
Correct |
5 ms |
7404 KB |
Output is correct |
11 |
Correct |
20 ms |
14700 KB |
Output is correct |
12 |
Correct |
41 ms |
19820 KB |
Output is correct |
13 |
Correct |
54 ms |
34044 KB |
Output is correct |
14 |
Correct |
125 ms |
49004 KB |
Output is correct |
15 |
Correct |
132 ms |
49772 KB |
Output is correct |
16 |
Correct |
114 ms |
37356 KB |
Output is correct |
17 |
Correct |
98 ms |
33132 KB |
Output is correct |
18 |
Correct |
37 ms |
19564 KB |
Output is correct |
19 |
Correct |
126 ms |
49112 KB |
Output is correct |
20 |
Correct |
133 ms |
49644 KB |
Output is correct |
21 |
Correct |
112 ms |
37228 KB |
Output is correct |
22 |
Correct |
102 ms |
32876 KB |
Output is correct |
23 |
Correct |
135 ms |
53740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7660 KB |
Output is correct |
2 |
Correct |
5 ms |
7660 KB |
Output is correct |
3 |
Correct |
7 ms |
7788 KB |
Output is correct |
4 |
Correct |
5 ms |
7404 KB |
Output is correct |
5 |
Correct |
5 ms |
7404 KB |
Output is correct |
6 |
Correct |
8 ms |
7788 KB |
Output is correct |
7 |
Correct |
5 ms |
7404 KB |
Output is correct |
8 |
Correct |
6 ms |
7660 KB |
Output is correct |
9 |
Correct |
9 ms |
8044 KB |
Output is correct |
10 |
Correct |
5 ms |
7404 KB |
Output is correct |
11 |
Correct |
20 ms |
14700 KB |
Output is correct |
12 |
Correct |
41 ms |
19820 KB |
Output is correct |
13 |
Correct |
54 ms |
34044 KB |
Output is correct |
14 |
Correct |
125 ms |
49004 KB |
Output is correct |
15 |
Correct |
132 ms |
49772 KB |
Output is correct |
16 |
Correct |
114 ms |
37356 KB |
Output is correct |
17 |
Correct |
98 ms |
33132 KB |
Output is correct |
18 |
Correct |
37 ms |
19564 KB |
Output is correct |
19 |
Correct |
126 ms |
49112 KB |
Output is correct |
20 |
Correct |
133 ms |
49644 KB |
Output is correct |
21 |
Correct |
112 ms |
37228 KB |
Output is correct |
22 |
Correct |
102 ms |
32876 KB |
Output is correct |
23 |
Correct |
135 ms |
53740 KB |
Output is correct |
24 |
Correct |
6 ms |
7532 KB |
Output is correct |
25 |
Correct |
99 ms |
14700 KB |
Output is correct |
26 |
Correct |
135 ms |
20460 KB |
Output is correct |
27 |
Correct |
2539 ms |
35052 KB |
Output is correct |
28 |
Correct |
1032 ms |
50708 KB |
Output is correct |
29 |
Correct |
2890 ms |
51308 KB |
Output is correct |
30 |
Correct |
1615 ms |
38892 KB |
Output is correct |
31 |
Correct |
1644 ms |
34540 KB |
Output is correct |
32 |
Correct |
147 ms |
20204 KB |
Output is correct |
33 |
Correct |
1041 ms |
50796 KB |
Output is correct |
34 |
Correct |
2887 ms |
51440 KB |
Output is correct |
35 |
Correct |
1734 ms |
38672 KB |
Output is correct |
36 |
Correct |
1665 ms |
34540 KB |
Output is correct |
37 |
Correct |
778 ms |
55532 KB |
Output is correct |
38 |
Correct |
2261 ms |
64976 KB |
Output is correct |