公式

G - 地域の特産品 / Local Specialty Products 解説 by admin

gpt-6-astra-medium

Overview

This problem asks us to divide cities connected by roads into groups and find the maximum value of the local specialties in each group. We can use a Disjoint Set Union (DSU / Union-Find) to maintain and update the maximum value whenever two groups are merged.

Analysis

Cities that can reach each other belong to the same connected component

Since roads are bidirectional, if it is possible to travel from city \(u\) to city \(v\), it is also possible to travel from city \(v\) to city \(u\). A group of cities that can mutually reach one another is called a connected component.

Cities belonging to the same connected component can reach the exact same set of cities. Therefore, the answer for all cities in the same connected component is the maximum value among all cities in that connected component.

For example, if cities \(1,2,3\) are connected by roads and have values \(4,9,2\) respectively, the answer for all of these cities is \(9\).

Searching from each city is too slow

One might consider running a BFS or DFS starting from each city to find the maximum value among reachable cities.

However, doing this independently for every city takes \(O(N(N+M))\) time in the worst case. Because the same connected component would be traversed repeatedly, this will not run in time under the constraints.

Therefore, we manage the connected components using a Union-Find. Furthermore, by maintaining the maximum value for each connected component, we avoid redundant calculations.

Algorithm

A Union-Find is a data structure that performs the following operations efficiently:

  • find(x): Finds the “root”, which is the representative of the group containing city \(x\).
  • unite(x, y): Merges the groups containing city \(x\) and city \(y\).

In this code, for each root \(r\), maximum[r] stores the maximum value within that group.

1. Initialization

Initially, before any roads are processed, each city forms its own individual group.

The maximum value of the group containing only city \(i\) is \(A_i\).

2. Merging groups for each road

Each time a road \((u,v)\) is read, we perform unite(u, v).

Letting the roots of \(u\) and \(v\) be \(x\) and \(y\) respectively, we process as follows:

  • If \(x=y\), they already belong to the same group, so do nothing.
  • If they belong to different groups, merge the smaller group into the larger group.
  • Letting \(x\) be the root after merging, update the maximum value as follows:

\[ \mathrm{maximum}[x] = \max(\mathrm{maximum}[x],\mathrm{maximum}[y]) \]

The merged group is the union of the two previous groups. Therefore, the larger of the two maximum values becomes the maximum value of the new group.

3. Outputting the answer for each city

After processing all roads, output the following as the answer for city \(i\):

dsu.maximum[dsu.find(i)]

By merging the endpoints of roads one by one, all cities that can reach each other by following roads will belong to the same group. Moreover, disconnected components will never be merged. Thus, the final groups coincide with the connected components.

Since the maximum value is correctly maintained during initialization and every merge, we can output the correct answer for each city.

Complexity

  • Time Complexity: \(O((N+M)\alpha(N))\)
  • Space Complexity: \(O(N)\)

Here, \(\alpha(N)\) is the inverse Ackermann function, which can be treated as practically constant. The code achieves this complexity by using path compression and union by size.

Roads can be processed as they are read, so there is no need to store all of them.

Implementation Notes

  • Convert city numbers to \(0\)-based indexing
    The input uses \(1\)-based indexing, but arrays are \(0\)-based, so use u - 1 and v - 1.

  • Combine parent pointer and group size in parent
    If parent[x] < 0, \(x\) is a root, and -parent[x] represents the size of the group. Otherwise, it represents the index of the parent vertex. Since the size is stored as a negative value, note that if parent[x] > parent[y], the group containing \(x\) is the smaller one.

  • Always retrieve the maximum value from the root
    After a merge, maximum is not updated for vertices that are no longer roots. When retrieving the answer, always query maximum[find(i)].

  • Naturally handles multi-edges and self-loops
    If the roots of both endpoints are identical, nothing is done, so no special handling is needed.

  • Isolated cities are handled correctly
    Since a city can always reach itself, the answer for an isolated city with no roads is \(A_i\). By setting the initial value to \(A_i\), cases where \(M=0\) are also handled without any modification.

    Source Code

#include <bits/stdc++.h>
using namespace std;

struct DSU {
    vector<int> parent, maximum;

    explicit DSU(const vector<int>& a)
        : parent(a.size(), -1), maximum(a) {}

    int find(int x) {
        if (parent[x] < 0) return x;
        return parent[x] = find(parent[x]);
    }

    void unite(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y) return;
        if (parent[x] > parent[y]) swap(x, y);
        parent[x] += parent[y];
        parent[y] = x;
        maximum[x] = max(maximum[x], maximum[y]);
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, M;
    cin >> N >> M;

    vector<int> A(N);
    for (int& a : A) cin >> a;

    DSU dsu(A);
    for (int j = 0; j < M; ++j) {
        int u, v;
        cin >> u >> v;
        dsu.unite(u - 1, v - 1);
    }

    for (int i = 0; i < N; ++i) {
        cout << dsu.maximum[dsu.find(i)] << '\n';
    }

    return 0;
}

This editorial was generated by gpt-6-astra-medium.