E - 伝言リレー / Message Relay 解説 by admin
gpt-5.5-highOverview
By following the pass destinations \(P_i\), each quiz travels along a fixed single path, and the first student \(j\) encountered who satisfies \(S_j \geq D_i\) will solve it.
For each starting position \(i\), we want to quickly find the first student on this path who satisfies the condition.
Analysis
Since the pass destination from each student is exactly \(1\) person, the graph is a functional graph where the out-degree of each vertex is \(1\).
Each connected component of a functional graph has the following structure:
- Several trees rooted towards a cycle.
- Eventually, any path always enters a cycle.
In other words, the sequence of passes starting from a student \(i\) is a single path:
\[ i \rightarrow P_i \rightarrow P_{P_i} \rightarrow \cdots \]
and it is sufficient to look only at:
- The part before entering the cycle.
- The part that goes around the cycle once.
This is because if no one can solve the quiz even after going around the cycle once, the next pass would attempt to hand it to an already visited student, resulting in a “visited” state and leaving the quiz unsolved.
Naively simulating the passes for each quiz takes up to \(O(N)\) per query.
Since there are \(N\) quizzes, the overall time complexity would be \(O(N^2)\), which is too slow for \(N \leq 2 \times 10^5\).
Therefore, we need to optimize the following operation:
Given a starting student \(v\), find the first student \(j\) among the first \(L\) people along the path of passes who satisfies \(S_j \geq x\).
This can be solved using doubling (binary lifting).
Algorithm
1. Doubling Preparation
We prepare the following tables:
up[k][v]
The student reached by following the passes \(2^k\) times from student \(v\).mx[k][v]
The maximum academic ability \(S\) among the first \(2^k\) students starting from student \(v\).
For example, mx[k][v] is the maximum value of the following set:
\[ v,\ P_v,\ P_{P_v},\ \ldots \]
consisting of the first \(2^k\) students.
The base cases are:
\[ up[0][v] = P_v \]
\[ mx[0][v] = S_v \]
The transitions are:
\[ up[k][v] = up[k-1][up[k-1][v]] \]
\[ mx[k][v] = \max(mx[k-1][v],\ mx[k-1][up[k-1][v]]) \]
2. Finding the First Student Who Can Solve the Quiz
The function first_ge(start, len, x) returns:
The first student among the first
lenpeople starting fromstartwho satisfies \(S_j \geq x\).
The idea is as follows:
- If the maximum value of a certain interval is strictly less than \(x\), there is no student in that interval who can solve the quiz.
- Thus, we can skip that entire interval at once.
We iterate from the largest \(2^k\) downwards. If
mx[k][cur] < x
then there is no student among these \(2^k\) people who can solve it, so we skip them.
After skipping as much as possible, if there is still a remaining budget, the current student cur is the first student who satisfies the condition. If there is no such student within the limit, then no student satisfies the condition.
This process can be done in \(O(\log N)\) time.
3. Finding Cycles in the Functional Graph
For each student, we find the following information:
depth[v]
The distance from student \(v\) to the cycle.entry[v]
The first student on the cycle reached by starting from student \(v\).cycle_len[v]
The length of the cycle in the component that the student belongs to.
First, by pushing vertices with an in-degree of \(0\) into a queue and repeatedly removing them, the remaining vertices will be the ones on the cycles.
After that, by performing a BFS from the cycle vertices using the reversed edges, we can compute depth, entry, and cycle_len for the vertices in the tree parts as well.
4. Answering Each Quiz
The difficulty of the quiz distributed to student \(i\) is \(D_i\).
First, we check the path from \(i\) to the cycle entrance. The length of this part, including the cycle entrance, is:
\[ depth[i] + 1 \]
res = first_ge(i, depth[i] + 1, D[i]);
If a student is found here, that is the answer.
If not found, we check the cycle for one full rotation:
res = first_ge(entry[i], cycle_len[i], D[i]);
If still not found, the answer is \(-1\).
In the code, the cycle entrance might be checked twice (once in the tree part and once in the cycle part). However, since we already know the student at the entrance cannot solve the quiz if the first search fails, this overlap does not affect the correctness of the answer.
Complexity
- Time Complexity: \(O(N \log N)\)
- Space Complexity: \(O(N \log N)\)
Building the doubling tables takes \(O(N \log N)\) time, and processing each quiz takes \(O(\log N)\) time, resulting in an overall time complexity of \(O(N \log N)\).
Implementation Points
The input is \(1\)-indexed, but it is converted to \(0\)-indexed in the code for easier handling.
mx[k][v]represents the “maximum value among the \(2^k\) students starting from \(v\)”. The student pointed to byup[k][v]is not included in this range.For cycle detection, we use the method of removing vertices with an in-degree of \(0\). In a functional graph, the vertices remaining after this process are exactly the ones on the cycles.
When outputting the final answer, remember to convert the student’s index back to \(1\)-indexed.
Source Code
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
cin >> N;
vector<int> P(N);
for (int i = 0; i < N; i++) {
cin >> P[i];
--P[i];
}
vector<int> S(N), D(N);
for (int i = 0; i < N; i++) cin >> S[i];
for (int i = 0; i < N; i++) cin >> D[i];
int LOG = 1;
while ((1 << LOG) <= N) ++LOG;
vector<vector<int>> up(LOG, vector<int>(N));
vector<vector<int>> mx(LOG, vector<int>(N));
for (int i = 0; i < N; i++) {
up[0][i] = P[i];
mx[0][i] = S[i];
}
for (int k = 1; k < LOG; k++) {
for (int i = 0; i < N; i++) {
up[k][i] = up[k - 1][up[k - 1][i]];
mx[k][i] = max(mx[k - 1][i], mx[k - 1][up[k - 1][i]]);
}
}
vector<vector<int>> rev(N);
vector<int> indeg(N, 0);
for (int i = 0; i < N; i++) {
rev[P[i]].push_back(i);
indeg[P[i]]++;
}
queue<int> q;
vector<char> in_cycle(N, true);
for (int i = 0; i < N; i++) {
if (indeg[i] == 0) q.push(i);
}
while (!q.empty()) {
int v = q.front();
q.pop();
in_cycle[v] = false;
int to = P[v];
indeg[to]--;
if (indeg[to] == 0) q.push(to);
}
vector<int> depth(N, -1), entry(N, -1), cycle_len(N, 0);
queue<int> bfs;
for (int i = 0; i < N; i++) {
if (in_cycle[i] && cycle_len[i] == 0) {
vector<int> cyc;
int cur = i;
do {
cyc.push_back(cur);
cur = P[cur];
} while (cur != i);
int len = (int)cyc.size();
for (int v : cyc) {
depth[v] = 0;
entry[v] = v;
cycle_len[v] = len;
bfs.push(v);
}
}
}
while (!bfs.empty()) {
int v = bfs.front();
bfs.pop();
for (int to : rev[v]) {
if (!in_cycle[to] && depth[to] == -1) {
depth[to] = depth[v] + 1;
entry[to] = entry[v];
cycle_len[to] = cycle_len[v];
bfs.push(to);
}
}
}
auto first_ge = [&](int start, int len, int x) -> int {
int cur = start;
int rem = len;
for (int k = LOG - 1; k >= 0; k--) {
int pw = 1 << k;
if (pw <= rem && mx[k][cur] < x) {
cur = up[k][cur];
rem -= pw;
}
}
if (rem == 0) return -1;
return cur;
};
vector<int> ans(N);
for (int i = 0; i < N; i++) {
int res = first_ge(i, depth[i] + 1, D[i]);
if (res == -1) {
res = first_ge(entry[i], cycle_len[i], D[i]);
}
ans[i] = (res == -1 ? -1 : res + 1);
}
for (int i = 0; i < N; i++) {
if (i) cout << ' ';
cout << ans[i];
}
cout << '\n';
return 0;
}
This editorial was generated by gpt-5.5-high.