PTA 小字輩 簡單修改並查集的find()方法

在這裏插入圖片描述

思路:一看題目給的數據,就想起來並查集,將並查集的find()函數簡單修改即可達到要求。(但是我使用Java寫的無法通過,顯示答案錯誤,同樣的方法在C++中卻得以全部通過,讓我很是疑惑)

核心部分

static int[] map; // 輸入數據
	static int[] record; // 記錄輩分
	

	static int find(int val) {
		if (record[val] != 0) {// 輩分已經知道,直接退出
			return record[val];
		}
		if (map[val] == -1) { // 是老祖宗
			return record[val] = 1;
		}
		// 不是老祖宗,繼續往下找,並確定自己的輩分
		return record[val] = find(map[val]) + 1;
	}

Java代碼(沒有通過,答案錯誤)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.StringTokenizer;

public class Main {
	public static void main(String[] args) throws IOException {
		Reader.init(System.in);
		int N = Reader.nextInt();
		map = new int[100005];
		record = new int[100005];
		for (int i = 1; i <= N; i++) {
			map[i] = Reader.nextInt();
		}
		for (int i = 1; i <= N; i++) {
			find(i);
			if (max < record[i])
				max = record[i];
		}
		System.out.println(max);
		boolean first = true;
		for (int i = 1; i <= N; i++) {
			if (record[i] == max) {
				if(first) {
					first = false;
					System.out.print(i);
				}else {
					System.out.print(" " + i);
				}
			}
		}

	}
    static int max = 0; // 確定最大輩分
	static int[] map; // 輸入數據
	static int[] record; // 記錄輩分
	

	static int find(int val) {
		if (record[val] != 0) {// 輩分已經知道,直接退出
			return record[val];
		}
		if (map[val] == -1) { // 是老祖宗
			return record[val] = 1;
		}
		// 不是老祖宗,繼續往下找,並確定自己的輩分
		return record[val] = find(map[val]) + 1;
	}

}

// Class for buffered reading int and double values *//*
class Reader {
	static BufferedReader reader;
	static StringTokenizer tokenizer;

	// ** call this method to initialize reader for InputStream *//*
	static void init(InputStream input) {
		reader = new BufferedReader(new InputStreamReader(input));
		tokenizer = new StringTokenizer("");
	}

	static void init(File file) {
		try {
			reader = new BufferedReader(new FileReader(file));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		tokenizer = new StringTokenizer("");
	}

	// ** get next word *//*
	static String next() throws IOException {
		while (!tokenizer.hasMoreTokens()) {
			// TODO add check for eof if necessary
			tokenizer = new StringTokenizer(reader.readLine());
		}
		return tokenizer.nextToken();
	}

	static String nextLine() throws IOException {
		return reader.readLine();
	}

	static int nextInt() throws IOException {
		return Integer.parseInt(next());
	}

	static char nextChar() throws IOException {
		return next().toCharArray()[0];
	}

	static float nextFloat() throws IOException {
		return Float.parseFloat(next());
	}

	static Double nextDouble() throws IOException {
		return Double.parseDouble(next());
	}
}

C++代碼(全部通過)

#include<iostream>
using namespace std;
int map[100005];
int record[100005];
int find(int i){
	if(record[i]!=0){
		return record[i];
	}
	if(map[i]==-1){
        record[0]--;
		return record[i] = 1;
	}
    record[0]--;
	record[i]= find(map[i])+1;;
	return record[i];	
}
int main(){
	int n;
	cin>>n;
	record[0] = n;
	for(int i=1;i<=n;i++){
		cin>>map[i];
	}
	int max=0;
	for(int i=1;i<=n && record[0]>0;i++){
		find(i);
		if(record[i]>max){
			max=record[i];
		}
	}
	cout<<max<<endl;
	int flag=1;
	for(int i=1;i<=n;i++){
		if(record[i]==max){
			if(flag){
			cout<<i;
			flag=0;	
			}else{
				cout<<" "<<i;
			}
			
		}
	}
	return 0;
}
發佈了20 篇原創文章 · 獲贊 2 · 訪問量 636
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章