답안 #144712

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
144712 2019-08-17T14:40:49 Z darkkcyan 열대 식물원 (Tropical Garden) (IOI11_garden) C++14
100 / 100
158 ms 40364 KB
#include<bits/stdc++.h>
#include "garden.h"
#include "gardenlib.h"

using namespace std;
using namespace std::placeholders;

#define llong long long 
#define xx first
#define yy second
#define len(x) ((int)x.size())
#define rep(i,n) for (int i = -1; ++ i < n; )
#define rep1(i,n) for (int i = 0; i ++ < n; )
#define all(x) x.begin(), x.end()
// #define rand __rand
// mt19937 rng(chrono::system_clock::now().time_since_epoch().count());  // or mt19937_64
// template<class T = int> T rand(T range = numeric_limits<T>::max()) {
    // return (T)(rng() % range);
// }

void count_routes(int N, int M, int P, int R[][2], int Q, int G[]) {
    const int inf = 2 * N + 10;

    vector<int> next_node(N * 2, -1);
    rep(i, M) {
        int prio[] = {next_node[R[i][0] << 1] == -1, next_node[R[i][1] << 1] == -1};
        rep(f, 2) {
            int u = R[i][f], v = R[i][!f];
            bool v_prio = prio[!f];
            if (next_node[u << 1] == -1)
                next_node[u << 1] = v << 1 | v_prio; 
            else if (next_node[u << 1 | 1] == -1) 
                next_node[u << 1 | 1] = v << 1 | v_prio;
        }
    }

    rep(i, N) {
        if (next_node[i << 1] == -1) continue;
        if (next_node[i << 1 | 1] != -1) continue;
        next_node[i << 1 | 1] = next_node[i << 1] & ~1;
        if (next_node[next_node[i << 1 | 1]] / 2 == i)
            next_node[i << 1 | 1] |= 1;
        // clog << i << ' ' << next_node[i << 1 | 1] << endl;
    }

    // rep(i, N) {
        // clog << i << ": ";
        // rep(f, 2) clog << next_node[i << 1 | f] / 2 << "(" << next_node[i << 1 | f] % 2 << "); ";
        // clog << endl;
    // }

    vector<vector<int>> gr(2 * N);
    rep(i, 2 * N) {
        if (next_node[i] == -1) continue;
        gr[next_node[i]].emplace_back(i);
        // clog << next_node[i] << ' ' << i << endl;
    }

    auto bfs = [&](int root) -> vector<int> {
        vector<int> dis(2 * N, -1);
        queue<int> qu;
        for (qu.push(root), dis[root] = 0; len(qu); qu.pop()) {
            int u = qu.front();
            for (auto v: gr[u]) {
                if (dis[v] != -1) continue;
                dis[v] = dis[u] + 1;
                qu.push(v);
            }
        }
        return dis;
    };

    vector<int> dis_to_p[2];
    int cycle_length[2] = {inf, inf};
    rep(i, 2) {
        int u = P << 1 | i;
        dis_to_p[i] = bfs(u);
        if (next_node[u] == -1) continue;
        if (dis_to_p[i][next_node[u]] == -1) continue;
        cycle_length[i] = dis_to_p[i][next_node[u]] - dis_to_p[i][u] + 1;
    }

    vector<vector<int>> group[2];
    rep(i, 2) {
        group[i].resize(2 * N);
        for (int v = 0; v < 2 * N; v += 2) {
            int dis = dis_to_p[i][v];
            if (dis == -1) continue;
            group[i][dis % cycle_length[i]].emplace_back(dis);
        }

        for (auto& f: group[i]) 
            sort(f.begin(), f.end());
    }

    rep(i, Q) {
        int ans = 0;
        int query = G[i];
        rep(f, 2) {
            if (cycle_length[f] >= inf) {
                if (query >= inf) continue;
                ans += len(group[f][query]);
            } else {
                auto& cur_group = group[f][query % cycle_length[f]];
                auto iter = upper_bound(cur_group.begin(), cur_group.end(), query);
                ans += (int)(iter - cur_group.begin());
            }
            // clog << cycle_length[f] << ' ' << ans << endl;
        }
        answer(ans);
    }
    // clog << next_node[0] << ' ' << next_node[1] << endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 504 KB Output is correct
2 Correct 3 ms 504 KB Output is correct
3 Correct 3 ms 604 KB Output is correct
4 Correct 2 ms 348 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 3 ms 632 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 476 KB Output is correct
9 Correct 4 ms 632 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 504 KB Output is correct
2 Correct 3 ms 504 KB Output is correct
3 Correct 3 ms 604 KB Output is correct
4 Correct 2 ms 348 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 3 ms 632 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 476 KB Output is correct
9 Correct 4 ms 632 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 15 ms 5156 KB Output is correct
12 Correct 31 ms 8312 KB Output is correct
13 Correct 69 ms 25720 KB Output is correct
14 Correct 117 ms 28312 KB Output is correct
15 Correct 158 ms 29248 KB Output is correct
16 Correct 113 ms 20512 KB Output is correct
17 Correct 91 ms 17436 KB Output is correct
18 Correct 31 ms 8312 KB Output is correct
19 Correct 158 ms 28452 KB Output is correct
20 Correct 149 ms 29308 KB Output is correct
21 Correct 104 ms 20344 KB Output is correct
22 Correct 98 ms 17452 KB Output is correct
23 Correct 120 ms 31224 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 504 KB Output is correct
2 Correct 3 ms 504 KB Output is correct
3 Correct 3 ms 604 KB Output is correct
4 Correct 2 ms 348 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 3 ms 632 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 476 KB Output is correct
9 Correct 4 ms 632 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 15 ms 5156 KB Output is correct
12 Correct 31 ms 8312 KB Output is correct
13 Correct 69 ms 25720 KB Output is correct
14 Correct 117 ms 28312 KB Output is correct
15 Correct 158 ms 29248 KB Output is correct
16 Correct 113 ms 20512 KB Output is correct
17 Correct 91 ms 17436 KB Output is correct
18 Correct 31 ms 8312 KB Output is correct
19 Correct 158 ms 28452 KB Output is correct
20 Correct 149 ms 29308 KB Output is correct
21 Correct 104 ms 20344 KB Output is correct
22 Correct 98 ms 17452 KB Output is correct
23 Correct 120 ms 31224 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 16 ms 5240 KB Output is correct
26 Correct 32 ms 8260 KB Output is correct
27 Correct 70 ms 25652 KB Output is correct
28 Correct 114 ms 28304 KB Output is correct
29 Correct 147 ms 29304 KB Output is correct
30 Correct 111 ms 20440 KB Output is correct
31 Correct 96 ms 17500 KB Output is correct
32 Correct 30 ms 8312 KB Output is correct
33 Correct 117 ms 28344 KB Output is correct
34 Correct 151 ms 29408 KB Output is correct
35 Correct 110 ms 20476 KB Output is correct
36 Correct 106 ms 17528 KB Output is correct
37 Correct 122 ms 31316 KB Output is correct
38 Correct 131 ms 40364 KB Output is correct