50分求调
  • 板块P5557 旅行
  • 楼主lzh009
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/3/17 20:18
  • 上次更新2024/3/17 22:18:24
查看原帖
50分求调
952814
lzh009楼主2024/3/17 20:18

只有50分

#include <iostream>
#include <vector>
#include <cmath>

int main() {
    int n, m;
    std::cin >> n;
    
    std::vector<int> a(n);
    for (int i = 0; i < n; i++) {
        std::cin >> a[i];
    }

    std::cin >> m;

    // Preprocess the final target city for each city and time
    std::vector<std::vector<int>> target(n, std::vector<int>(31));
    for (int i = 0; i < n; i++) {
        target[i][0] = a[i];
    }

    for (int j = 1; j <= 30; j++) {
        for (int i = 0; i < n; i++) {
            target[i][j] = target[target[i][j-1]-1][j-1];
        }
    }

    for (int i = 0; i < m; i++) {
        int S, t1, t2;
        std::cin >> S >> t1 >> t2;

        int target_city = S;
        for (int j = 0; j <= 30; j++) {
            if (t1 & (1 << j)) {
                target_city = target[target_city-1][j];
            }
        }

        // Output the target city after t1^t2 time units
        std::cout << target_city << std::endl;
    }

    return 0;
}

2024/3/17 20:18
加载中...