一道题两种解法:深度优先搜索、广度优先搜索-AtCode ABC204 C - Tour

该篇博客探讨了如何利用深度优先搜索(DFS)和广度优先搜索(BFS)解决图论问题,具体是计算在一个有向图中起点和终点城市的组合数量。题目描述了一个国家的交通网络,其中不允许反向行驶。作者提供了两种解决方案,分别是DFS和BFS,通过遍历所有可能的起点,寻找能够到达的所有终点。样例输入和输出展示了算法的正确性,并给出了官方的两种解题方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

标签

  • DFS、BFS

题目地址

C - Tour

  • https://atcoder.jp/contests/abc204/editorial/1991

問題描述

The republic of AtCoder has N cities numbered 1 through N and Mroads numbered 1 through M.

Road i leads from City Ai_ii to City Bi_ii, but you cannot use it to get from City Bi_ii to City Ai_ii.

Puma is planning her journey where she starts at some city, travels along zero or more roads, and finishes at some city.

How many pairs of cities can be the origin and destination of Puma’s journey? We distinguish pairs with the same set of cities in different orders.

Constraints

  • 2 ≤\leq N ≤\leq 2000
  • 0 ≤\leq M ≤\leq min⁡\minmin(2000,N(N-1))
  • 1 ≤\leq Ai_ii,Bi_ii ≤\leq N
  • Ai_ii ≠\neq= Bi_ii
  • (Ai_ii,Bi_ii) are distinct.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A1 B1
...
AM BM

Output

Print the answer.


Sample Input 1

3 3
1 2
2 3
3 2

Sample Output 1

7

We have seven pairs of cities that can be the origin and destination: (1,1),(1,2),(1,3),(2,2),(2,3),(3,2),(3,3).


Sample Input 2

3 0

Sample Output 2

3

We have three pairs of cities that can be the origin and destination: (1,1),(2,2),(3,3).


Sample Input 3

4 4
1 2
2 3
3 4
4 1

Sample Output 3

16

Every pair of cities can be the origin and destination.

题意

AtCoder王国有

  • N个编号为1到N的城市
  • M条编号为1到M的道路。

道路i从Ai_ii市通往Bi_ii市,但不能能用从Bi_ii市通往Ai_ii市。

彪马正在计划她的旅程,她从某个城市出发,沿着零条或多条道路旅行,然后在某个城市结束。

求有多少种起点和终点城市组合?

思路

  • 固定起始点,然后遍历到达可能的城市
  • 可以考虑使用DFS、BFS算法遍历

题解

小码匠: DFS

int n;
vector<bool>leaf(n);

void dfs(int x, vector<vector<int>> &vvi) {
    if(leaf[x]) {
        return;
    }
    leaf[x] = true;
    for(auto a : vvi[x]) {
        dfs(a, vvi);
    }
}

void coder_solution() {
    // 提升cin、cout效率
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int m, temp = 0;
    cin >> n >> m;
    vector<vector<int>> vvi(n);
    int a, b;
    for (int i = 0; i < m; i++) {
        cin >> a >> b;
        vvi[a - 1].push_back(b - 1);
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            leaf[j] = false;
        }
        dfs(i, vvi);
        for (int j = 0; j < n; j++) {
            if (leaf[j]) {
                temp++;
            }
        }
    }
}

小码匠: BFS

void best_solution() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n, m;
    cin >> n >> m;
    int a, b;
    vector<vector<int>> vvi(n);
    for(int i = 0; i < m; i++) {
        cin >> a >> b;
        vvi[a - 1].push_back(b - 1);
    }

    vector<int> dist(n, -1);
    queue<int> que;
    dist[0] = 0;
    int v;
    int j = 0;
    for(int i = 0; i < n; i++) {
        que.push(i);
        dist[i] = 0;
        while(!que.empty()) {
            v = que.front();
            que.pop();
            for(auto next_v : vvi[v]) {
                if (dist[next_v] != -1) {
                    continue;
                }
                dist[next_v] = dist[v] + 1;
                que.push(next_v);
            }
        }
        j += n - count(dist.begin(), dist.end(), -1);
        dist.assign(n, -1);
    }
    cout << j;
}

官方题解1:BFS

  • 一些语法感觉有些诡异,例如第8行,第14行的循环语句,老码农建议不要学,不符合多数人习惯
#include<bits/stdc++.h>
using namespace std;

int main(){
    int n, m;
    cin >> n >> m;
    auto e = vector(n, vector<int>());
    for(int i{}, a, b; i < (m); ++i){
        cin >> a >> b;
        --a, --b;
        e[a].push_back(b);
    }
    ll ans{};
    for(int s{}; s< (n); ++s){
        vector<bool> vis(n);
        vis[s] = true;
        queue<int> q;
        q.push(s);
        while(!q.empty()){
            ++ans;
            int i = q.front();
            q.pop();
            for(auto&j:e[i]) if(!vis[j]){
                vis[j] = true;
                q.push(j);
            }
        }
    }
    cout << ans << endl;
}

官方题解2:DFS

#include<bits/stdc++.h>

using namespace std;
const int MAX_N = 2000;
vector<vector<int>> G;
bool temp[MAX_N];

void dfs(int v) {
    if (temp[v]) {
        return;
    }
    temp[v] = true;
    for (auto vv: G[v]){
        dfs(vv);
    }
}

int main() {
    int N, M;
    cin >> N >> M;
    G.resize(N);
    for (int i = 0; i < M; i++) {
        int a, b;
        cin >> a >> b;
        G[a - 1].push_back(b - 1);
    }
    int ans = 0;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            temp[j] = false;
        }
        dfs(i);
        for (int j = 0; j < N; j++) {
            if (temp[j]){
                ans++;
            |
        }
    }
    cout << ans;
}

复盘

  • 动态数组初始化

    dist.assign(n, -1);
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小码匠和老码农

喜欢作者

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

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

打赏作者

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

抵扣说明:

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

余额充值