#include "garden.h"
#include "gardenlib.h"
#include <iostream>
#include <vector>
#include <stack>
#include <set>
#include <map>
using namespace std;
typedef pair<int, int> pii;
const int N = 1.5e5 + 2;
const int INF = 1e9 + 1;
int n, m, src;
pii org[2];
vector<int> g[N];
stack<pii> st;
map<pii, int> vst, f[2], r[2];
vector<pair<pii, pii> > a;
void Dfs(int u, int p)
{
pii pre(p, u);
pii e(u, g[u].size() > 1 && g[u][0] == p ? g[u][1] : g[u][0]);
f[0][pre] = f[1][pre] = -1;
if (vst.find(e) == vst.end())
{
vst[e] = 1;
st.push(e);
Dfs(e.second, u);
vst[e] = 2;
r[0][pre] = r[0][e];
r[1][pre] = r[1][e];
if (pre == org[0]) f[0][pre] = 0;
else if (f[0][e] != -1) f[0][pre] = f[0][e] + 1;
if (pre == org[1]) f[1][pre] = 0;
else if (f[1][e] != -1) f[1][pre] = f[1][e] + 1;
}
else if (vst[e] == 1)
{
for (int i = 0; ;++i)
{
auto top = st.top(); st.pop();
if (top == org[0]) f[0][pre] = i;
if (top == org[1]) f[1][pre] = i;
++r[0][pre];
++r[1][pre];
if (top == e) break;
}
if (f[0][pre] == -1) r[0][pre] = -1;
else f[0][pre] = (r[0][pre] - f[0][pre]) % r[0][pre];
if (f[1][pre] == -1) r[1][pre] = -1;
else f[1][pre] = (r[1][pre] - f[1][pre]) % r[1][pre];
}
else
{
r[0][pre] = r[0][e];
r[1][pre] = r[1][e];
if (pre == org[0]) f[0][pre] = 0;
else if (f[0][e] != -1) f[0][pre] = f[0][e] + 1;
if (pre == org[1]) f[1][pre] = 0;
else if (f[1][e] != -1) f[1][pre] = f[1][e] + 1;
}
}
void count_routes(int N, int M, int P, int R[][2], int Q, int G[])
{
n = N, m = M, src = P;
for (int i = 0; i < m; ++i)
{
int u, v; u = R[i][0], v = R[i][1];
if (g[u].size() < 2) g[u].push_back(v);
if (g[v].size() < 2) g[v].push_back(u);
}
org[0] = {src, g[src][0]};
org[1] = {src, g[src].size() > 1 ? g[src][1] : -1};
for (int u = 0; u < n; ++u)
{
while (!st.empty()) st.pop();
pii e(u, g[u][0]);
if (vst.find(e) == vst.end()) Dfs(u, -1);
a.push_back({{f[0][e], r[0][e]}, {f[1][e], r[1][e]}});
//cerr << f[0][e] << ' ' << r[0][e] << ' ' << f[1][e] << ' ' << r[1][e] << '\n';
}
int q = Q;
for (int i = 0; i < q; ++i)
{
int x = G[i], ans = 0;
for (auto p: a)
{
auto satis = [](int x, pii q) -> bool { return q.first != -1 && (x == q.first || (x > q.first && q.second != -1 && (x - q.first) % q.second == 0)); };
ans += satis(x, p.first) || satis(x, p.second);
}
answer(ans);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4344 KB |
Output is correct |
2 |
Correct |
8 ms |
4444 KB |
Output is correct |
3 |
Correct |
8 ms |
4448 KB |
Output is correct |
4 |
Correct |
5 ms |
4004 KB |
Output is correct |
5 |
Correct |
6 ms |
3892 KB |
Output is correct |
6 |
Correct |
8 ms |
4472 KB |
Output is correct |
7 |
Correct |
6 ms |
3960 KB |
Output is correct |
8 |
Correct |
9 ms |
4472 KB |
Output is correct |
9 |
Correct |
11 ms |
4652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4344 KB |
Output is correct |
2 |
Correct |
8 ms |
4444 KB |
Output is correct |
3 |
Correct |
8 ms |
4448 KB |
Output is correct |
4 |
Correct |
5 ms |
4004 KB |
Output is correct |
5 |
Correct |
6 ms |
3892 KB |
Output is correct |
6 |
Correct |
8 ms |
4472 KB |
Output is correct |
7 |
Correct |
6 ms |
3960 KB |
Output is correct |
8 |
Correct |
9 ms |
4472 KB |
Output is correct |
9 |
Correct |
11 ms |
4652 KB |
Output is correct |
10 |
Correct |
5 ms |
3932 KB |
Output is correct |
11 |
Correct |
139 ms |
19248 KB |
Output is correct |
12 |
Correct |
316 ms |
28504 KB |
Output is correct |
13 |
Correct |
539 ms |
77156 KB |
Output is correct |
14 |
Correct |
1522 ms |
89092 KB |
Output is correct |
15 |
Correct |
1756 ms |
90120 KB |
Output is correct |
16 |
Correct |
1053 ms |
62796 KB |
Output is correct |
17 |
Correct |
791 ms |
53216 KB |
Output is correct |
18 |
Correct |
431 ms |
28788 KB |
Output is correct |
19 |
Correct |
1197 ms |
88460 KB |
Output is correct |
20 |
Correct |
1311 ms |
89748 KB |
Output is correct |
21 |
Correct |
1085 ms |
66288 KB |
Output is correct |
22 |
Correct |
909 ms |
57804 KB |
Output is correct |
23 |
Correct |
1695 ms |
98556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4344 KB |
Output is correct |
2 |
Correct |
8 ms |
4444 KB |
Output is correct |
3 |
Correct |
8 ms |
4448 KB |
Output is correct |
4 |
Correct |
5 ms |
4004 KB |
Output is correct |
5 |
Correct |
6 ms |
3892 KB |
Output is correct |
6 |
Correct |
8 ms |
4472 KB |
Output is correct |
7 |
Correct |
6 ms |
3960 KB |
Output is correct |
8 |
Correct |
9 ms |
4472 KB |
Output is correct |
9 |
Correct |
11 ms |
4652 KB |
Output is correct |
10 |
Correct |
5 ms |
3932 KB |
Output is correct |
11 |
Correct |
139 ms |
19248 KB |
Output is correct |
12 |
Correct |
316 ms |
28504 KB |
Output is correct |
13 |
Correct |
539 ms |
77156 KB |
Output is correct |
14 |
Correct |
1522 ms |
89092 KB |
Output is correct |
15 |
Correct |
1756 ms |
90120 KB |
Output is correct |
16 |
Correct |
1053 ms |
62796 KB |
Output is correct |
17 |
Correct |
791 ms |
53216 KB |
Output is correct |
18 |
Correct |
431 ms |
28788 KB |
Output is correct |
19 |
Correct |
1197 ms |
88460 KB |
Output is correct |
20 |
Correct |
1311 ms |
89748 KB |
Output is correct |
21 |
Correct |
1085 ms |
66288 KB |
Output is correct |
22 |
Correct |
909 ms |
57804 KB |
Output is correct |
23 |
Correct |
1695 ms |
98556 KB |
Output is correct |
24 |
Correct |
4 ms |
3960 KB |
Output is correct |
25 |
Correct |
222 ms |
19172 KB |
Output is correct |
26 |
Correct |
421 ms |
28432 KB |
Output is correct |
27 |
Correct |
2437 ms |
77080 KB |
Output is correct |
28 |
Correct |
2669 ms |
89188 KB |
Output is correct |
29 |
Correct |
3707 ms |
90200 KB |
Output is correct |
30 |
Correct |
2138 ms |
62940 KB |
Output is correct |
31 |
Correct |
2007 ms |
53276 KB |
Output is correct |
32 |
Correct |
445 ms |
28964 KB |
Output is correct |
33 |
Correct |
2290 ms |
89328 KB |
Output is correct |
34 |
Correct |
3164 ms |
90616 KB |
Output is correct |
35 |
Correct |
2115 ms |
66572 KB |
Output is correct |
36 |
Correct |
2628 ms |
58696 KB |
Output is correct |
37 |
Correct |
2446 ms |
99668 KB |
Output is correct |
38 |
Correct |
4050 ms |
119812 KB |
Output is correct |