G - 地域の特産品 / Local Specialty Products Editorial by admin Editorial for printing
Gemini 3.8 Flash (High)Overview
In an undirected graph where cities are vertices and roads are edges, this problem asks us to find, for each city, the maximum value of a local specialty among all vertices in its connected component.
Analysis
Since roads can be traversed in both directions, the relation “reachable from city \(u\) to city \(v\)” is an equivalence relation. That is, the graph is partitioned into several mutually unreachable groups (connected components).
Cities belonging to the same connected component can reach each other, while cities in different connected components cannot. From this, we can make the following key observation:
- For all cities belonging to the same connected component, the set of reachable cities is identical.
- Therefore, the answers for all cities in the same connected component are the same, which is “the maximum value of the local specialties among the cities in that connected component.”
Naive Approach and Its Issues
If we run a Breadth-First Search (BFS) or Depth-First Search (DFS) starting from each city \(i\), each search takes \(O(N + M)\) time. Performing this for all \(N\) cities results in an overall time complexity of \(O(N(N + M))\), which will exceed the time limit (TLE) under the constraints \(N, M \le 2 \times 10^5\).
Solution
When we find an unvisited city, we search the connected component containing that city only once. During the search, we record the “list of cities in that connected component” and the “maximum value of the local specialties.” Once the search is finished, we assign the computed maximum value as the answer for all cities in that component. With this method, each vertex and edge is examined at most a few times, allowing us to find the answers quickly.
Algorithm
We process using Breadth-First Search (BFS) as follows:
- Prepare a boolean array
visitedto track the visit status of each city, and initialize all entries tofalse. - Iterate through cities \(i = 1, 2, \dots, N\):
- If city \(i\) has already been visited (
visited[i] == true), skip it because its answer has already been determined by another search. - If city \(i\) is unvisited, start a BFS from city \(i\):
- Prepare a queue, push city \(i\), and set
visited[i] = true. - Prepare a list
componentto store the cities belonging to the current connected component. - Initialize a variable
max_valfor the maximum value with the value of city \(i\), \(A_i\). - Continue the search until the queue is empty: each time a city \(u\) is popped, add it to
componentand updatemax_val = max(max_val, A_u). Push adjacent unvisited cities into the queue. - Once the search is complete, set
ans[u] = max_valfor all cities \(u\) incomponent.
- Prepare a queue, push city \(i\), and set
- If city \(i\) has already been visited (
- Print the answer for all cities.
*Note: This problem can also be solved similarly using a Union-Find (Disjoint Set Union) data structure by maintaining the maximum value at the representative of each connected component.
Complexity
- Time Complexity: \(O(N + M)\)
- Constructing the graph takes \(O(N + M)\).
- In the BFS, every vertex and edge is pushed to the queue and visited at most once across the entire algorithm, taking \(O(N + M)\) in total.
- Therefore, the overall time complexity is \(O(N + M)\), which easily runs within the time limit.
- Space Complexity: \(O(N + M)\)
- Maintaining the adjacency list takes \(O(N + M)\) memory, while the
visitedarray, answer array, queue, etc., use \(O(N)\) memory.
- Maintaining the adjacency list takes \(O(N + M)\) memory, while the
Implementation Notes
Handling Multiple Edges and Self-loops:
- As stated in the problem statement, there may be multiple edges connecting the same pair of cities or self-loops connecting a city to itself. However, by using a visited array (
visited) in standard BFS, these are naturally ignored, so no special branch handling is required.
- As stated in the problem statement, there may be multiple edges connecting the same pair of cities or self-loops connecting a city to itself. However, by using a visited array (
Conversion to 0-indexed:
- The city indices in the input are \(1\)-indexed, from \(1\) to \(N\). Converting them to \(0\)-indexed (\(0\) to \(N-1\)) to match array indexing makes the implementation simpler.
Fast I/O:
Since the number of input and output lines can reach several hundred thousand, in C++, including
cin.tie(nullptr); ios::sync_with_stdio(false);and using"\n"instead ofstd::endlfor newlines helps prevent TLE.Source Code
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
if (!(cin >> n >> m)) return 0;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
vector<vector<int>> adj(n);
for (int i = 0; i < m; ++i) {
int u, v;
cin >> u >> v;
--u; --v;
adj[u].push_back(v);
adj[v].push_back(u);
}
vector<int> ans(n);
vector<bool> visited(n, false);
vector<int> component;
vector<int> q;
for (int i = 0; i < n; ++i) {
if (visited[i]) continue;
component.clear();
q.clear();
visited[i] = true;
q.push_back(i);
int max_val = a[i];
int head = 0;
while (head < (int)q.size()) {
int u = q[head++];
component.push_back(u);
max_val = max(max_val, a[u]);
for (int v : adj[u]) {
if (!visited[v]) {
visited[v] = true;
q.push_back(v);
}
}
}
for (int u : component) {
ans[u] = max_val;
}
}
for (int i = 0; i < n; ++i) {
cout << ans[i] << "\n";
}
return 0;
}
This editorial was generated by gemini-3.8-flash-high.
posted:
last update: