/*input
*/
#include <bits/stdc++.h>
#include "garden.h"
#include "gardenlib.h"
using namespace std;
namespace my_template {
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef vector<pi> vpi;
typedef vector<vpi> vpii;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
typedef vector<pd> vpd;
typedef vector<bool> vb;
typedef vector<vb> vbb;
typedef std::string str;
typedef std::vector<str> vs;
#define x first
#define y second
#define debug(...) cout<<"["<<#__VA_ARGS__<<": "<<__VA_ARGS__<<"]\n"
const ld PI = 3.14159265358979323846264338327950288419716939937510582097494L;
template<typename T>
pair<T, T> operator+(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x + b.x, a.y + b.y); }
template<typename T>
pair<T, T> operator-(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x - b.x, a.y - b.y); }
template<typename T>
T operator*(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.x + a.y * b.y); }
template<typename T>
T operator^(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.y - a.y * b.x); }
template<typename T>
void print(vector<T> vec, string name = "") {
cout << name;
for (auto u : vec)
cout << u << ' ';
cout << '\n';
}
}
using namespace my_template;
const int MOD = 1000000007;
const ll INF = std::numeric_limits<ll>::max();
const int MX = 300101;
vii all(MX);
int n, m, p;
vii edges(MX);
vii revEdges(MX);
vii dis(2);
// 0 - negaliu ilgiausio // atejo is ilgiausio
// 1 - galiu ilgiausia
void makeEdges() {
for (int i = 0; i < n; ++i)
{
// 0
int k;
if (all[i].size() == 2) {
k = all[i][1];
} else {
k = all[i][0];
}
edges[i * 2].emplace_back(2 * k + int(all[k][0] != i));
revEdges[2 * k + int(all[k][0] != i)].emplace_back(i * 2);
// printf("%d %d\n", i * 2, 2 * k + int(all[k][0] != i));
k = all[i][0];
edges[i * 2 + 1].emplace_back(2 * k + int(all[k][0] != i));
revEdges[2 * k + int(all[k][0] != i)].emplace_back(i * 2 + 1);
// printf("%d %d\n", i * 2 + 1, 2 * k + int(all[k][0] != i));
}
}
void makeDis() {
dis[0] = dis[1] = vi(2 * n, MOD);
dis[0][2 * p] = 0;
queue<int> q;
q.push(2 * p);
while (q.size()) {
int c = q.front(); q.pop();
for (auto u : revEdges[c]) {
if (dis[0][u] == MOD) {
dis[0][u] = dis[0][c] + 1;
q.push(u);
}
}
}
q.push(2 * p + 1);
dis[1][2 * p + 1] = 0;
while (q.size()) {
int c = q.front(); q.pop();
for (auto u : revEdges[c]) {
if (dis[1][u] == MOD) {
dis[1][u] = dis[1][c] + 1;
q.push(u);
}
}
}
}
int start;
int len = 0;
vb been;
bool find_cycle(int x) {
if (x == start and been[start]) {
// found
return true;
}
int u = edges[x][0];
if (!been[u]) {
been[u] = true;
if (find_cycle(u)) {
len++;
return true;
}
}
return false;
}
void count_routes(int N, int M, int P, int R[][2], int Q, int G[])
{
n = N;
m = M;
p = P;
for (int i = 0; i < M; ++i)
{
int a = R[i][0];
int b = R[i][1];
if (all[a].size() < 2)
all[a].emplace_back(b);
if (all[b].size() < 2)
all[b].emplace_back(a);
}
makeEdges();
makeDis();
int len0 = 0;
int len1 = 0;
{
been = vb(2 * N, false);
start = 2 * p;
len = 0;
find_cycle(2 * p);
// assert(len);
len0 = (len ? len : MOD);
// printf("len = %d\n", len);
been = vb(2 * N, false);
start = 2 * p + 1;
len = 0;
find_cycle(2 * p + 1);
// assert(len);
len1 = (len ? len : MOD);
// printf("len = %d\n", len);
}
// vi visi;
map<int, int> kiek0;
map<int, int> kiek1;
vi visi0;
vi visi1;
for (int i = 1; i < 2 * N; i += 2)
{
if (dis[0][i] != MOD) {
// kiek0[dis[0][i]]++;
visi0.emplace_back(dis[0][i]);
// printf("0 dis = %d\n", dis[0][i]);
// visi.emplace_back();
}
if (dis[1][i] != MOD) {
// kiek1[dis[1][i]]++;
// printf("1 dis = %d\n", dis[1][i]);
visi1.emplace_back(dis[1][i]);
// visi.emplace_back(dis[1][i]);
}
}
sort(visi0.begin(), visi0.end());
sort(visi1.begin(), visi1.end());
vi ind(Q); for (int i = 0; i < Q; ++i) ind[i] = i;
sort(ind.begin(), ind.end(), [&](const int &a, const int &b) {
return G[a] < G[b];
});
vi ats(Q);
int j0 = 0;
int j1 = 0;
for (int i = 0; i < Q; ++i)
{
while (j0 < (int)visi0.size() and visi0[j0] <= G[ind[i]]) {
kiek0[(visi0[j0]) % len0]++;
j0++;
}
while (j1 < (int)visi1.size() and visi1[j1] <= G[ind[i]]) {
kiek1[(visi1[j1]) % len1]++;
j1++;
}
ats[ind[i]] = kiek0[G[ind[i]] % len0] + kiek1[G[ind[i]] % len1];
}
// print(visi);
// printf("len0 = %d, len1 = %d\n", len0, len1);
for (int i = 0; i < Q; i++) {
// int ret = kiek0[G[i] % len0] + kiek1[G[i] % len1];
answer(ats[i]);
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
21632 KB |
Output is correct |
2 |
Correct |
13 ms |
21632 KB |
Output is correct |
3 |
Correct |
12 ms |
21632 KB |
Output is correct |
4 |
Correct |
12 ms |
21504 KB |
Output is correct |
5 |
Correct |
14 ms |
21504 KB |
Output is correct |
6 |
Correct |
13 ms |
21632 KB |
Output is correct |
7 |
Correct |
13 ms |
21504 KB |
Output is correct |
8 |
Correct |
13 ms |
21632 KB |
Output is correct |
9 |
Correct |
14 ms |
21760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
21632 KB |
Output is correct |
2 |
Correct |
13 ms |
21632 KB |
Output is correct |
3 |
Correct |
12 ms |
21632 KB |
Output is correct |
4 |
Correct |
12 ms |
21504 KB |
Output is correct |
5 |
Correct |
14 ms |
21504 KB |
Output is correct |
6 |
Correct |
13 ms |
21632 KB |
Output is correct |
7 |
Correct |
13 ms |
21504 KB |
Output is correct |
8 |
Correct |
13 ms |
21632 KB |
Output is correct |
9 |
Correct |
14 ms |
21760 KB |
Output is correct |
10 |
Correct |
12 ms |
21504 KB |
Output is correct |
11 |
Correct |
26 ms |
25216 KB |
Output is correct |
12 |
Correct |
46 ms |
27384 KB |
Output is correct |
13 |
Correct |
123 ms |
48192 KB |
Output is correct |
14 |
Correct |
182 ms |
42232 KB |
Output is correct |
15 |
Correct |
219 ms |
43544 KB |
Output is correct |
16 |
Correct |
147 ms |
36616 KB |
Output is correct |
17 |
Correct |
137 ms |
34440 KB |
Output is correct |
18 |
Correct |
49 ms |
27384 KB |
Output is correct |
19 |
Correct |
160 ms |
42232 KB |
Output is correct |
20 |
Correct |
203 ms |
43032 KB |
Output is correct |
21 |
Correct |
162 ms |
36596 KB |
Output is correct |
22 |
Correct |
137 ms |
34296 KB |
Output is correct |
23 |
Correct |
171 ms |
44280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
21632 KB |
Output is correct |
2 |
Correct |
13 ms |
21632 KB |
Output is correct |
3 |
Correct |
12 ms |
21632 KB |
Output is correct |
4 |
Correct |
12 ms |
21504 KB |
Output is correct |
5 |
Correct |
14 ms |
21504 KB |
Output is correct |
6 |
Correct |
13 ms |
21632 KB |
Output is correct |
7 |
Correct |
13 ms |
21504 KB |
Output is correct |
8 |
Correct |
13 ms |
21632 KB |
Output is correct |
9 |
Correct |
14 ms |
21760 KB |
Output is correct |
10 |
Correct |
12 ms |
21504 KB |
Output is correct |
11 |
Correct |
26 ms |
25216 KB |
Output is correct |
12 |
Correct |
46 ms |
27384 KB |
Output is correct |
13 |
Correct |
123 ms |
48192 KB |
Output is correct |
14 |
Correct |
182 ms |
42232 KB |
Output is correct |
15 |
Correct |
219 ms |
43544 KB |
Output is correct |
16 |
Correct |
147 ms |
36616 KB |
Output is correct |
17 |
Correct |
137 ms |
34440 KB |
Output is correct |
18 |
Correct |
49 ms |
27384 KB |
Output is correct |
19 |
Correct |
160 ms |
42232 KB |
Output is correct |
20 |
Correct |
203 ms |
43032 KB |
Output is correct |
21 |
Correct |
162 ms |
36596 KB |
Output is correct |
22 |
Correct |
137 ms |
34296 KB |
Output is correct |
23 |
Correct |
171 ms |
44280 KB |
Output is correct |
24 |
Correct |
15 ms |
21504 KB |
Output is correct |
25 |
Correct |
28 ms |
25472 KB |
Output is correct |
26 |
Correct |
45 ms |
28024 KB |
Output is correct |
27 |
Correct |
120 ms |
49344 KB |
Output is correct |
28 |
Correct |
164 ms |
43896 KB |
Output is correct |
29 |
Correct |
199 ms |
44700 KB |
Output is correct |
30 |
Correct |
153 ms |
38280 KB |
Output is correct |
31 |
Correct |
136 ms |
36104 KB |
Output is correct |
32 |
Correct |
50 ms |
28280 KB |
Output is correct |
33 |
Correct |
163 ms |
43896 KB |
Output is correct |
34 |
Correct |
213 ms |
45204 KB |
Output is correct |
35 |
Correct |
157 ms |
38280 KB |
Output is correct |
36 |
Correct |
147 ms |
36216 KB |
Output is correct |
37 |
Correct |
188 ms |
46120 KB |
Output is correct |
38 |
Correct |
188 ms |
60448 KB |
Output is correct |