PAT甲级-2021秋-第3题-DFS遍历-AC代码

7-3 Playground Exploration (25 分)

A playground is equipped with ball pits and tents connected by tunnels. Kids can crawl through the tunnels to meet their friends at a spot with a tent or a ball pit.
Now let’s mark each meeting spot (a tent or a ball pit) by a number. Assume that once a kid starts to explore the playground from any meeting spot, he/she will always choose the next destination with the smallest number, and he/she would never want to visit the same spot twice. Your job is to help the kids to find the best starting spot so they can explore as many spots as they can.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers N N N ( ≤ 100 ≤ 100 100), the total number of spots, and M M M, the number of tunnels. Then M M M lines follow, each describes a tunnel by giving the indices of the spots (from 1 1 1 to N N N) at the two ends.

Output Specification:

Print in a line the best starting spot which leads to the maximum number of spots, and the number of spots a kid can explore. If the solution is not unique, output the one with the smallest index. There must be exactly 1 space between the two numbers, and there must be no extra space at the beginning or the end of the line.

Sample Input:

8 10
1 2
3 4
5 8
1 4
6 1
3 7
2 4
5 3
2 8
2 5

Sample Output:

6 7

Hint:

Actually there are two solutions. Kids can either start from 6 and go through 1->2->4->3->5->8, or from 7 to 3->4->1->2->5->8, both can visit 7 spots. Since 6 is a smaller index, we output 6 as the starting spot.

满分题解

/**
 * 题目:PAT_A实战-XFS-简单-2021秋-DFS遍历
 *
 * 说明:
 * 1. 输入:n,m, m行边(u,v)
 * 2. 任务:如上所述
 * 3. 输出:最佳起点,能遍历到的最多点数
 *
 * 技巧:
 * 1. 当时想先暴力做这题然后等着超时的,没想到过了,甚至有点小失望(后来想想N那么小,能暴力也很合理);
 *    图论的题的难度奠定了PAT甲级的难度,所以这次考试偏简单
 *
 */
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<climits>
using namespace std;

#define MAXN        109

int n;
int m;
int w[MAXN][MAXN];

int vis[MAXN];
int cnt;
int cnt_max;
int s_ret;

void dfs(int u)
{
    vis[u] = 1;
    ++cnt;
    for (int v = 1; v <= n; ++v) {
        if (vis[v] == 0 && w[u][v] == 1) {
            dfs(v);
            break;
        }
    }
    vis[u] = 2;
}

int main(void)
{
    cin >> n >> m;
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= n; ++j) {
            w[i][j] = INT_MAX;
        }
    }
    for (int i = 0; i < m; ++i) {
        int u, v;
        cin >> u >> v;
        w[u][v] = w[v][u] = 1;
    }

    cnt_max = 0;
    for (int s = 1; s <= n; ++s) {
        cnt = 0;
        memset(vis, 0, sizeof(vis));
        dfs(s);
        if (cnt_max < cnt) {
            cnt_max = cnt;
            s_ret = s;
        }
    }

    cout << s_ret << " " << cnt_max;
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

XtionDoubleAI

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

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

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

打赏作者

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

抵扣说明:

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

余额充值