sankingshi
BAN USERpublic static class City{
private String name;
private int population;
private List<City> neighbors = new ArrayList<City>();
public City(String name, int population) {
super();
this.name = name;
this.population = population;
}
public static void addaddNeighborPair(final City c1, final City c2){
c1.addNeighbor(c2);
c2.addNeighbor(c1);
}
public void addNeighbor(City city){
this.neighbors.add(city);
}
public int getPopulation() {
return population;
}
public void setPopulation(int population) {
this.population = population;
}
public List<City> getNeighbors() {
return neighbors;
}
public void setNeighbors(List<City> neighbors) {
this.neighbors = neighbors;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public static int getMaxTraffic(City city){
int max = 0;
for( City c : city.neighbors){
max = Math.max(max, get(city, c));
}
return max;
}
public static int get(City gameCity, City city){
int traffic = city.population;
for(City neighbor : city.getNeighbors()){
if( gameCity != neighbor ){
traffic += get(city, neighbor);
}
}
return traffic;
}
If the gamecity only has one neighbor, the total traffic should be the summary of all other city's population.
- sankingshi August 11, 2016If the gamecity have more than one neighbors, to calculate the summary of population of every neighbor sub tree, then find the max one as the return.