#include <bits/stdc++.h>
#include "garden.h"
#include "gardenlib.h"
using namespace std;
#define fin cin
#define fout cout
//ifstream fin("x.in"); ofstream fout("x.out");
typedef long long i64;
const int nmax = 15e4 + 5;
const int mmax = 2 * 15e4 + nmax + 5;
const int qmax = 2000;
const pair<int, int> nul = {-1, -1};
static vector<int> c[mmax + 1];
static pair<int, int> pe_ciclu[mmax + 1];
static int reprezentant[mmax + 1], h[mmax + 1];
static bool finalok[mmax + 1];
static vector<int> g[mmax + 1], gt[mmax + 1];
static vector<pair<int, int>> gorig[nmax + 1];
static int top, stk[mmax + 1];
static vector<pair<int, int>> qry[mmax + 1];
static int rasp[qmax + 1];
bool cmp (pair<int, int> a, pair<int, int> b) {
return a.second < b.second;
}
int best (int nod, int indm) {
if (gorig[nod].size() == 1)
return gorig[nod][0].second;
if (gorig[nod][0].second / 2 != indm / 2)
return gorig[nod][0].second;
else
return gorig[nod][1].second;
}
void fa_graf (int N, int &M, int P, int R[][2], int Q, int G[]) {
for (int i = 0; i < M; ++ i) {
gorig[R[i][0]].push_back({R[i][1], 2 * i});
gorig[R[i][1]].push_back({R[i][0], 2 * i + 1});
if (R[i][1] == P)
finalok[2 * i] = 1;
if (R[i][0] == P)
finalok[2 * i + 1] = 1;
}
for (int i = 0; i < N; ++ i) {
sort(gorig[i].begin(), gorig[i].end(), cmp);
}
// muchiile de functie
for (int i = 0; i < M; ++ i) {
int find_edge = best(R[i][1], 2 * i);
g[2 * i].push_back(find_edge);
find_edge = best(R[i][0], 2 * i + 1);
g[2 * i + 1].push_back(find_edge);
}
// 0 ... 2M - 1 sunt muchiile
// 2M... 2M + N - 1 sunt nodurile de start
// adauga noduri suplimentare de start
for (int i = 0; i < N; ++ i) {
if (gorig[i].size())
g[2 * M + i].push_back(gorig[i][0].second);
}
M *= 2;
}
void ciclu (int nod, int tata, int ind) {
c[ind].push_back(nod);
pe_ciclu[nod] = {ind, c[ind].size() - 1};
for (auto i : g[nod]) {
if (pe_ciclu[i] != nul && pe_ciclu[i].first != ind) {
ciclu(i, nod, ind);
}
}
}
int cauta_ciclii (int N) {
// sortare topologica ca sa scot arborii
vector<int> grad(N, 0);
for (int i = 0; i < N; ++ i) {
for (auto j : g[i])
++ grad[j];
}
queue<int> q;
for (int i = 0; i < N; ++ i) {
if (grad[i] == 0)
q.push(i);
}
while (!q.empty()) {
int x = q.front();
q.pop();
pe_ciclu[x] = {-1, -1};
for (auto i : g[x]) {
-- grad[i];
if (grad[i] == 0)
q.push(i);
}
}
// determin care sunt ciclii
int cycles = 0;
for (int i = 0; i < N; ++ i) {
if (pe_ciclu[i] != nul && pe_ciclu[i].first == 0) {
++ cycles;
ciclu(i, -1, cycles);
}
}
return cycles;
}
void dfs (int nod, int tata) {
for (auto i : gt[nod]) {
if (pe_ciclu[i] == nul && i != tata) {
reprezentant[i] = reprezentant[nod];
h[i] = h[nod] + 1;
dfs(i, nod);
}
}
}
void det_arbori (int N, int cnt) {
for (int i = 0; i < N; ++ i) {
for (auto j : g[i]) {
gt[j].push_back(i);
}
}
for (int u = 1; u <= cnt; ++ u) {
for (auto i : c[u]) {
reprezentant[i] = i;
dfs(i, -1);
}
}
}
void solve (int nod, int tata) {
stk[top ++] = nod;
for (auto i : qry[nod]) {
rasp[i.second] += finalok[stk[i.first]];
}
for (auto i : gt[nod]) {
if (pe_ciclu[i] == nul && i != tata) {
solve(i, nod);
}
}
-- top;
}
void count_routes(int N, int M, int P, int R[][2], int Q, int G[])
{
fa_graf(N, M, P, R, Q, G);
int cnt = cauta_ciclii(M + N);
det_arbori(M + N, cnt);
for (int j = M; j < M + N; ++ j) {
for (int i = 0; i < Q; ++ i) {
// pot ajunge de la j in g[i] pasi la o muchie care da in nodul P?
if (G[i] >= h[j]) { // intru pe ciclu
pair<int, int> pos = pe_ciclu[reprezentant[j]];
int final_node = c[pos.first][(pos.second + G[i] - h[j]) % c[pos.first].size()];
if (finalok[final_node] == 1)
++ rasp[i];
} else { // tre sa ridic j cu G[i]
qry[j].push_back({h[j] - G[i], i});
}
}
}
for (int i = 1; i <= cnt; ++ i) {
for (auto j : c[i]) {
solve(j, -1);
}
}
for(int i = 0; i < Q; i ++)
answer(rasp[i]);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
46456 KB |
Output is correct |
2 |
Correct |
45 ms |
46372 KB |
Output is correct |
3 |
Correct |
45 ms |
46456 KB |
Output is correct |
4 |
Correct |
44 ms |
46128 KB |
Output is correct |
5 |
Correct |
44 ms |
46300 KB |
Output is correct |
6 |
Correct |
46 ms |
46696 KB |
Output is correct |
7 |
Correct |
44 ms |
46356 KB |
Output is correct |
8 |
Correct |
45 ms |
46444 KB |
Output is correct |
9 |
Correct |
51 ms |
47476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
46456 KB |
Output is correct |
2 |
Correct |
45 ms |
46372 KB |
Output is correct |
3 |
Correct |
45 ms |
46456 KB |
Output is correct |
4 |
Correct |
44 ms |
46128 KB |
Output is correct |
5 |
Correct |
44 ms |
46300 KB |
Output is correct |
6 |
Correct |
46 ms |
46696 KB |
Output is correct |
7 |
Correct |
44 ms |
46356 KB |
Output is correct |
8 |
Correct |
45 ms |
46444 KB |
Output is correct |
9 |
Correct |
51 ms |
47476 KB |
Output is correct |
10 |
Correct |
45 ms |
46252 KB |
Output is correct |
11 |
Correct |
75 ms |
51832 KB |
Output is correct |
12 |
Correct |
141 ms |
57416 KB |
Output is correct |
13 |
Correct |
137 ms |
74856 KB |
Output is correct |
14 |
Correct |
433 ms |
79192 KB |
Output is correct |
15 |
Correct |
450 ms |
80252 KB |
Output is correct |
16 |
Correct |
395 ms |
74464 KB |
Output is correct |
17 |
Correct |
371 ms |
72804 KB |
Output is correct |
18 |
Correct |
144 ms |
57152 KB |
Output is correct |
19 |
Correct |
437 ms |
79100 KB |
Output is correct |
20 |
Correct |
451 ms |
80276 KB |
Output is correct |
21 |
Correct |
392 ms |
74896 KB |
Output is correct |
22 |
Correct |
379 ms |
73580 KB |
Output is correct |
23 |
Correct |
469 ms |
82404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
46456 KB |
Output is correct |
2 |
Correct |
45 ms |
46372 KB |
Output is correct |
3 |
Correct |
45 ms |
46456 KB |
Output is correct |
4 |
Correct |
44 ms |
46128 KB |
Output is correct |
5 |
Correct |
44 ms |
46300 KB |
Output is correct |
6 |
Correct |
46 ms |
46696 KB |
Output is correct |
7 |
Correct |
44 ms |
46356 KB |
Output is correct |
8 |
Correct |
45 ms |
46444 KB |
Output is correct |
9 |
Correct |
51 ms |
47476 KB |
Output is correct |
10 |
Correct |
45 ms |
46252 KB |
Output is correct |
11 |
Correct |
75 ms |
51832 KB |
Output is correct |
12 |
Correct |
141 ms |
57416 KB |
Output is correct |
13 |
Correct |
137 ms |
74856 KB |
Output is correct |
14 |
Correct |
433 ms |
79192 KB |
Output is correct |
15 |
Correct |
450 ms |
80252 KB |
Output is correct |
16 |
Correct |
395 ms |
74464 KB |
Output is correct |
17 |
Correct |
371 ms |
72804 KB |
Output is correct |
18 |
Correct |
144 ms |
57152 KB |
Output is correct |
19 |
Correct |
437 ms |
79100 KB |
Output is correct |
20 |
Correct |
451 ms |
80276 KB |
Output is correct |
21 |
Correct |
392 ms |
74896 KB |
Output is correct |
22 |
Correct |
379 ms |
73580 KB |
Output is correct |
23 |
Correct |
469 ms |
82404 KB |
Output is correct |
24 |
Correct |
49 ms |
46164 KB |
Output is correct |
25 |
Correct |
658 ms |
52200 KB |
Output is correct |
26 |
Correct |
1121 ms |
57956 KB |
Output is correct |
27 |
Correct |
2543 ms |
75416 KB |
Output is correct |
28 |
Correct |
4024 ms |
79864 KB |
Output is correct |
29 |
Correct |
4301 ms |
81136 KB |
Output is correct |
30 |
Correct |
2808 ms |
75632 KB |
Output is correct |
31 |
Correct |
2520 ms |
73804 KB |
Output is correct |
32 |
Correct |
1133 ms |
58544 KB |
Output is correct |
33 |
Correct |
4125 ms |
80008 KB |
Output is correct |
34 |
Correct |
4306 ms |
81020 KB |
Output is correct |
35 |
Correct |
2784 ms |
75340 KB |
Output is correct |
36 |
Correct |
2514 ms |
73908 KB |
Output is correct |
37 |
Correct |
4374 ms |
82852 KB |
Output is correct |
38 |
Execution timed out |
5065 ms |
89608 KB |
Time limit exceeded |