#include <bits/stdc++.h>
#include "garden.h"
#include "gardenlib.h"
using namespace std;
const int maxn = 3e5+10;
typedef pair<int, int> pii;
int grafo[2][maxn];
int pai[maxn], nivel[maxn], dist[2][maxn];
int sz1, sz2;
int pai_uf[maxn], peso_uf[maxn], comp[maxn];
bool mark[maxn], mark2[maxn], inCycle[maxn];
vector<int> rev[maxn];
void makeGraph(int N)
{
for (int i = 0; i < N; i++)
{
int u = grafo[0][i], v = grafo[1][i];
if (u == -1 && v == -1) continue;
if (v == -1)
{
if (grafo[0][u] == i) pai[i] = u+N;
else pai[i] = u;
continue;
}
if (grafo[0][u] == i && grafo[1][u] != -1) pai[i] = u+N;
else pai[i] = u;
if (grafo[0][v] == i && grafo[1][v] != -1) pai[i+N] = v+N;
else pai[i+N] = v;
}
}
int getRoot(int u)
{
mark[u] = true;
if (mark[pai[u]]) return u;
return getRoot(pai[u]);
}
void getNivel(int u, int cc)
{
mark2[u] = true, comp[u] = cc;
for (auto v: rev[u])
{
if (mark2[v]) continue;
nivel[v] = nivel[u]+1;
getNivel(v, cc);
}
}
void getCycle(int u, int fim)
{
inCycle[u] = true;
if (u != fim) getCycle(pai[u], fim);
}
void bfs(int u, bool q)
{
memset(mark, 0, sizeof mark);
queue<int> fila;
fila.push(u), dist[q][u] = 0;
while (!fila.empty())
{
int u = fila.front();
fila.pop();
for (auto v: rev[u])
{
if (!mark[v])
{
fila.push(v); mark[v] = 1;
dist[q][v] = dist[q][u]+1;
}
}
}
}
bool check(int i, int P, int K, bool q)
{
int sz = (q ? sz1 : sz2);
if ((inCycle[P] && inCycle[i]) || (inCycle[P] && !inCycle[i]))
{
int d = dist[q][i];
if (d <= K && K%sz == d%sz) return true;
}
else if (dist[q][i] != -1 && !inCycle[P] && !inCycle[i])
{
int d = dist[q][i];
if (d == K) return true;
}
return false;
}
void count_routes(int N, int M, int P, int R[][2], int Q, int G[])
{
memset(pai, -1, sizeof pai);
memset(grafo, -1, sizeof grafo);
memset(dist, -1, sizeof dist);
for (int i = 0; i < M; i++)
{
int u = R[i][0], v = R[i][1];
if (grafo[0][u] == -1) grafo[0][u] = v;
else if (grafo[1][u] == -1) grafo[1][u] = v;
if (grafo[0][v] == -1) grafo[0][v] = u;
else if (grafo[1][v] == -1) grafo[1][v] = u;
}
makeGraph(N);
for (int i = 0; i < 2*N; i++)
if (pai[i] != -1)
rev[pai[i]].push_back(i);
int cc = 0;
for (int i = 0; i < 2*N; i++)
{
if (!mark2[i] && pai[i] != -1)
{
int R = getRoot(i);
getNivel(R, ++cc);
}
}
int ini1=-1, fim1=-1;
int ini2=-1, fim2=-1;
for (int i = 0; i < 2*N; i++)
{
if (comp[i] == comp[P] && nivel[pai[i]] > nivel[i])
ini1 = pai[i], fim1 = i, sz1 = nivel[pai[i]]-nivel[i]+1;
if (comp[i] == comp[P+N] && nivel[pai[i]] > nivel[i])
ini2 = pai[i], fim2 = i, sz2 = nivel[pai[i]]-nivel[i]+1;
}
if (ini1 != -1) getCycle(ini1, fim1);
if (ini2 != -1) getCycle(ini2, fim2);
bfs(P, 1); bfs(P+N, 0);
for (int q = 0; q < Q; q++)
{
int K = G[q], ans = 0;
for (int i = 0; i < N; i++)
{
if (comp[i] == comp[P] && check(i, P, K, 1)) ans++;
else if (comp[i] == comp[P+N] && check(i, P+N, K, 0)) ans++;
}
answer(ans);
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
13688 KB |
Output is correct |
2 |
Correct |
14 ms |
13688 KB |
Output is correct |
3 |
Correct |
14 ms |
13688 KB |
Output is correct |
4 |
Correct |
14 ms |
13560 KB |
Output is correct |
5 |
Correct |
14 ms |
13544 KB |
Output is correct |
6 |
Correct |
14 ms |
13688 KB |
Output is correct |
7 |
Correct |
14 ms |
13532 KB |
Output is correct |
8 |
Correct |
15 ms |
13660 KB |
Output is correct |
9 |
Correct |
16 ms |
13728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
13688 KB |
Output is correct |
2 |
Correct |
14 ms |
13688 KB |
Output is correct |
3 |
Correct |
14 ms |
13688 KB |
Output is correct |
4 |
Correct |
14 ms |
13560 KB |
Output is correct |
5 |
Correct |
14 ms |
13544 KB |
Output is correct |
6 |
Correct |
14 ms |
13688 KB |
Output is correct |
7 |
Correct |
14 ms |
13532 KB |
Output is correct |
8 |
Correct |
15 ms |
13660 KB |
Output is correct |
9 |
Correct |
16 ms |
13728 KB |
Output is correct |
10 |
Correct |
14 ms |
13560 KB |
Output is correct |
11 |
Correct |
24 ms |
15232 KB |
Output is correct |
12 |
Correct |
38 ms |
16248 KB |
Output is correct |
13 |
Correct |
64 ms |
29276 KB |
Output is correct |
14 |
Correct |
156 ms |
21616 KB |
Output is correct |
15 |
Correct |
169 ms |
21752 KB |
Output is correct |
16 |
Correct |
178 ms |
19724 KB |
Output is correct |
17 |
Correct |
145 ms |
18872 KB |
Output is correct |
18 |
Correct |
42 ms |
16024 KB |
Output is correct |
19 |
Correct |
136 ms |
21492 KB |
Output is correct |
20 |
Correct |
171 ms |
21628 KB |
Output is correct |
21 |
Correct |
134 ms |
19848 KB |
Output is correct |
22 |
Correct |
120 ms |
18928 KB |
Output is correct |
23 |
Correct |
131 ms |
22344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
13688 KB |
Output is correct |
2 |
Correct |
14 ms |
13688 KB |
Output is correct |
3 |
Correct |
14 ms |
13688 KB |
Output is correct |
4 |
Correct |
14 ms |
13560 KB |
Output is correct |
5 |
Correct |
14 ms |
13544 KB |
Output is correct |
6 |
Correct |
14 ms |
13688 KB |
Output is correct |
7 |
Correct |
14 ms |
13532 KB |
Output is correct |
8 |
Correct |
15 ms |
13660 KB |
Output is correct |
9 |
Correct |
16 ms |
13728 KB |
Output is correct |
10 |
Correct |
14 ms |
13560 KB |
Output is correct |
11 |
Correct |
24 ms |
15232 KB |
Output is correct |
12 |
Correct |
38 ms |
16248 KB |
Output is correct |
13 |
Correct |
64 ms |
29276 KB |
Output is correct |
14 |
Correct |
156 ms |
21616 KB |
Output is correct |
15 |
Correct |
169 ms |
21752 KB |
Output is correct |
16 |
Correct |
178 ms |
19724 KB |
Output is correct |
17 |
Correct |
145 ms |
18872 KB |
Output is correct |
18 |
Correct |
42 ms |
16024 KB |
Output is correct |
19 |
Correct |
136 ms |
21492 KB |
Output is correct |
20 |
Correct |
171 ms |
21628 KB |
Output is correct |
21 |
Correct |
134 ms |
19848 KB |
Output is correct |
22 |
Correct |
120 ms |
18928 KB |
Output is correct |
23 |
Correct |
131 ms |
22344 KB |
Output is correct |
24 |
Correct |
15 ms |
13560 KB |
Output is correct |
25 |
Correct |
116 ms |
15360 KB |
Output is correct |
26 |
Correct |
149 ms |
16220 KB |
Output is correct |
27 |
Correct |
2997 ms |
29280 KB |
Output is correct |
28 |
Correct |
1515 ms |
23008 KB |
Output is correct |
29 |
Correct |
3084 ms |
23268 KB |
Output is correct |
30 |
Correct |
1833 ms |
21056 KB |
Output is correct |
31 |
Correct |
1868 ms |
20532 KB |
Output is correct |
32 |
Correct |
534 ms |
16668 KB |
Output is correct |
33 |
Correct |
1525 ms |
23220 KB |
Output is correct |
34 |
Correct |
3013 ms |
23496 KB |
Output is correct |
35 |
Correct |
2074 ms |
21120 KB |
Output is correct |
36 |
Correct |
2487 ms |
20152 KB |
Output is correct |
37 |
Correct |
1047 ms |
23856 KB |
Output is correct |
38 |
Correct |
4093 ms |
34148 KB |
Output is correct |