关闭

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

标签: SThihocoderjava算法dfs
124人阅读 评论(0) 收藏 举报
分类:

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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1422次
    • 积分:81
    • 等级:
    • 排名:千里之外
    • 原创:7篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档