Submission #511360

#TimeUsernameProblemLanguageResultExecution timeMemory
511360tabrTropical Garden (IOI11_garden)C++17
100 / 100
2569 ms23404 KiB
#include <bits/stdc++.h> using namespace std; #ifdef tabr #include "library/debug.cpp" #else #define debug(...) #endif #ifdef tabr void answer(int n) { cout << n << '\n'; } #else #include "garden.h" #include "gardenlib.h" #endif void count_routes(int n, int m, int p, int r[][2], int q, int k[]) { vector<vector<int>> g(n); for (int i = 0; i < m; i++) { g[r[i][0]].emplace_back(r[i][1]); g[r[i][1]].emplace_back(r[i][0]); } vector<int> nxt(2 * n); for (int i = 0; i < n; i++) { nxt[i] = g[i][0]; nxt[i + n] = (g[i].size() == 1 ? g[i][0] : g[i][1]); if (g[nxt[i]][0] == i) { nxt[i] += n; } if (g[nxt[i + n]][0] == i) { nxt[i + n] += n; } } g = vector<vector<int>>(2 * n); for (int i = 0; i < 2 * n; i++) { g[nxt[i]].emplace_back(i); } vector<int> a(2 * n, -1), b(2 * n, -1); a[p] = 0; b[p + n] = 0; queue<int> que; que.emplace(p); while (!que.empty()) { int v = que.front(); que.pop(); for (int to : g[v]) { if (a[to] == -1) { a[to] = a[v] + 1; que.emplace(to); } } } que.emplace(p + n); while (!que.empty()) { int v = que.front(); que.pop(); for (int to : g[v]) { if (b[to] == -1) { b[to] = b[v] + 1; que.emplace(to); } } } int c = 0; int v = p; do { v = nxt[v]; c++; } while (v != p && c < 2 * n + 1); int d = 0; v = p + n; do { v = nxt[v]; d++; } while (v != p + n && d < 2 * n + 1); debug(c, d); for (int i = 0; i < q; i++) { int ans = 0; for (int j = 0; j < n; j++) { if (c > 2 * n) { if (k[i] == a[j]) { ans++; } } else { if (a[j] != -1 && k[i] >= a[j] && (k[i] - a[j]) % c == 0) { ans++; } } if (d > 2 * n) { if (k[i] == b[j]) { ans++; } } else { if (b[j] != -1 && k[i] >= b[j] && (k[i] - b[j]) % d == 0) { ans++; } } } answer(ans); } } #ifdef tabr int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m, p, q; cin >> n >> m >> p >> q; int r[100][2]; int k[100]; for (int i = 0; i < m; i++) { cin >> r[i][0] >> r[i][1]; } for (int i = 0; i < q; i++) { cin >> k[i]; } count_routes(n, m, p, r, q, k); return 0; } #endif
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...