L2-012 關於堆的判斷 (25分) java

將一系列給定數字順序插入一個初始爲空的小頂堆H[]。隨後判斷一系列相關命題是否爲真。命題分下列幾種:

  • x is the rootx是根結點;
  • x and y are siblingsxy是兄弟結點;
  • x is the parent of yxy的父結點;
  • x is a child of yxy的一個子結點。

輸入格式:

每組測試第1行包含2個正整數N≤\le 1000)和M≤\le 20),分別是插入元素的個數、以及需要判斷的命題數。下一行給出區間[−10000,10000][-10000, 10000][10000,10000]內的N個要被插入一個初始爲空的小頂堆的整數。之後M行,每行給出一個命題。題目保證命題中的結點鍵值都是存在的。

輸出格式:

對輸入的每個命題,如果其爲真,則在一行中輸出T,否則輸出F

輸入樣例:

5 4
46 23 26 24 10
24 is the root
26 and 23 are siblings
46 is the parent of 23
23 is a child of 10

輸出樣例:

F
T
F
T
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.regex.Pattern;

import javax.management.monitor.Monitor;


//** 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("");
	}

	// ** get next word *//*
	static String next() throws IOException {
		while (!tokenizer.hasMoreTokens()) {
			// TODO add ceck for eof if necessary
			tokenizer = new StringTokenizer(reader.readLine());
		}
		return tokenizer.nextToken();
	}
	static boolean hasNext()throws IOException {
		return tokenizer.hasMoreTokens();
	}
	static String nextLine() throws IOException{
		return reader.readLine();
	}
	static char nextChar() throws IOException{
		return next().charAt(0);
	}
	static int nextInt() throws IOException {
		return Integer.parseInt(next());
	}
	static long nextLong() throws IOException {
		return Long.parseLong(next());
	}
	static float nextFloat() throws IOException {
		return Float.parseFloat(next());
	}
	static double nextDouble() throws IOException {
		return Double.parseDouble(next());
	}
	static void close() throws IOException {
		reader.close();
	}
}
class Writer{
	static BufferedWriter writer;
	static void init(OutputStream outputStream) {
		writer = new BufferedWriter(new OutputStreamWriter(outputStream));
	}
	static void print(Object object) throws IOException {
		writer.write(object.toString());
	}
	static void println(Object object) throws IOException {
		writer.write(object.toString());
		writer.write("\n");
	}
	static void close() throws IOException {
		// TODO Auto-generated method stub
		writer.close();
	}
}
public class Main {
	
	public static void main(String[] args) throws IOException {
		Reader.init(System.in);
		Writer.init(System.out);
		solve();
		Reader.close();
		Writer.close();
	}
	static int[]heap;
	static int size;
	private static void solve() throws IOException {
		int n = Reader.nextInt();
		int m = Reader.nextInt();
		heap = new int[1005];
		create();
		for (int i = 0; i < n; i++) {
			int x = Reader.nextInt();
			insert(x);
		}
		for (int i = 0; i < m; i++) {
			String s = Reader.nextLine();
			String[]t = s.split(" ");
			if (s.contains("is the root")) {
				if (Integer.parseInt(t[0])==heap[1]) {
					Writer.println("T");
				}else {
					Writer.println("F");
				}
			}else if(s.contains("are siblings")) {
				int x = Integer.parseInt(t[0]);
				int y = Integer.parseInt(t[2]);
				if (search(x)/2 == search(y)/2) {
					Writer.println("T");
				}else {
					Writer.println("F");
				}
			}else if(s.contains("is the parent of")) {
				int x = Integer.parseInt(t[0]);
				int y = Integer.parseInt(t[5]);
				if (search(x)==search(y)/2) {
					Writer.println("T");
				}else {
					Writer.println("F");
				}
			}else if(s.contains("is a child of")) {
				int x = Integer.parseInt(t[0]);
				int y = Integer.parseInt(t[5]);
				int ix = search(x);
				int iy = search(y);
				if (ix == iy*2||ix == iy*2+1) {
					Writer.println("T");
				}else {
					Writer.println("F");
				}
			}
			
		}
	}
	static void create() {
		size = 0;
		heap[0] = -100005;
	}
	private static void insert(int x) {
		int i ;
		for (i = ++size; heap[i/2] > x; i/=2) {
			heap[i] = heap[i/2];
		}
		heap[i] = x;
	}
	static int search(int data) {
		for (int i = 0; i < heap.length; i++) {
			if (heap[i]==data) {
				return i;
			}
		}
		return -1;
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章