题解:2024牛客暑期多校第5场——H [入]

https://ac.nowcoder.com/acm/contest/81600/H

英文题干

There is an undirected graph where each vertex has a unique weight .

Initially, a chess piece is placed on a vertex. In each move, the piece moves to the adjacent vertex with the smallest weight. If all adjacent vertices have weights greater than the current vertex, the movement stops immediately.

You can freely assign weights to the vertices (ensuring they are all unique) and choose the initial position of the chess piece. Your goal is to maximize the number of vertices visited by the chess piece.

Input:

The first line contains two positive integers .

The next lines each contain two positive integers , indicating an edge in the graph.

Output:

Output a single integer, which is the maximum number of vertices visited by the chess piece.

中文题干

有一个无向图,其中每个顶点都有唯一权重

最初,一个棋子被放置在一个顶点上。在每一步中,棋子移动到权重最小的相邻顶点。如果所有相邻顶点的权重大于当前顶点,则移动立即停止。

你可以自由分配顶点的权重 (确保它们都是唯一的),并选择棋子的初始位置。你的目标是最大化棋子访问的顶点数量。

思路

AC代码

时间复杂度:O()

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int infmin = 0xc0c0c0c0;
const int infmax = 0x3f3f3f3f;
const int maxn = 45;

vector<int> mp[maxn];
int vis[maxn]; // 可能被多次标记,开int
int ans = 0;

void dfs(int cur, int le) {  // 当前节点和当前总边数
    ans = max(ans, le);
    for (auto nxt : mp[cur]) {
        if (!vis[nxt]) {
            for (auto v : mp[cur]) {
                vis[v]++;
            }
            dfs(nxt, le + 1);
            for (auto v : mp[cur]) {
                vis[v]--;
            }
        }
    }
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int n, m;
    cin >> n >> m;
    int n1, n2;
    for (int i = 1; i <= m; i++) {
        cin >> n1 >> n2;
        mp[n1].push_back(n2);
        mp[n2].push_back(n1);
    }

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            vis[j] = 0;
        }
        vis[i] = 1;
        dfs(i, 0);
    }

    cout << ans + 1;
    return 0;
}
// Inspired by 学习中ing
全部评论

相关推荐

今年读完研的我无房无车无对象,月入还没有过万&nbsp;看到他在朋友圈晒房产证,感叹自己白读了这么多年书
梦想是成为七海千秋:那咋了,双9毕业的现在还没存款呢(因为没念完),高中毕业的去直播带货月入几百万也是完全有可能的,退一万步讲,有些人刚出生父母就给买车买房了,上哪说理去,哪怕是同一个起点也会有截然不同的走向,过好自己的生活就完事了。
点赞 评论 收藏
分享
06-04 09:27
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务