# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
404881 | Joshc | Tropical Garden (IOI11_garden) | C++11 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "garden.h"
#include "gardenlib.h"
#include <algorithm>
#include <vector>
using namespace std;
const int INF = 2000000000;
vector<int> edges[150001], edges2[300001], num[300001], num2[300001];
int dist[300001], dist2[300001], m[300001], m2[300001], x = 0, y = 0;
void dfs(int v, int p, int cur, int dist[], int& len) {
dist[v] = cur;
for (int i : edges2[v]) {
if (i == p) continue;
if (dist[i] != INF) len = cur+1;
else dfs(i, v, cur+1, dist, len);
}
}
void count_routes(int n, int z, int p, int r[][2], int q, int g[]) {
int n, z, p, q, a, b;
for (int i=0; i<z; i++) {
edges[r[i][0]].push_back(r[i][1]);
edges[r[i][1]].push_back(r[i][0]);
}
for (int i=0; i<n; i++) {
edges2[edges[i][0]*2 + (i == edges[edges[i][0]][0])].push_back(i*2);
if (edges[i].size() == 1) edges2[edges[i][0]*2 + (i == edges[edges[i][0]][0])].push_back(i*2+1);
else edges2[edges[i][1]*2 + (i == edges[edges[i][1]][0])].push_back(i*2+1);
}
fill(&dist[0], &dist[300001], INF);
fill(&dist2[0], &dist2[300001], INF);
dfs(p*2, p*2, 0, dist, x);
dfs(p*2+1, p*2+1, 0, dist2, y);
for (int i=0; i<2*n; i+=2) {
if (dist[i] != INF) {
m[dist[i]]++;
if (x) num[dist[i]%x].push_back(dist[i]);
}
if (dist2[i] != INF) {
m2[dist2[i]]++;
if (y) num2[dist2[i]%y].push_back(dist2[i]);
}
}
for (int i=0; i<2*n; i++) {
sort(num[i].begin(), num[i].end());
sort(num2[i].begin(), num2[i].end());
}
for (int i=0; i<q; i++) {
int a = g[i];
int cur = x ? upper_bound(num[a%x].begin(), num[a%x].end(), a)-num[a%x].begin() : (a < 2*n ? m[a] : 0);
int cur2 = y ? upper_bound(num2[a%y].begin(), num2[a%y].end(), a)-num2[a%y].begin() : (a < 2*n ? m2[a] : 0);
answer(cur+cur2);
}
}