PAT-2021年秋季考试 - 甲级 7-3 Playground Exploration (C++)

在这里插入图片描述
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 ( ≤ 100 ) N (≤100) N(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 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.

Caution:

为什么说这次考试简单呢?因为图的这道题看清题目后你甚至都不用 DFS。

附上其他三道题的解答:7-17-27-4

Solution:

// Talk is cheap, show me the code
// Created by Misdirection 2021-09-11 15:21:52
// All rights reserved.

#include <iostream>
#include <vector>

using namespace std;

int n, m;
bool G[105][105];
int maxNum = -1, ansStart = -1;
bool vis[105];

int getPathLen(int idx){

    int cnt = 1;
    int pos = idx;

    while(true){
        vis[pos] = true;
        int i;
        for(i = 1; i <= n; ++i){
            if(i == pos || vis[i] || !G[pos][i]) continue;

            cnt++;
            pos = i;
            break;
        }

        if(i == n + 1) break;
    }

    return cnt;
}

int main(){
    // n - the total number of spots
    // m - the number of tunnels
    scanf("%d %d", &n, &m);

    for(int i = 0; i < m; ++i){
        int v1, v2;
        scanf("%d %d", &v1, &v2);

        G[v1][v2] = true;
        G[v2][v1] = true;
    }

    for(int i = 1; i <= n; ++i){
        fill(vis, vis + 105, false);

        int len = getPathLen(i);

        if(len > maxNum){
            maxNum = len;
            ansStart = i;
        }
    }

    printf("%d %d\n", ansStart, maxNum);

    return 0;
}

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

负反馈循环

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

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

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

打赏作者

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

抵扣说明:

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

余额充值