AtCoder题解——ACL Beginner Contest——C - Connect Cities

该博客讨论了一道编程题目,涉及到并查集数据结构的应用。题目要求通过构建道路连接所有城市,求解最少需要建造的道路数量。博主通过分析得出这是一个典型的并查集问题,给出了AC代码实现,利用并查集的合并与查找操作,计算出所需构建的最少道路数。

题目相关

题目链接

AtCoder ACL Beginner Contest C题,https://atcoder.jp/contests/abl/tasks/abl_c

Problem Statement

There are N cities numbered 1 through N, and M bidirectional roads numbered 1 through M. Road i connects City Ai and City Bi.

Snuke can perform the following operation zero or more times:

  • Choose two distinct cities that are not directly connected by a road, and build a new road between the two cities.

After he finishes the operations, it must be possible to travel from any city to any other cities by following roads (possibly multiple times).

What is the minimum number of roads he must build to achieve the goal?

Input

Input is given from Standard Input in the following format:

N M
A1 B1
:
AM BM

Output

Print the answer.

Samples1

Sample Input 1

3 1
1 2

Sample Output 1

1

Explaination

Initially, there are three cities, and there is a road between City 1 and City 2.

Snuke can achieve the goal by building one new road, for example, between City 1 and City 3. After that,

  • We can travel between 1 and 2 directly.
  • We can travel between 1 and 3 directly.
  • We can travel between 2 and 3 by following both roads (2 - 1 - 3).

Constraints

  • 2 ≤ N ≤ 100,000
  • 1 ≤ M ≤ 100,000
  • 1 ≤ Ai < Bi ≤N
  • No two roads connect the same pair of cities.
  • All values in input are integers.

题解报告

题目翻译

有 N 个城市,编号为 1 到 N。M 条双向道路,编号 1 到 M。第 i 条道路 Ai,连接了城市 Ai 和 Bi。

我们下面进行以下操作零次或者多次:

  • 选择两个不同城市,而且这两个城市之间没有直接道路连接。建立一条新的道路,连接这两个城市。

当完成这些操作后,我们应该可以从任意一个城市,移动到其他任何一个城市,不管中间是否需要经过其他城市。

为了达到这个目标,我们最少需要建设几条道路。

题目分析

非常典型的一个并查集问题。我们只需要根据题目提供的数据,如果这两个城市的父节点不是一样的,我们将这两个城市建立并集,并记录下当前操作次数。假设这个数据为 cnt。

我们知道 n 个城市最少只需要 n-1 条马路,就可以互相连接。

因此最终的答案是 n-1-cnt。

AC 参考代码

//https://atcoder.jp/contests/abl/tasks/abl_c
//查并集
#include <bits/stdc++.h>

using namespace std;

map<int, int> ds;
map<int, int> ranks;

int find_root(int x) {
    return x==ds[x]?x:ds[x]=find_root(ds[x]);
}

void union_set(int x, int y) {
    int x_root=find_root(x);
    int y_root=find_root(y);
    if (x_root!=y_root) {
        if (ranks[x_root]>ranks[y_root]) {
            ds[y_root]=x_root; 
        } else if (ranks[x_root]<ranks[y_root]) {
            ds[x_root]=y_root;
        } else {
            ds[x_root]=y_root;
            ranks[y_root]++;
        }
    }
}

int main() {
    int n,m;
    cin>>n>>m;

    //初始化
    for (int i=1; i<=n; i++) {
        ds[i]=i;
        ranks[i]=0;
    }

    int cnt=0;
    for (int i=1; i<=m; i++) {
        int x,y;
        cin>>x>>y;

        x=find_root(x);
        y=find_root(y);
        if (x==y) {
            continue;
        }

        union_set(x, y);
        cnt++;
    }

    cnt=n-1-cnt;
    if (cnt<0) {
        cnt=0;
    }
    cout<<cnt<<"\n";

    return 0;
}

<think>好的,用户现在想找AtCoderBeginner Contest408的题解或解思路。我需要先确认这个比赛是否已经结束,以及是否有公开的题解资源。由于AtCoder通常会在比赛后一段时间内发布官方题解,但有时候社区成员或博客作者也会分享他们的解方法。首先,我应该检查用户提供的引用内容,但看起来用户之前的引用是关于其他比赛的,比如ABC093和ABC405,可能与当前问无关。不过,用户可能需要类似的解思路结构,比如分讲解、代码示例等。接下来,我需要考虑用户可能的真实需求。他们可能参加了比赛但遇到困难,或者想通过题解来学习算法和编程技巧。因此,回答需要结构清晰,分目讲解思路,并给出关键点的解决方法,可能的话提供代码示例。 由于我无法直接访问外部网站,无法提供最新的ABC408的具体题解,但可以给出一般性的解步骤和常见问的解决方法。例如,对于ABC的目,通常前几涉及基础算法、模拟、数学问等,后面目可能涉及动态规划、图论等更复杂的算法。需要注意的是,用户可能希望得到具体的解思路,比如每道的关键点、算法选择以及代码实现。因此,我应该分目进行说明,可能按照A到F的顺序,逐一简要讲解。另外,用户可能需要参考之前的类似题解的格式,比如分说明、代码示例等。同时,根据用户之前的引用中提到的代码结构,可能需要使用Python或C++等语言的代码示例。最后,生成的相关问应该围绕如何准备AtCoder比赛、常见型解析、算法学习资源等,帮助用户进一步深入学习。</think>### AtCoder Beginner Contest 408 题解思路概述 根据AtCoder比赛惯例,ABC 408包含6道目(A-F),难度递增。以下为通用解框架: #### A(基础语法) 通常考察输入输出和简单计算。例如: ```python a, b = map(int, input().split()) print(a + b % 24) ``` **关键点**:注意时间循环处理[^1] #### B(条件判断) 可能涉及字符串处理或坐标判断。例如检测网格中的特定模式: ```python grid = [input() for _ in range(9)] count = 0 for i in range(9): for j in range(9): if grid[i][j] == '#' and check_pattern(i,j): count +=1 print(count) ``` #### C(贪心/数学) 常需数学建模,如求最大最小值的排列组合: $$ \max\left(\sum_{i=1}^n a_i \cdot b_{\sigma(i)}\right) $$ 可通过排序后对应相乘解决[^2] #### D(图论/动态规划) 典型解法示例: ```python from collections import deque def bfs(start): q = deque([start]) dist = [-1]*(n+1) dist[start] = 0 while q: u = q.popleft() for v in graph[u]: if dist[v] == -1: dist[v] = dist[u]+1 q.append(v) return max(dist) ``` #### E-F(高级算法) 可能涉及: 1. 线段树区间查询 2. 网络流建模 3. 组合数学优化
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力的老周

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值