find() method of PTA small generation simple modification and set lookup

Idea: as soon as you look at the data given by the title, remember to look up the set and simply modify the find() function of the set to meet the requirements. (but I can't write in Java and display the wrong answer. The same method can all pass in C + +, which makes me very confused.)

Core part

static int[] map; // Input data
	static int[] record; // Record seniority
	

	static int find(int val) {
		if (record[val] != 0) {// You've already known your seniority, quit directly
			return record[val];
		}
		if (map[val] == -1) { // It's the old ancestor.
			return record[val] = 1;
		}
		// It's not the ancestor. Continue to look down and determine your rank
		return record[val] = find(map[val]) + 1;
	}

Java code (failed, wrong answer)

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; // Determine the maximum rank
	static int[] map; // Input data
	static int[] record; // Record seniority
	

	static int find(int val) {
		if (record[val] != 0) {// You've already known your seniority, quit directly
			return record[val];
		}
		if (map[val] == -1) { // It's the old ancestor.
			return record[val] = 1;
		}
		// It's not the ancestor. Continue to look down and determine your rank
		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 + + code (all passed)

#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;
}
Published 16 original articles, won praise 2, visited 176
Private letter follow

Tags: Java

Posted on Fri, 07 Feb 2020 11:49:42 -0500 by d1223m