#include <bits/stdc++.h>
#define int long long
#define FOR(var,bound) for(int var = 0; var < bound; var++)
#define FORB(var,lb,ub) for (int var = lb; var < ub; var++)
#define FORR(var,bound) for(int var = bound-1; var >= 0; var--)
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef pair<int, int> pii;
void answer(int32_t x);
int n, m, p;
vector<pii> conn[600000];
int jump[600000];
vi back[600000];
int visitedA[600000], visitedB[600000];
void count_routes(int32_t N, int32_t M, int32_t P, int32_t R[][2], int32_t Q, int32_t G[]) {
n = N;
m = M;
p = P;
FOR(i, m) {
conn[R[i][0]].push_back({i, R[i][1]});
conn[R[i][1]].push_back({i, R[i][0]});
}
FOR(i, n) {
sort(conn[i].begin(), conn[i].end());
}
FOR(i, n) {
sort(conn[i].begin(), conn[i].end());
jump[i*2] = conn[i][0].second * 2;
if (conn[jump[i*2]/2][0].first == conn[i][0].first) {
jump[i*2]++;
}
if (conn[i].size() == 1) {
jump[i*2+1] = jump[i*2];
} else {
jump[i*2+1] = conn[i][1].second * 2;
if (conn[jump[i*2+1]/2][0].first == conn[i][1].first) {
jump[i*2+1]++;
}
}
}
FOR(i, n * 2) {
back[jump[i]].push_back(i);
}
memset(visitedA, -1, sizeof(visitedA));
memset(visitedB, -1, sizeof(visitedB));
int period = 0;
queue<pii> q;
q.push({p*2, 0});
while (!q.empty()) {
auto [x, dist] = q.front(); q.pop();
if (visitedA[x] == -1) {
visitedA[x] = dist;
} else {
if (period == 0) {
period = dist - visitedA[x];
}
continue;
}
for (int c: back[x]) {
q.push({c, dist + 1});
}
}
q.push({p*2+1, 0});
while (!q.empty()) {
auto [x, dist] = q.front(); q.pop();
if (visitedB[x] == -1) {
visitedB[x] = dist;
} else {
if (period == 0) {
period = dist - visitedB[x];
}
continue;
}
for (int c: back[x]) {
q.push({c, dist + 1});
}
}
vi m(period);
vi times;
FOR(i, n) {
if (visitedA[i*2] != -1) times.push_back(visitedA[i*2]);
if (visitedB[i*2] != -1) times.push_back(visitedB[i*2]);
}
sort(times.begin(), times.end());
int ti = 0;
vi gc(Q);
FOR(i, Q) {
gc[i] = G[i];
}
sort(gc.begin(), gc.end());
map<int, int> res;
for (int x: gc) {
while (ti < times.size() && times[ti] <= x) {
m[times[ti++] % period]++;
}
res[x] = m[x%period];
}
FOR(i, Q) {
//cout << res[g[i]] << " ";
answer(res[G[i]]);
}
}
//#define LOCAL_TEST
#ifdef LOCAL_TEST
void answer(int32_t x) {
//cout << "ANSWER: " << x << endl;
cout << x << " ";
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m, p;
cin >> n >> m >> p;
vector<int32_t[2]> R(m);
FOR(i, m) {
cin >> R[i][0] >> R[i][1];
}
int q;
cin >> q;
vector<int32_t> G(q);
FOR(i, q) {
cin >> G[i];
}
count_routes(n, m, p, R.data(), q, G.data());
cout << endl;
}
#endif
Compilation message
garden.cpp: In function 'void count_routes(int32_t, int32_t, int32_t, int32_t (*)[2], int32_t, int32_t*)':
garden.cpp:127:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
127 | while (ti < times.size() && times[ti] <= x) {
| ~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
37972 KB |
Output is correct |
2 |
Correct |
18 ms |
37932 KB |
Output is correct |
3 |
Correct |
16 ms |
38056 KB |
Output is correct |
4 |
Correct |
16 ms |
37844 KB |
Output is correct |
5 |
Correct |
16 ms |
37844 KB |
Output is correct |
6 |
Correct |
17 ms |
38028 KB |
Output is correct |
7 |
Correct |
16 ms |
37928 KB |
Output is correct |
8 |
Correct |
17 ms |
37972 KB |
Output is correct |
9 |
Correct |
18 ms |
38356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
37972 KB |
Output is correct |
2 |
Correct |
18 ms |
37932 KB |
Output is correct |
3 |
Correct |
16 ms |
38056 KB |
Output is correct |
4 |
Correct |
16 ms |
37844 KB |
Output is correct |
5 |
Correct |
16 ms |
37844 KB |
Output is correct |
6 |
Correct |
17 ms |
38028 KB |
Output is correct |
7 |
Correct |
16 ms |
37928 KB |
Output is correct |
8 |
Correct |
17 ms |
37972 KB |
Output is correct |
9 |
Correct |
18 ms |
38356 KB |
Output is correct |
10 |
Correct |
16 ms |
37844 KB |
Output is correct |
11 |
Correct |
24 ms |
40660 KB |
Output is correct |
12 |
Correct |
37 ms |
43024 KB |
Output is correct |
13 |
Correct |
51 ms |
52696 KB |
Output is correct |
14 |
Correct |
93 ms |
53984 KB |
Output is correct |
15 |
Correct |
99 ms |
56256 KB |
Output is correct |
16 |
Correct |
82 ms |
51892 KB |
Output is correct |
17 |
Incorrect |
81 ms |
51228 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
37972 KB |
Output is correct |
2 |
Correct |
18 ms |
37932 KB |
Output is correct |
3 |
Correct |
16 ms |
38056 KB |
Output is correct |
4 |
Correct |
16 ms |
37844 KB |
Output is correct |
5 |
Correct |
16 ms |
37844 KB |
Output is correct |
6 |
Correct |
17 ms |
38028 KB |
Output is correct |
7 |
Correct |
16 ms |
37928 KB |
Output is correct |
8 |
Correct |
17 ms |
37972 KB |
Output is correct |
9 |
Correct |
18 ms |
38356 KB |
Output is correct |
10 |
Correct |
16 ms |
37844 KB |
Output is correct |
11 |
Correct |
24 ms |
40660 KB |
Output is correct |
12 |
Correct |
37 ms |
43024 KB |
Output is correct |
13 |
Correct |
51 ms |
52696 KB |
Output is correct |
14 |
Correct |
93 ms |
53984 KB |
Output is correct |
15 |
Correct |
99 ms |
56256 KB |
Output is correct |
16 |
Correct |
82 ms |
51892 KB |
Output is correct |
17 |
Incorrect |
81 ms |
51228 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |