2024牛客暑期多校训练营5 H 入

链接:登录—专业IT笔试面试备考平台_牛客网
来源:牛客网
 

题目描述

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

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 ai​ 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.

输入描述:

The first line contains two positive integers n,m.

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

输出描述:

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

示例1

输入

4 5
3 4
2 4
1 4
1 2
1 3

输出

3

示例2

输入

6 8
4 6
1 6
4 5
5 6
2 5
2 4
2 6
3 5

输出

4

备注:

1≤n≤40,1≤m≤(2n​),1≤u,v≤n.

题目大意:给你n个点,m条边,每个点的权值各不相同(由你自己决定),当前点必须跑到于其相邻的最小权值的点,问最多能经过几个点。

思路:爆搜即可。(注意:因为每次只能到相邻最小值的点,所以遇到多个相邻点需要一起跑)。

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int a[50],vis[50],in[50];
vector<int> G[50];
int n,m,ans=0;
void dfs(int x,int sum){
	ans=max(sum,ans);
	vector<int> v;
	for(int i=0;i<G[x].size();i++){
		if(!vis[G[x][i]]){
			v.push_back(G[x][i]);
			vis[G[x][i]]=1;
		}
	}
	for(int i=0;i<v.size();i++){
		dfs(v[i],sum+1);
	}
	for(int i=0;i<v.size();i++){
		vis[v[i]]=0;
	}
}
signed main()
{
	IOS
	cin >> n >> m;
	while(m--){
		int u,v;
		cin >> u >> v;
		G[u].push_back(v);
		G[v].push_back(u);
	}
	for(int i=1;i<=n;i++){
		vis[i]=1;
		dfs(i,1);
		vis[i]=0;
	}
	cout << ans << endl;
	return 0;
}

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值