链接:登录—专业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;
}