首页 > 试题广场 >

游历魔法王国

[编程题]游历魔法王国
  • 热度指数:157 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
魔法王国一共有n个城市,编号为0~n-1号,n个城市之间的道路连接起来恰好构成一棵树。
小易现在在0号城市,每次行动小易会从当前所在的城市走到与其相邻的一个城市,小易最多能行动L次。
如果小易到达过某个城市就视为小易游历过这个城市了,小易现在要制定好的旅游计划使他能游历最多的城市,请你帮他计算一下他最多能游历过多少个城市(注意0号城市已经游历了,游历过的城市不重复计算)。

输入描述:
输入包括两行,第一行包括两个正整数n(2 ≤ n ≤ 50)和L(1 ≤ L ≤ 100),表示城市个数和小易能行动的次数。
第二行包括n-1个整数parent[i](0 ≤ parent[i] ≤ i), 对于每个合法的i(0 ≤ i ≤ n - 2),在(i+1)号城市和parent[i]间有一条道路连接。


输出描述:
输出一个整数,表示小易最多能游历的城市数量。
示例1

输入

5 2
0 1 2 3

输出

3
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int n = sc.nextInt();
            int l = sc.nextInt();
            int[] parent = new int[n];
            for (int i = 1; i < n; i++) {
                parent[i] = sc.nextInt();
            }
            System.out.println(mostCities(parent, n, l));
        }
    }
    
    private static int mostCities(int[] parent, int n, int move) {
        int[] h = new int[n];    // h[i]是i号城市到0号城市的距离
        h[0] = 0;
        Queue<Integer> queue = new LinkedList<>();
        queue.add(0);
        int d = 0;
        while (!queue.isEmpty()) {
            int cur = queue.poll();
            for (int i = 1; i < n; i++) {
                if (parent[i] == cur) {
                    d = h[i] = h[cur] + 1;
                    queue.add(i);
                }
            }
        }
        // d是离0号城市最远的城市的距离,move是可移动次数
        // move<=d时,最多可游历move+1个城市
        // move>d时,最优的移动策略是保证沿着从0开始最长的路径单向走完
        //     且在此之前在从0开始的其他较短的路径上往返
        //     此时的城市数为:沿最长路径单向移动经过的城市数(d)
        //     + 剩余的步数用来在其他路径往返((move - d) / 2) + 0号城市(1)
        //     但结果不能超过城市的个数
        return (move <= d)? move + 1: Math.min(n, d + (move - d) / 2 + 1);
    }
}


编辑于 2018-07-09 18:07:18 回复(0)
一开始想错了,最长的路径是要走,但不是先走,而是最后走!所以剩下的步数除以二才是多出来的可以走的城市数量。
import java.util.*;
public class Main {
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
        int L = sc.nextInt();
 
		int[] depth = new int[n];
		int maxDepth = 0;
		for(int i=1; i<n; i++) {
			depth[i] = depth[sc.nextInt()] + 1;
			maxDepth = Math.max(maxDepth, depth[i]);
		}
		
		if(L<=maxDepth)
			System.out.println(L+1);
		else {
			int leftCanGoCity = (L-maxDepth)/2;
			int all = leftCanGoCity + maxDepth + 1;
			System.out.println(Math.min(all, n));
		}
	}
}


发表于 2019-09-05 19:49:10 回复(0)

为什么没人考虑一下,计算树的最大深度时,宽度优先遍历的顺序和节点遍历顺序(1~N-1)不一定是一致的吗?

考虑一下这个输入样例

10 5
3 3 0 0 4 4 1 1 1
得到的最大深度d应该是3,而不是2。
加了一个求宽度优先遍历顺序的过程。如下

#include <bits/stdc++.h>
#include <vector>
#include <queue>
#include <iostream>
using namespace std;
const int maxn = 50 + 5;
int n, L;
int parent[maxn];
int dp[200];

vector<int> find_child(int _parent,int n){
  vector<int> children;
  for(int i=1;i<n;i++){
    if(_parent == parent[i]){
      children.push_back(i);
    }
  }
  return children;
}
int main() {
  scanf("%d%d", &n, &L);
  for(int i = 1; i < n; i++) scanf("%d", &parent[i]);
  int mx = 0;

  queue<int> q;
  vector<int> travel_list;
  q.push(0);

  while(!q.empty()){
    travel_list.push_back(q.front());
    auto children = find_child(q.front(),n);

    // cout<<"q.front()"<<q.front()<<endl;
    q.pop();

    for(auto c:children){
      q.push(c);
    }

  }



  //去掉0号,因为它没有parent
  for(int i = 1; i < travel_list.size(); i++) {
    int j = travel_list[i];
    dp[j] = dp[parent[j]] + 1;
    mx = max(mx, dp[j]);
  }

  for(int i = 0; i < n - 1; i++) {
    dp[i + 1] = dp[parent[i]] + 1;
    mx = max(mx, dp[i + 1]);
  }
  int d = min(L, mx);
  cout << min(n, 1 + d + (L - d) / 2);
  return 0;
}
编辑于 2018-07-25 15:21:33 回复(0)