# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
286816 | ChrisT | 열대 식물원 (Tropical Garden) (IOI11_garden) | C++17 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#include "garden.h"
using namespace std;
void count_routes(int n, int m, int p, int r[][2], int q, int g[]) {
vector<int> togo(2*n+1,-1);
for (int i = 0; i < m; i++) {
auto [a,b] = r[i];
if (!~togo[2*a]) togo[2*a] = 2*b+(!~togo[2*b]);
else if (!~togo[2*a+1]) togo[2*a+1] = 2*b+(!~togo[2*b]);
if (!~togo[2*b]) togo[2*b] = 2*a+(togo[2*a]/2==b);
else if (!~togo[2*b+1]) togo[2*b+1] = 2*a+(togo[2*a]/2==b);
}
n *= 2;
vector<vector<int>> adj(n+1);
for (int i = 0; i < n; i++) {
if (!~togo[i]) togo[i] = togo[i-1];
adj[togo[i]].push_back(i);
}
vector<int> cmp(n+1), dist(n+1), to(n+1,-1), ind(n+1); vector<vector<int>> cyc(n+1); int cc = 0;
for (int i = 0; i < n; i++) if (!cmp[i]) {
int cur = i; ++cc;
while (!cmp[cur]) {
cmp[cur] = cc, cur = togo[cur];
}
int o = cur; queue<int> qq;
do {
ind[cur] = (int)cyc[cc].size(); cyc[cc].push_back(cur); to[cur] = cur; qq.push(cur);
} while ((cur = togo[cur]) != o);
while (!qq.empty()) {
int cur = qq.front(); qq.pop();
for (int j : adj[cur]) if (!~to[j]) {
to[j] = to[cur]; cmp[j] = cc;
dist[j] = dist[cur] + 1;
qq.push(j);
}
}
}
array<vector<int>,2> temp;
for (int i = 2*p; i <= 2*p+1; i++) if (dist[i]) {
queue<int> qq; qq.push(i); int par = i % 2;
temp[par].resize(n);
fill(temp[par].begin(),temp[par].end(),1e9);
temp[par][i] = 0;
while (!qq.empty()) {
int cur = qq.front(); qq.pop();
for (int j : adj[cur]) if (temp[par][cur] + 1 < temp[par][j]) {
temp[par][j] = temp[par][cur] + 1;
qq.push(j);
}
}
}
auto get = [&] (int x, int k) {
int ret = 0;
if (dist[x]) {
for (int i = 0; i < n; i+=2) ret += temp[x%2][i] == k;
} else {
for (int i = 0; i < n; i+=2) if (dist[i] <= k) {
int go = k - dist[i], id = (ind[to[i]] + go) % ((int)cyc[cmp[i]].size());
ret += cyc[cmp[i]][id] == x;
}
}
return ret;
};
for (int i = 0; i < q; i++) answer(get(2*p,g[i])+get(2*p+1,g[i]));
}