hihocoder 1069最近公共祖先(DFS + ST)java实现

package tree;


import java.util.ArrayList;

import java.util.HashMap;

import java.util.Map.Entry;

import java.util.Scanner;


/*

 * 8

adam sam

sam joey

tom lv

tom lee

kevin john

adam kevin

sam peter

sam tom

 */

public class Week17 {


public static void main(String[] args) {

Scanner scanner =new Scanner(System.in);

int datanum = Integer.parseInt(scanner.nextLine());

HashMap<String, ArrayList<String>> DFSmap = new HashMap<String, ArrayList<String>>();

ArrayList<DFSNode17> DFSarrAdep = new ArrayList<DFSNode17>();

HashMap<String, Integer> fAppearMap = new HashMap<String, Integer>();

DFSNode17 [][]ST =new DFSNode17[2*datanum + 2][2*datanum + 2];

String admisssion =null;

for(inti=0; i<datanum;i++){

String data =scanner.nextLine();

String father =data.split(" ")[0];

String son =data.split(" ")[1];

if(i==0)admisssion = father;

Week17.createDFSmap(DFSmap,father, son);

}

Week17.DFS(admisssion, 0,DFSmap, DFSarrAdep,fAppearMap);

Week17.createST(DFSarrAdep,ST);

int querynum = Integer.parseInt(scanner.nextLine());

for(inti=0; i<querynum;i++){

String data =scanner.nextLine();

String p1 =data.split(" ")[0];

String p2 =data.split(" ")[1];

Week17.searchST(p1,p2, ST, fAppearMap);

}

}

public static void createDFSmap(HashMap<String, ArrayList<String>>DFSmap, String father, Stringson){

if(DFSmap.containsKey(father)){

ArrayList<String> temp = DFSmap.get(father);

temp.add(son);

}

else{

ArrayList<String> temp = new ArrayList<String>();

temp.add(son);

DFSmap.put(father,temp);

}

}

public static void DFS(String admisssion, int depth, HashMap<String, ArrayList<String>> DFSmap, ArrayList<DFSNode17>DFSarrAdep, HashMap<String, Integer> fAppearMap){

DFSarrAdep.add(new DFSNode17(admisssion,depth));

if(!fAppearMap.containsKey(admisssion))fAppearMap.put(admisssion,DFSarrAdep.size()-1);

if(DFSmap.containsKey(admisssion)){

ArrayList<String> temp = DFSmap.get(admisssion);

for(inti=0; i<temp.size();i++){

DFS(temp.get(i), ++depth,DFSmap, DFSarrAdep,fAppearMap);

depth--;

DFSarrAdep.add(new DFSNode17(admisssion,depth));

}

}

}

public static void createST(ArrayList<DFSNode17>DFSarrAdep, DFSNode17 [][]ST){

for(inti=1; i<ST[0].length;i*=2){

for(intj=1; j<ST.length;j++){

if(i==1)ST[j][i] =DFSarrAdep.get(j-1);

else{

if(j +i/2 < ST.length){

int depth1 = ST[j][i/2].getDepth();

int depth2 = ST[j +i/2][i/2].getDepth();

ST[j][i] =depth1 > depth2 ?ST[j + i/2][i/2] : ST[j][i/2];

}

else ST[j][i] =ST[j][i/2];

}

}

}

}

public static void searchST(String p1, String p2, DFSNode17 [][]ST, HashMap<String, Integer>fAppearMap){

if(p1.equals(p2)) System.out.println(p1);

else{

intappear1 = fAppearMap.get(p1) + 1;

intappear2 = fAppearMap.get(p2) + 1;

intleft, right;

if(appear1 >appear2){

left =appear2;

right =appear1;

}

else{

left =appear1;

right =appear2;

}

int range = right - left + 1;

int power = 0;

while(range != 0){

range >>= 1;

power++;

}

int depth1 = ST[left][(int)Math.pow(2,power-1)].getDepth();

int depth2 = ST[right - (int)Math.pow(2,power-1) + 1][(int)Math.pow(2,power-1)].getDepth();

String name =depth1 <= depth2 ?ST[left][(int)Math.pow(2,power-1)].getValue() : 

ST[right - (int)Math.pow(2,power-1) + 1][(int)Math.pow(2,power-1)].getValue(); 

System.out.println(name);

}

}

}


class DFSNode17{

private Stringvalue;

privateint depth;

public DFSNode17(Stringvalue, int depth){

this.value =value;

this.depth =depth;

}


public String getValue() {

returnvalue;

}


public void setValue(String value) {

this.value =value;

}


public int getDepth() {

returndepth;

}


public void setDepth(int depth) {

this.depth =depth;

}


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值