Official

C - ネットワークの整理 / Organizing the Network Editorial by admin

gpt-5.5-xhigh

Overview

Given an undirected graph consisting of \(N\) computers and \(N\) cables, we determine whether it is possible to remove at most \(K\) cables to make the graph cycle-free, i.e., a forest.

We find the minimum number of cables that must be removed to eliminate all cycles, and check whether this number is at most \(K\).

Analysis

In a cycle-free undirected graph, i.e., a forest, each connected component with \(v\) vertices has at most \(v-1\) edges.

Therefore, to eliminate cycles, we need to remove “redundant edges.”

For example, consider examining edges one by one:

  • An edge connecting two vertices that are not yet connected
    → Adding this edge does not create a cycle.
  • An edge connecting two vertices that already belong to the same connected component
    → Adding this edge creates a cycle.

Edges of the latter type are “redundant edges” that must be removed somewhere to make the graph a forest.

For example,

1 - 2
2 - 3
1 - 3

With these 3 edges, the first two already connect vertices \(1, 2, 3\).
Adding the edge \(1-3\) on top of that creates a cycle, so this edge is redundant.

Self-loops and multi-edges can be handled in the same way.

  • A self-loop connects a vertex to itself, so it immediately forms a cycle.
  • For multi-edges, the first edge connects the two vertices, and any subsequent edges create cycles.

A brute-force approach of trying all possible subsets of cables to remove would require up to \(2^N\) possibilities, which is far too slow for \(N \leq 2 \times 10^5\).

Instead, we use Union-Find (DSU) to efficiently determine whether each edge creates a cycle.

Algorithm

Using Union-Find, we examine each edge one by one.

  1. Initially, each vertex belongs to its own separate connected component.
  2. For each cable \((A_i, B_i)\), do the following:
    • If \(A_i\) and \(B_i\) are still in different connected components, merge them.
    • If they are already in the same connected component, adding this cable would create a cycle, so count it as a redundant cable.
  3. Let redundant be the number of redundant cables.
  4. If redundant <= K, output Yes; otherwise, output No.

Edges that were successfully merged by Union-Find can remain as edges forming the forest.
On the other hand, edges that could not be merged are cycle-creating edges and are candidates for removal.

Ultimately, if we keep only the successfully merged edges, we obtain a cycle-free forest.
Therefore, the minimum number of edges that need to be removed is redundant.

Complexity

  • Time complexity: \(O(N \alpha(N))\)
  • Space complexity: \(O(N)\)

Here, \(\alpha(N)\) is the inverse Ackermann function, which can be considered virtually constant in practice.

Implementation Notes

Making unite(a, b) in Union-Find return whether the operation was successful leads to a concise implementation.

if (!dsu.unite(A, B)) redundant++;

By writing it this way, all of the following cases:

  • Self-loops
  • Edges connecting vertices already in the same connected component
  • Second or subsequent multi-edges

are all collectively counted as “redundant edges.”

Source Code

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

struct DSU {
    vector<int> parent, sz;
    DSU(int n) : parent(n + 1), sz(n + 1, 1) {
        iota(parent.begin(), parent.end(), 0);
    }
    int find(int x) {
        if (parent[x] == x) return x;
        return parent[x] = find(parent[x]);
    }
    bool unite(int a, int b) {
        a = find(a);
        b = find(b);
        if (a == b) return false;
        if (sz[a] < sz[b]) swap(a, b);
        parent[b] = a;
        sz[a] += sz[b];
        return true;
    }
};

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

    int N, K;
    cin >> N >> K;

    DSU dsu(N);
    int redundant = 0;

    for (int i = 0; i < N; i++) {
        int A, B;
        cin >> A >> B;
        if (!dsu.unite(A, B)) redundant++;
    }

    cout << (redundant <= K ? "Yes" : "No") << '\n';
    return 0;
}

This editorial was generated by gpt-5.5-xhigh.