답안 #1022666

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1022666 2024-07-13T22:01:42 Z PanosPask 열대 식물원 (Tropical Garden) (IOI11_garden) C++14
100 / 100
61 ms 22824 KB
#include "garden.h"
#include "gardenlib.h"
#include <bits/stdc++.h>
#define pb push_back

using namespace std;

typedef pair<int, int> pi;

const int MAXUP = 30;

int N, Q;
vector<pi> queries;
vector<vector<int>> adj_list;

/*target[up][x]
 * x = 2 * i: The 2^up target of i if we reached i without going through the most beautiful path.
 * x = 2 * i + 1: The 2^up target of i if we reached i by going through the most beautiful path. 
 */
vector<int> target;

/*dist[i]:
 *Distance from i to the current destination (either 2 * P or 2 * P + 1)
 */
vector<int> dist;

// Variables used for cycle finding
vector<bool> on_stack;
vector<bool> visited;
int cur = 0, len = 0;

// Variables used for finding number of nodes that end at dest after some moves
vector<int> freq;
vector<int> freqmodlen;

// Final answer for each query
vector<int> ans;

// Returns distance from node to dest (2 * N if dest cannot be reached from node)
int dfs(int node, int dest)
{
    if (visited[node]) {
        if (on_stack[node]) {
            // New cycle
            // Check if destination is inside this new cycle
            // Since dfs started on dest, the completed cycle must also end on dest

            if (node == dest) {
                // dest belongs to this cycle
                len = cur;
                return 0;
            }
            else {
                // dest does not belong to a cycle
                return 2 * N;
            }
        }
        else {
            return dist[node];
        }
    }

    visited[node] = on_stack[node] = true;
    cur++;

    dist[node] = min(2 * N, dfs(target[node], dest) + 1);
    if (node == dest) {
        dist[node] = 0;
    }

    if (node % 2 == 0) {
        // We only care about even numbered nodes because they represent the starting positions
        freq[dist[node]]++;
    }

    on_stack[node] = false;
    cur--;

    return dist[node];
}

void process_destination(int t)
{
    visited.assign(2 * N, false);
    on_stack.assign(2 * N, false);
    dist.assign(2 * N + 1, 2 * N);
    len = cur = 0;
    freq.assign(2 * N + 1, 0);

    dfs(t, t);
    for (int i = 0; i < 2 * N; i++) {
        if (!visited[i]) {
            dfs(i, t);
        }
    }

    freqmodlen.assign(len, 0);

    int steps = 0;
    for (int i = 0; i < Q; i++) {
        while (steps < min(2 * N, queries[i].first)) {
            // Add new step
            if (len) {
                freqmodlen[steps % len] += freq[steps];
            }
            steps++;
        }

        if (queries[i].first < 2 * N) {
            ans[queries[i].second] += freq[queries[i].first];
        }
        if (len) {
            ans[queries[i].second] += freqmodlen[queries[i].first % len];
        }
    }
}

void add_target(int fr, int to)
{
    if (adj_list[to][0] == fr / 2) {
        target[fr] = 2 * to + 1;
    }
    else {
        target[fr] = 2 * to;
    }
}

void count_routes(int n, int M, int P, int R[][2], int q, int G[])
{
    N = n;
    Q = q;

    queries.resize(Q);
    ans.assign(Q, 0);
    adj_list.resize(N);
    target.resize(2 * N);

    for (int i = 0; i < Q; i++) {
        queries[i] = make_pair(G[i], i);
    }
    sort(queries.begin(), queries.end());

    for (int i = 0; i < M; i++) {
        adj_list[R[i][0]].pb(R[i][1]);
        adj_list[R[i][1]].pb(R[i][0]);
    }


    for (int i = 0; i < N; i++) {
        add_target(2 * i, adj_list[i][0]);

        if (adj_list[i].size() >= 2) {
            add_target(2 * i + 1, adj_list[i][1]);
        }
        else {
            add_target(2 * i + 1, adj_list[i][0]);
        }
    }

    process_destination(2 * P);
    process_destination(2 * P + 1);

    for (int i = 0; i < Q; i++) {
        answer(ans[i]);
    }
}


# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 2 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 2 ms 604 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 8 ms 2652 KB Output is correct
12 Correct 18 ms 4952 KB Output is correct
13 Correct 25 ms 17236 KB Output is correct
14 Correct 48 ms 13872 KB Output is correct
15 Correct 61 ms 14164 KB Output is correct
16 Correct 39 ms 10880 KB Output is correct
17 Correct 36 ms 9552 KB Output is correct
18 Correct 15 ms 4444 KB Output is correct
19 Correct 44 ms 13940 KB Output is correct
20 Correct 61 ms 14160 KB Output is correct
21 Correct 43 ms 10576 KB Output is correct
22 Correct 39 ms 9556 KB Output is correct
23 Correct 51 ms 15444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 2 ms 604 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 8 ms 2652 KB Output is correct
12 Correct 18 ms 4952 KB Output is correct
13 Correct 25 ms 17236 KB Output is correct
14 Correct 48 ms 13872 KB Output is correct
15 Correct 61 ms 14164 KB Output is correct
16 Correct 39 ms 10880 KB Output is correct
17 Correct 36 ms 9552 KB Output is correct
18 Correct 15 ms 4444 KB Output is correct
19 Correct 44 ms 13940 KB Output is correct
20 Correct 61 ms 14160 KB Output is correct
21 Correct 43 ms 10576 KB Output is correct
22 Correct 39 ms 9556 KB Output is correct
23 Correct 51 ms 15444 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 9 ms 2764 KB Output is correct
26 Correct 16 ms 4728 KB Output is correct
27 Correct 28 ms 17492 KB Output is correct
28 Correct 48 ms 13912 KB Output is correct
29 Correct 50 ms 14164 KB Output is correct
30 Correct 39 ms 10836 KB Output is correct
31 Correct 37 ms 9776 KB Output is correct
32 Correct 15 ms 4528 KB Output is correct
33 Correct 51 ms 13980 KB Output is correct
34 Correct 48 ms 14160 KB Output is correct
35 Correct 40 ms 10836 KB Output is correct
36 Correct 37 ms 9556 KB Output is correct
37 Correct 48 ms 15604 KB Output is correct
38 Correct 47 ms 22824 KB Output is correct